/
internal-platform-client.ts
173 lines (153 loc) · 5.39 KB
/
internal-platform-client.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import fs from 'fs-extra';
import { basename, join } from 'path';
import type { BuildOptions } from '../utils/options';
import { aliasPlugin } from './plugins/alias-plugin';
import { replacePlugin } from './plugins/replace-plugin';
import { reorderCoreStatementsPlugin } from './plugins/reorder-statements';
import { getBanner } from '../utils/banner';
import { writePkgJson } from '../utils/write-pkg-json';
import { rollup, RollupOptions } from 'rollup';
import glob from 'glob';
import ts from 'typescript';
import { minify } from 'terser';
export async function internalClient(opts: BuildOptions) {
const inputClientDir = join(opts.buildDir, 'client');
const outputInternalClientDir = join(opts.output.internalDir, 'client');
const outputInternalClientPolyfillsDir = join(outputInternalClientDir, 'polyfills');
await fs.emptyDir(outputInternalClientDir);
await fs.emptyDir(outputInternalClientPolyfillsDir);
await copyPolyfills(opts, outputInternalClientPolyfillsDir);
// write @stencil/core/internal/client/package.json
writePkgJson(opts, outputInternalClientDir, {
name: '@stencil/core/internal/client',
description: 'Stencil internal client platform to be imported by the Stencil Compiler and internal runtime. Breaking changes can and will happen at any time.',
main: 'index.js',
});
const internalClientBundle: RollupOptions = {
input: join(inputClientDir, 'index.js'),
output: {
format: 'es',
dir: outputInternalClientDir,
entryFileNames: '[name].js',
chunkFileNames: '[name].js',
banner: getBanner(opts, 'Stencil Client Platform'),
preferConst: true,
},
treeshake: {
moduleSideEffects: 'no-external',
propertyReadSideEffects: false,
},
plugins: [
{
name: 'internalClientPlugin',
resolveId(importee) {
if (importee === '@platform') {
return join(inputClientDir, 'index.js');
}
},
},
aliasPlugin(opts),
replacePlugin(opts),
reorderCoreStatementsPlugin(),
],
};
const internalClientPatchBrowserBundle: RollupOptions = {
input: join(inputClientDir, 'client-patch-browser.js'),
output: {
format: 'es',
dir: outputInternalClientDir,
entryFileNames: 'patch-browser.js',
chunkFileNames: '[name].js',
banner: getBanner(opts, 'Stencil Client Patch Browser'),
preferConst: true,
},
treeshake: {
moduleSideEffects: 'no-external',
propertyReadSideEffects: false,
},
plugins: [
{
name: 'internalClientPatchPlugin',
resolveId(importee) {
if (importee === '@platform') {
return {
id: `@stencil/core`,
external: true,
};
}
},
},
{
name: 'internalClientRuntimeCssShim',
resolveId(importee) {
if (importee === './polyfills/css-shim.js') {
return importee;
}
return null;
},
async load(id) {
// bundle the css-shim into one file
if (id === './polyfills/css-shim.js') {
const rollupBuild = await rollup({
input: join(inputClientDir, 'polyfills', 'css-shim', 'index.js'),
onwarn: message => {
if (/top level of an ES module/.test(message as any)) return;
console.error(message);
},
});
const { output } = await rollupBuild.generate({ format: 'es' });
const transpileToEs5 = ts.transpileModule(output[0].code, {
compilerOptions: {
target: ts.ScriptTarget.ES5,
},
});
let code = transpileToEs5.outputText;
if (opts.isProd) {
const minifyResults = await minify(code);
code = minifyResults.code;
}
const dest = join(outputInternalClientPolyfillsDir, 'css-shim.js');
await fs.writeFile(dest, code);
return code;
}
return null;
},
},
{
name: 'internalClientRuntimePolyfills',
resolveId(importee) {
if (importee.startsWith('./polyfills')) {
const fileName = basename(importee);
return join(opts.srcDir, 'client', 'polyfills', fileName);
}
return null;
},
},
aliasPlugin(opts),
replacePlugin(opts),
reorderCoreStatementsPlugin(),
],
};
const internalClientPatchEsmBundle = { ...internalClientPatchBrowserBundle };
internalClientPatchEsmBundle.input = join(inputClientDir, 'client-patch-esm.js');
internalClientPatchEsmBundle.output = {
format: 'es',
dir: outputInternalClientDir,
entryFileNames: 'patch-esm.js',
chunkFileNames: '[name].js',
banner: getBanner(opts, 'Stencil Client Patch Esm'),
preferConst: true,
};
return [internalClientBundle, internalClientPatchBrowserBundle, internalClientPatchEsmBundle];
}
async function copyPolyfills(opts: BuildOptions, outputInternalClientPolyfillsDir: string) {
const srcPolyfillsDir = join(opts.srcDir, 'client', 'polyfills');
const srcPolyfillFiles = glob.sync('*.js', { cwd: srcPolyfillsDir });
await Promise.all(
srcPolyfillFiles.map(async fileName => {
const src = join(srcPolyfillsDir, fileName);
const dest = join(outputInternalClientPolyfillsDir, fileName);
await fs.copyFile(src, dest);
}),
);
}