/
internal.ts
91 lines (73 loc) · 4.18 KB
/
internal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import fs from 'fs-extra';
import type { BuildOptions } from '../utils/options';
import { cleanDts } from '../utils/bundle-dts';
import { internalAppData } from './internal-app-data';
import { internalClient } from './internal-platform-client';
import { internalHydrate } from './internal-platform-hydrate';
import { internalTesting } from './internal-platform-testing';
import { join } from 'path';
import { writePkgJson } from '../utils/write-pkg-json';
export async function internal(opts: BuildOptions) {
const inputInternalDir = join(opts.buildDir, 'internal');
await fs.emptyDir(opts.output.internalDir);
await copyStencilInternalDts(opts, opts.output.internalDir);
await copyStencilCoreEntry(opts);
// copy @stencil/core/internal default entry, which defaults to client
// but we're not exposing all of Stencil's internal code (only the types)
await fs.copyFile(join(inputInternalDir, 'default.js'), join(opts.output.internalDir, 'index.js'));
// write @stencil/core/internal/package.json
writePkgJson(opts, opts.output.internalDir, {
name: '@stencil/core/internal',
description: 'Stencil internals only to be imported by the Stencil Compiler. Breaking changes can and will happen at any time.',
main: 'index.js',
types: 'index.d.ts',
});
const clientPlatformBundle = await internalClient(opts);
const hydratePlatformBundles = await internalHydrate(opts);
const testingPlatform = await internalTesting(opts);
return [...clientPlatformBundle, ...hydratePlatformBundles, ...testingPlatform, await internalAppData(opts)];
}
async function copyStencilInternalDts(opts: BuildOptions, outputInternalDir: string) {
const declarationsInputDir = join(opts.buildDir, 'declarations');
// copy to @stencil/core/internal
// @stencil/core/internal/index.d.ts
const indexDtsSrcPath = join(declarationsInputDir, 'index.d.ts');
const indexDtsDestPath = join(outputInternalDir, 'index.d.ts');
let indexDts = cleanDts(await fs.readFile(indexDtsSrcPath, 'utf8'));
indexDts = prependExtModules(indexDts);
await fs.writeFile(indexDtsDestPath, indexDts);
// @stencil/core/internal/stencil-private.d.ts
const privateDtsSrcPath = join(declarationsInputDir, 'stencil-private.d.ts');
const privateDtsDestPath = join(outputInternalDir, 'stencil-private.d.ts');
const privateDts = cleanDts(await fs.readFile(privateDtsSrcPath, 'utf8'));
await fs.writeFile(privateDtsDestPath, privateDts);
// @stencil/core/internal/stencil-public.compiler.d.ts
const compilerDtsSrcPath = join(declarationsInputDir, 'stencil-public-compiler.d.ts');
const compilerDtsDestPath = join(outputInternalDir, 'stencil-public-compiler.d.ts');
const compilerDts = cleanDts(await fs.readFile(compilerDtsSrcPath, 'utf8'));
await fs.writeFile(compilerDtsDestPath, compilerDts);
// @stencil/core/internal/stencil-public-docs.d.ts
const docsDtsSrcPath = join(declarationsInputDir, 'stencil-public-docs.d.ts');
const docsDtsDestPath = join(outputInternalDir, 'stencil-public-docs.d.ts');
const docsDts = cleanDts(await fs.readFile(docsDtsSrcPath, 'utf8'));
await fs.writeFile(docsDtsDestPath, docsDts);
// @stencil/core/internal/stencil-public-runtime.d.ts
const runtimeDtsSrcPath = join(declarationsInputDir, 'stencil-public-runtime.d.ts');
const runtimeDtsDestPath = join(outputInternalDir, 'stencil-public-runtime.d.ts');
const runtimeDts = cleanDts(await fs.readFile(runtimeDtsSrcPath, 'utf8'));
await fs.writeFile(runtimeDtsDestPath, runtimeDts);
// @stencil/core/internal/stencil-ext-modules.d.ts (.svg/.css)
const srcExtModuleOutput = join(opts.srcDir, 'declarations', 'stencil-ext-modules.d.ts');
const dstExtModuleOutput = join(outputInternalDir, 'stencil-ext-modules.d.ts');
await fs.copyFile(srcExtModuleOutput, dstExtModuleOutput);
}
function prependExtModules(content: string) {
return `/// <reference path="./stencil-ext-modules.d.ts" />\n` + content;
}
async function copyStencilCoreEntry(opts: BuildOptions) {
// write @stencil/core entry
const stencilCoreSrcDir = join(opts.srcDir, 'internal', 'stencil-core');
const stencilCoreDstDir = join(opts.output.internalDir, 'stencil-core');
await fs.ensureDir(stencilCoreDstDir);
await fs.copy(stencilCoreSrcDir, stencilCoreDstDir);
}