-
Notifications
You must be signed in to change notification settings - Fork 393
/
index.ts
358 lines (319 loc) · 15.6 KB
/
index.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Copyright (c) 2024, Salesforce, 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 fs from 'fs';
import path from 'path';
import { URLSearchParams } from 'url';
import { Plugin, SourceMapInput, RollupLog } from 'rollup';
import pluginUtils, { FilterPattern } from '@rollup/pluginutils';
import { transformSync, StylesheetConfig, DynamicImportConfig } from '@lwc/compiler';
import { resolveModule, ModuleRecord, RegistryType } from '@lwc/module-resolver';
import { APIVersion, getAPIVersionFromNumber } from '@lwc/shared';
import type { CompilerDiagnostic } from '@lwc/errors';
export interface RollupLwcOptions {
/** A boolean indicating whether to compile for SSR runtime target. */
targetSSR?: boolean;
/** A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should transform on. By default all files are targeted. */
include?: FilterPattern;
/** A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should not transform. By default no files are ignored. */
exclude?: FilterPattern;
/** The LWC root module directory. */
rootDir?: string;
/** If `true` the plugin will produce source maps. If `'inline'`, the plugin will produce inlined source maps and append them to the end of the generated file. */
sourcemap?: boolean | 'inline';
/** The [module resolution](https://lwc.dev/guide/es_modules#module-resolution) overrides passed to the `@lwc/module-resolver`. */
modules?: ModuleRecord[];
/** The stylesheet compiler configuration to pass to the `@lwc/style-compiler` */
stylesheetConfig?: StylesheetConfig;
/** The configuration to pass to the `@lwc/template-compiler`. */
preserveHtmlComments?: boolean;
/** The configuration to pass to `@lwc/compiler`. */
experimentalDynamicComponent?: DynamicImportConfig;
/** The configuration to pass to `@lwc/template-compiler`. */
experimentalDynamicDirective?: boolean;
/** The configuration to pass to `@lwc/template-compiler`. */
enableDynamicComponents?: boolean;
/** The configuration to pass to `@lwc/compiler`. */
enableLightningWebSecurityTransforms?: boolean;
// TODO [#3370]: remove experimental template expression flag
/** The configuration to pass to `@lwc/template-compiler`. */
experimentalComplexExpressions?: boolean;
/** @deprecated Spread operator is now always enabled. */
enableLwcSpread?: boolean;
/** The configuration to pass to `@lwc/compiler` to disable synthetic shadow support */
disableSyntheticShadowSupport?: boolean;
/** The API version to associate with the compiled module */
apiVersion?: APIVersion;
/** True if the static content optimization should be enabled. Defaults to true */
enableStaticContentOptimization?: boolean;
}
const PLUGIN_NAME = 'rollup-plugin-lwc-compiler';
const DEFAULT_MODULES = [
{ npm: '@lwc/engine-dom' },
{ npm: '@lwc/synthetic-shadow' },
{ npm: '@lwc/wire-service' },
];
const IMPLICIT_DEFAULT_HTML_PATH = '@lwc/resources/empty_html.js';
const EMPTY_IMPLICIT_HTML_CONTENT = 'export default void 0';
const IMPLICIT_DEFAULT_CSS_PATH = '@lwc/resources/empty_css.css';
const EMPTY_IMPLICIT_CSS_CONTENT = '';
function isImplicitHTMLImport(importee: string, importer: string): boolean {
return (
path.extname(importer) === '.js' &&
path.extname(importee) === '.html' &&
path.dirname(importer) === path.dirname(importee) &&
path.basename(importer, '.js') === path.basename(importee, '.html')
);
}
function isImplicitCssImport(importee: string, importer: string): boolean {
return (
path.extname(importee) === '.css' &&
path.extname(importer) === '.html' &&
(path.basename(importee, '.css') === path.basename(importer, '.html') ||
path.basename(importee, '.scoped.css') === path.basename(importer, '.html'))
);
}
interface Descriptor {
filename: string;
scoped: boolean;
specifier: string | null;
}
function parseDescriptorFromFilePath(id: string): Descriptor {
const [filename, query] = id.split('?', 2);
const params = new URLSearchParams(query);
const scoped = params.has('scoped');
const specifier = params.get('specifier');
return {
filename,
specifier,
scoped,
};
}
function appendAliasSpecifierQueryParam(id: string, specifier: string): string {
const [filename, query] = id.split('?', 2);
const params = new URLSearchParams(query);
params.set('specifier', specifier);
return `${filename}?${params.toString()}`;
}
function transformWarningToRollupLog(
warning: CompilerDiagnostic,
src: string,
id: string
): RollupLog {
// For reference on RollupLogs (f.k.a. RollupWarnings), a good example is:
// https://github.com/rollup/plugins/blob/53776ee/packages/typescript/src/diagnostics/toWarning.ts
const pluginCode = `LWC${warning.code}`; // modeled after TypeScript, e.g. TS5055
const result: RollupLog = {
// Replace any newlines in case they exist, just so the Rollup output looks a bit cleaner
message: `@lwc/rollup-plugin: ${warning.message?.replace(/\n/g, ' ')}`,
plugin: PLUGIN_NAME,
pluginCode,
};
const { location } = warning;
if (location) {
result.loc = {
// The CompilerDiagnostic from @lwc/template-compiler reports an undefined filename, because it loses the
// filename context here:
// https://github.com/salesforce/lwc/blob/e2bc36f/packages/%40lwc/compiler/src/transformers/template.ts#L35-L38
file: id,
// For LWC, the column is 0-based and the line is 1-based. Rollup just reports this for informational
// purposes, though; it doesn't seem to matter what we put here.
column: location.column,
line: location.line,
};
// To get a fancier output like @rollup/plugin-typescript's, we would need to basically do our
// own color coding on the entire line, adding the wavy lines to indicate an error, etc. You can see how
// TypeScript does it here: https://github.com/microsoft/TypeScript/blob/1a4643b/src/compiler/program.ts#L453-L485
// Outputting just the string that caused the error is good enough for now though.
if (typeof location.start === 'number' && typeof location.length === 'number') {
result.frame = src.substring(location.start, location.start + location.length);
}
}
return result;
}
/**
* Rollup plugin for bundling LWC components
* @param pluginOptions LWC rollup plugin options
* @returns LWC rollup plugin
*/
export default function lwc(pluginOptions: RollupLwcOptions = {}): Plugin {
const filter = pluginUtils.createFilter(pluginOptions.include, pluginOptions.exclude);
let { rootDir, modules = [] } = pluginOptions;
const {
targetSSR,
stylesheetConfig,
sourcemap = false,
preserveHtmlComments,
experimentalDynamicComponent,
experimentalDynamicDirective,
enableDynamicComponents,
enableLightningWebSecurityTransforms,
// TODO [#3370]: remove experimental template expression flag
experimentalComplexExpressions,
disableSyntheticShadowSupport,
apiVersion,
} = pluginOptions;
return {
name: PLUGIN_NAME,
buildStart({ input }) {
if (rootDir === undefined) {
if (Array.isArray(input)) {
rootDir = path.dirname(path.resolve(input[0]));
if (input.length > 1) {
this.warn(
`The "rootDir" option should be explicitly set when passing an "input" array to rollup. The "rootDir" option is implicitly resolved to ${rootDir}.`
);
}
} else {
rootDir = path.dirname(path.resolve(Object.values(input)[0]));
this.warn(
`The "rootDir" option should be explicitly set when passing "input" object to rollup. The "rootDir" option is implicitly resolved to ${rootDir}.`
);
}
} else {
rootDir = path.resolve(rootDir);
}
modules = [...modules, ...DEFAULT_MODULES, { dir: rootDir }];
},
resolveId(importee, importer) {
if (importer) {
// Importer has been resolved already and may contain an alias specifier
const { filename: importerFilename } = parseDescriptorFromFilePath(importer);
// Normalize relative import to absolute import
// Note that in @rollup/plugin-node-resolve v13, relative imports will sometimes
// be in absolute format (e.g. "/path/to/module.js") so we have to check that as well.
if (importee.startsWith('.') || importee.startsWith('/')) {
const importerExt = path.extname(importerFilename);
// if importee has query params importeeExt will store them.
// ex: if scoped.css?scoped=true, importeeExt = .css?scoped=true
const importeeExt = path.extname(importee) || importerExt;
const importeeResolvedPath = path.resolve(
path.dirname(importerFilename),
importee
);
// importeeAbsPath will contain query params because they are attached to importeeExt.
// ex: myfile.scoped.css?scoped=true
const importeeAbsPath = pluginUtils.addExtension(
importeeResolvedPath,
importeeExt
);
// remove query params
const { filename: importeeNormalizedFilename } =
parseDescriptorFromFilePath(importeeAbsPath);
if (
isImplicitHTMLImport(importeeNormalizedFilename, importerFilename) &&
!fs.existsSync(importeeNormalizedFilename)
) {
return IMPLICIT_DEFAULT_HTML_PATH;
}
if (
isImplicitCssImport(importeeNormalizedFilename, importerFilename) &&
!fs.existsSync(importeeNormalizedFilename)
) {
return IMPLICIT_DEFAULT_CSS_PATH;
}
return importeeAbsPath;
} else {
// Could be an import like `import component from 'x/component'`
try {
const { entry, specifier, type } = resolveModule(importee, importer, {
modules,
rootDir,
});
if (type === RegistryType.alias) {
// specifier must be in in namespace/name format
const [namespace, name, ...rest] = specifier.split('/');
// Alias specifier must have been in the namespace / name format
// to be used as the tag name of a custom element.
// Verify 3 things about the alias specifier:
// 1. The namespace is a non-empty string
// 2. The name is an non-empty string
// 3. The specifier was in a namespace / name format, ie no extra '/' (this is what rest checks)
const hasValidSpecifier =
!!namespace?.length && !!name?.length && !rest?.length;
if (hasValidSpecifier) {
return appendAliasSpecifierQueryParam(entry, specifier);
}
}
return entry;
} catch (err: any) {
if (err && err.code !== 'NO_LWC_MODULE_FOUND') {
throw err;
}
}
}
}
},
load(id) {
if (id === IMPLICIT_DEFAULT_HTML_PATH) {
return EMPTY_IMPLICIT_HTML_CONTENT;
}
if (id === IMPLICIT_DEFAULT_CSS_PATH) {
return EMPTY_IMPLICIT_CSS_CONTENT;
}
// Have to parse the `?scoped=true` in `load`, because it's not guaranteed
// that `resolveId` will always be called (e.g. if another plugin resolves it first)
const { filename, specifier: hasAlias } = parseDescriptorFromFilePath(id);
const isCSS = path.extname(filename) === '.css';
if (isCSS || hasAlias) {
const exists = fs.existsSync(filename);
if (exists) {
return fs.readFileSync(filename, 'utf8');
} else if (isCSS) {
this.warn(
`The imported CSS file ${filename} does not exist: Importing it as undefined. ` +
`This behavior may be removed in a future version of LWC. Please avoid importing a ` +
`CSS file that does not exist.`
);
return EMPTY_IMPLICIT_CSS_CONTENT;
}
}
},
transform(src, id) {
const { scoped, filename, specifier } = parseDescriptorFromFilePath(id);
// Filter user-land config and lwc import
if (!filter(filename)) {
return;
}
// Extract module name and namespace from file path.
// Specifier will only exist for modules with alias paths.
// Otherwise, use the file directory structure to resolve namespace and name.
const [namespace, name] =
specifier?.split('/') ?? path.dirname(filename).split(path.sep).slice(-2);
const apiVersionToUse = getAPIVersionFromNumber(apiVersion);
const { code, map, warnings } = transformSync(src, filename, {
name,
namespace,
outputConfig: { sourcemap },
stylesheetConfig,
experimentalDynamicComponent,
experimentalDynamicDirective,
enableDynamicComponents,
enableLightningWebSecurityTransforms,
// TODO [#3370]: remove experimental template expression flag
experimentalComplexExpressions,
preserveHtmlComments,
scopedStyles: scoped,
disableSyntheticShadowSupport,
apiVersion: apiVersionToUse,
// Only pass this in if it's actually specified – otherwise unspecified becomes undefined becomes false
...('enableStaticContentOptimization' in pluginOptions && {
enableStaticContentOptimization: pluginOptions.enableStaticContentOptimization,
}),
targetSSR,
});
if (warnings) {
for (const warning of warnings) {
this.warn(transformWarningToRollupLog(warning, src, filename));
}
}
const rollupMap = map as SourceMapInput;
return { code, map: rollupMap };
},
};
}
// For backward compatibility with commonjs format
module.exports = lwc;