-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathserve-integration.js
More file actions
127 lines (111 loc) · 4.55 KB
/
serve-integration.js
File metadata and controls
127 lines (111 loc) · 4.55 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
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
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { rollup } from 'rollup';
import lwcRollupPlugin from '@lwc/rollup-plugin';
import {
API_VERSION,
COVERAGE,
DISABLE_STATIC_CONTENT_OPTIMIZATION,
DISABLE_SYNTHETIC_SHADOW_SUPPORT_IN_COMPILER,
} from '../../helpers/options.js';
/** Cache reused between each compilation to speed up the compilation time. */
let cache;
const createRollupPlugin = (input, options) => {
const suiteDir = path.dirname(input);
// TODO [#3370]: remove experimental template expression flag
const experimentalComplexExpressions = suiteDir.includes('template-expressions');
return lwcRollupPlugin({
// Sourcemaps don't work with Istanbul coverage
sourcemap: !process.env.COVERAGE,
experimentalDynamicComponent: {
loader: fileURLToPath(new URL('../../helpers/dynamic-loader', import.meta.url)),
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 transform = async (ctx) => {
const input = ctx.path.slice(1); // strip leading / from URL path to get relative file path
const defaultRollupPlugin = createRollupPlugin(input);
const customLwcRollupPlugin = {
...defaultRollupPlugin,
transform(src, id) {
let transform;
// 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) {
// The original Karma tests only ever had filename-based config for API version 60.
// Filename-based config is a pattern we want to move away from, so this transform
// only works for that version, so that we could simplify the logic here.
if (apiVersion !== '60') {
throw new Error(
'TODO: fully implement or remove support for filename-based API version'
);
}
transform = createRollupPlugin(input, {
apiVersion: 60,
}).transform;
} else if (nativeOnly) {
transform = createRollupPlugin(input, {
disableSyntheticShadowSupport: true,
}).transform;
} else {
transform = defaultRollupPlugin.transform;
}
return 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/loader',
// Some helper files export functions that mutate a global state. The setup file calls
// some of those functions and does not get bundled. Including the helper files in the
// bundle would create a separate global state, causing tests to fail. We don't need to
// mark _all_ helpers as external, but we do anyway for ease of maintenance.
/\/helpers\/\w+\.js$/,
],
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',
// FIXME: Does web-test-runner use istanbul?
// Sourcemaps don't work with Istanbul coverage
sourcemap: COVERAGE ? false : 'inline',
});
return output[0].code;
};
/** @type {import('@web/dev-server-core').Plugin} */
export default {
async serve(ctx) {
if (ctx.path.endsWith('.spec.js')) {
return await transform(ctx);
}
},
};