-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathlwc.mjs
More file actions
97 lines (83 loc) · 3.39 KB
/
lwc.mjs
File metadata and controls
97 lines (83 loc) · 3.39 KB
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
import path from 'node:path';
import { rollup } from 'rollup';
import lwcRollupPlugin from '@lwc/rollup-plugin';
import {
DISABLE_SYNTHETIC_SHADOW_SUPPORT_IN_COMPILER,
API_VERSION,
DISABLE_STATIC_CONTENT_OPTIMIZATION,
} from './options.mjs';
// Cache reused between each compilation to speed up the compilation time.
let cache;
export default async (ctx) => {
const input = ctx.path.slice(1); // strip leading / from URL path to get relative file path
const suiteDir = path.dirname(input);
// TODO [#3370]: remove experimental template expression flag
const experimentalComplexExpressions = suiteDir.includes('template-expressions');
const createRollupPlugin = (options) => {
return lwcRollupPlugin({
// Sourcemaps don't work with Istanbul coverage
sourcemap: !process.env.COVERAGE,
experimentalDynamicComponent: {
loader: 'test-utils',
strict: true,
},
enableDynamicComponents: true,
enableLwcOn: true,
experimentalComplexExpressions,
enableStaticContentOptimization: !DISABLE_STATIC_CONTENT_OPTIMIZATION,
disableSyntheticShadowSupport: DISABLE_SYNTHETIC_SHADOW_SUPPORT_IN_COMPILER,
apiVersion: API_VERSION,
modules: [
{
// Assume `ctx.path` is a component file, e.g. modules/x/foo/foo.js
dir: path.resolve(input, '../../..'),
},
],
...options,
});
};
const defaultRollupPlugin = createRollupPlugin();
const customLwcRollupPlugin = {
...defaultRollupPlugin,
transform(src, id) {
let rollupPluginToUse;
// Override the LWC Rollup plugin to specify different options based on file name patterns.
// This allows us to alter the API version or other compiler props on a filename-only basis.
const apiVersion = id.match(/useApiVersion(\d+)/)?.[1];
const nativeOnly = /\.native-only\./.test(id);
if (apiVersion) {
rollupPluginToUse = createRollupPlugin({
apiVersion: parseInt(apiVersion, 10),
});
} else if (nativeOnly) {
rollupPluginToUse = createRollupPlugin({ disableSyntheticShadowSupport: true });
} else {
rollupPluginToUse = defaultRollupPlugin;
}
return rollupPluginToUse.transform.call(this, src, id);
},
};
const bundle = await rollup({
input,
cache,
plugins: [customLwcRollupPlugin],
// Rollup should not attempt to resolve the engine and the test utils, Karma takes care of injecting it
// globally in the page before running the tests.
external: ['lwc', 'wire-service', 'test-utils', '@test/loader'],
onwarn(warning, warn) {
// Ignore warnings from our own Rollup plugin
if (warning.plugin !== 'rollup-plugin-lwc-compiler') {
warn(warning);
}
},
});
cache = bundle.cache;
const { output } = await bundle.generate({
format: 'esm',
// TODO: Does web-test-runner use istanbul?
// Sourcemaps don't work with Istanbul coverage
sourcemap: process.env.COVERAGE ? false : 'inline',
});
const { code } = output[0];
return code;
};