-
Notifications
You must be signed in to change notification settings - Fork 382
/
bundler.ts
executable file
·161 lines (133 loc) · 4.68 KB
/
bundler.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
/*
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: MIT
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT
*/
import { rollup, Plugin, RollupWarning } from 'rollup';
import {
CompilerError,
CompilerDiagnostic,
generateCompilerDiagnostic,
DiagnosticLevel,
ModuleResolutionErrors,
normalizeToDiagnostic,
} from '@lwc/errors';
import rollupModuleResolver from '../rollup-plugins/module-resolver';
import rollupEnvReplacement from '../rollup-plugins/env-replacement';
import rollupTransform from '../rollup-plugins/transform';
import rollupCompat from '../rollup-plugins/compat';
import rollupMinify from '../rollup-plugins/minify';
import { NormalizedCompileOptions, validateNormalizedCompileOptions } from '../options';
import { SourceMap } from '../compiler/compiler';
export interface BundleReport {
code: string;
diagnostics: CompilerDiagnostic[];
map: SourceMap | null;
}
const DEFAULT_FORMAT = 'amd';
function handleRollupWarning(diagnostics: CompilerDiagnostic[]) {
return function onwarn(warning: string | RollupWarning) {
let message;
let origin = {};
if (typeof warning === 'string') {
message = warning;
} else {
message = warning.message;
if (warning.loc) {
const { loc, pos } = warning;
origin = {
filename: loc.file,
location: {
line: loc.line,
column: loc.column,
start: pos,
length: 0,
},
};
}
}
diagnostics.push(
generateCompilerDiagnostic(ModuleResolutionErrors.MODULE_RESOLUTION_ERROR, {
messageArgs: [message],
origin,
})
);
};
}
export async function bundle(options: NormalizedCompileOptions): Promise<BundleReport> {
validateNormalizedCompileOptions(options);
const { outputConfig, name, namespace } = options;
// TODO [#1268]: remove format option once tests are converted to 'amd' format
const format = (outputConfig as any).format || DEFAULT_FORMAT;
const diagnostics: CompilerDiagnostic[] = [];
const plugins: Plugin[] = [
rollupModuleResolver({
options,
}),
];
// Run environment variable replacement first. This ensures that the source code is still untouched
// at this point.
if (Object.keys(outputConfig.env).length) {
plugins.push(
rollupEnvReplacement({
options,
})
);
}
plugins.push(
rollupTransform({
options,
})
);
if (outputConfig.compat) {
plugins.push(rollupCompat(outputConfig));
}
if (outputConfig.minify) {
plugins.push(rollupMinify(outputConfig));
}
let code: string = '';
let map: SourceMap | null = null;
try {
const rollupBundler = await rollup({
input: name,
plugins,
onwarn: handleRollupWarning(diagnostics),
});
const { output } = await rollupBundler.generate({
format,
amd: { id: namespace + '/' + name },
sourcemap: outputConfig.sourcemap,
// Adding use strict is unnecessary because all the modules in Core are evaluated
// automatically in strict mode.
strict: false,
// [W-7272006] Force generated modules to use named exports. This prevents running into
// issue when the interop code generated by rollup throws an exception when the exported
// object prototype is null.
exports: 'named',
});
// Rollup produces multiple chunks when a module uses "import()" with a relative import
// path. We need to ensure the compiled module only contains the main chunk.
if (output.length > 1) {
diagnostics.push(
generateCompilerDiagnostic(ModuleResolutionErrors.RELATIVE_DYNAMIC_IMPORT)
);
}
const result = output[0];
code = result.code;
map = result.map;
} catch (e) {
// Rollup may have clobbered error.code with its own data
if (e instanceof CompilerError && (e as any).pluginCode) {
e.code = (e as any).pluginCode;
}
const diagnostic = normalizeToDiagnostic(ModuleResolutionErrors.MODULE_RESOLUTION_ERROR, e);
diagnostic.level = DiagnosticLevel.Fatal;
diagnostics.push(diagnostic);
}
return {
diagnostics,
code,
map,
};
}