-
Notifications
You must be signed in to change notification settings - Fork 438
Expand file tree
/
Copy pathlwc.js
More file actions
125 lines (99 loc) · 4.24 KB
/
lwc.js
File metadata and controls
125 lines (99 loc) · 4.24 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
/*
* 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
*/
/**
* This transformation is inspired from the karma-rollup-transform:
* https://github.com/jlmakes/karma-rollup-preprocessor/blob/master/lib/index.js
*/
'use strict';
const path = require('path');
const { rollup } = require('rollup');
const lwcRollupPlugin = require('@lwc/rollup-plugin');
const compatRollupPlugin = require('rollup-plugin-compat');
const { COMPAT, DISABLE_SYNTHETIC_SHADOW_SUPPORT_IN_COMPILER } = require('../shared/options');
const Watcher = require('./Watcher');
function createPreprocessor(config, emitter, logger) {
const { basePath } = config;
const log = logger.create('preprocessor-lwc');
const watcher = new Watcher(config, emitter, log);
// Cache reused between each compilation to speed up the compilation time.
let cache;
return async (_content, file, done) => {
const input = file.path;
const suiteDir = path.dirname(input);
// Wrap all the tests into a describe block with the file stricture name
const ancestorDirectories = path.relative(basePath, suiteDir).split(path.sep);
const intro = ancestorDirectories
.map((tag) => `describe("${tag}", function () {`)
.join('\n');
const outro = ancestorDirectories.map(() => `});`).join('\n');
const plugins = [
lwcRollupPlugin({
sourcemap: true,
dynamicImportConfig: {
loader: 'test-utils',
strict: true,
},
enableLwcSpread: true,
enableScopedSlots: true,
disableSyntheticShadowSupport: DISABLE_SYNTHETIC_SHADOW_SUPPORT_IN_COMPILER,
}),
];
if (COMPAT) {
plugins.push(
compatRollupPlugin({
// The compat polyfills are injected at runtime by Karma, polyfills can be shared between all the
// suites.
polyfills: false,
})
);
}
try {
const bundle = await rollup({
input,
plugins,
cache,
// 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);
}
},
});
watcher.watchSuite(input, bundle.watchFiles);
// eslint-disable-next-line require-atomic-updates
cache = bundle.cache;
const { output } = await bundle.generate({
format: 'iife',
sourcemap: 'inline',
// The engine and the test-utils is injected as UMD. This mapping defines how those modules can be
// referenced from the window object.
globals: {
lwc: 'LWC',
'wire-service': 'WireService',
'test-utils': 'TestUtils',
},
intro,
outro,
});
const { code, map } = output[0];
// We need to assign the source to the original file so Karma can source map the error in the console. Add
// also adding the source map inline for browser debugging.
// eslint-disable-next-line require-atomic-updates
file.sourceMap = map;
done(null, code);
} catch (error) {
const location = path.relative(basePath, file.path);
log.error('Error processing “%s”\n\n%s\n', location, error.stack || error.message);
done(error, null);
}
};
}
createPreprocessor.$inject = ['config', 'emitter', 'logger'];
module.exports = { 'preprocessor:lwc': ['factory', createPreprocessor] };