-
Notifications
You must be signed in to change notification settings - Fork 766
/
rollup-bundle.ts
96 lines (83 loc) · 3.08 KB
/
rollup-bundle.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
import { BuildCtx, CompilerCtx, Config, EntryModule, JSModuleList } from '../../declarations';
import bundleEntryFile from './rollup-plugins/bundle-entry-file';
import bundleJson from './rollup-plugins/json';
import { createOnWarnFn, loadRollupDiagnostics } from '../../util/logger/logger-rollup';
import { generatePreamble, hasError } from '../util';
import { getBundleIdPlaceholder } from '../../util/data-serialize';
import localResolution from './rollup-plugins/local-resolution';
import inMemoryFsRead from './rollup-plugins/in-memory-fs-read';
import { InputOptions, OutputChunk, rollup } from 'rollup';
import nodeEnvVars from './rollup-plugins/node-env-vars';
import pathsResolution from './rollup-plugins/paths-resolution';
export async function createBundle(config: Config, compilerCtx: CompilerCtx, buildCtx: BuildCtx, entryModules: EntryModule[]) {
const builtins = require('rollup-plugin-node-builtins');
const globals = require('rollup-plugin-node-globals');
let rollupBundle: OutputChunk;
const rollupConfig: InputOptions = {
input: entryModules.map(b => b.entryKey),
experimentalCodeSplitting: true,
preserveSymlinks: false,
plugins: [
config.sys.rollup.plugins.nodeResolve({
jsnext: true,
main: true
}),
config.sys.rollup.plugins.commonjs({
include: 'node_modules/**',
sourceMap: false
}),
bundleJson(config),
globals(),
builtins(),
bundleEntryFile(config, entryModules),
inMemoryFsRead(config, compilerCtx),
await pathsResolution(config, compilerCtx),
localResolution(config, compilerCtx),
nodeEnvVars(config),
...config.plugins
],
onwarn: createOnWarnFn(config, buildCtx.diagnostics)
};
try {
rollupBundle = await rollup(rollupConfig);
} catch (err) {
console.log(err);
loadRollupDiagnostics(config, compilerCtx, buildCtx, err);
}
if (hasError(buildCtx.diagnostics) || !rollupBundle) {
throw new Error('rollup died');
}
return rollupBundle;
}
export async function writeEsModules(config: Config, rollupBundle: OutputChunk) {
const results = await rollupBundle.generate({
format: 'es',
banner: generatePreamble(config),
intro: `const { h, Context } = window.${config.namespace};`,
});
return <any>results as JSModuleList;
}
export async function writeLegacyModules(config: Config, rollupBundle: OutputChunk, entryModules: EntryModule[]) {
const { chunks } = <any>rollupBundle;
Object.keys(chunks).map(key => {
return [key, chunks[key]];
}).forEach(([key, value]) => {
const entryModule = entryModules.find(b => b.entryKey === `./${key}.js`);
if (entryModule) {
entryModule.dependencies = (<any>value).imports.slice();
}
});
const results = await rollupBundle.generate({
format: 'amd',
amd: {
id: getBundleIdPlaceholder(),
define: `${config.namespace}.loadBundle`
},
banner: generatePreamble(config),
intro:
`const h = window.${config.namespace}.h;
const Context = window.${config.namespace}.Context;`,
strict: false,
});
return <any>results as JSModuleList;
}