/
inlined-compiler-deps-plugin.ts
60 lines (54 loc) · 1.46 KB
/
inlined-compiler-deps-plugin.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
import fs from 'fs-extra';
import { join } from 'path';
import { rollup, Plugin } from 'rollup';
import rollupCommonjs from '@rollup/plugin-commonjs';
import rollupJson from '@rollup/plugin-json';
import rollupNodeResolve from '@rollup/plugin-node-resolve';
import type { BuildOptions } from '../../utils/options';
export function inlinedCompilerDepsPlugin(opts: BuildOptions, inputDir: string): Plugin {
return {
name: 'inlinedCompilerDepsPlugin',
resolveId(id) {
if (id === '@compiler-deps') {
return id;
}
return null;
},
load(id) {
if (id === '@compiler-deps') {
return bundleCompilerDeps(opts, inputDir);
}
return null;
},
};
}
async function bundleCompilerDeps(opts: BuildOptions, inputDir: string) {
const cacheFile = join(opts.buildDir, 'compiler-deps-bundle-cache.js');
if (!opts.isProd) {
try {
return await fs.readFile(cacheFile, 'utf8');
} catch (e) {}
}
const build = await rollup({
input: join(inputDir, 'sys', 'modules', 'compiler-deps.js'),
external: ['fs', 'module', 'path', 'util', 'resolve'],
plugins: [
rollupNodeResolve({
preferBuiltins: false,
}),
rollupCommonjs(),
rollupJson({
preferConst: true,
}),
],
treeshake: {
moduleSideEffects: false,
},
});
await build.write({
format: 'es',
file: cacheFile,
preferConst: true,
});
return await fs.readFile(cacheFile, 'utf8');
}