-
Notifications
You must be signed in to change notification settings - Fork 922
/
plugin.js
60 lines (58 loc) · 2.14 KB
/
plugin.js
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
const {isTestFilePath} = require('@web/test-runner');
const snowpack = require('snowpack');
const path = require('path');
module.exports = function () {
if (process.env.NODE_ENV !== 'test') {
throw new Error(`@snowpack/web-test-runner-plugin: NODE_ENV must === "test" to build files correctly.
To Resolve:
1. Set "process.env.NODE_ENV = 'test';" at the top of your web-test-runner.config.js file (before all imports).
2. Prefix your web-test-runner CLI command: "NODE_ENV=test web-test-runner ...".
`);
}
let server, config;
return {
name: 'snowpack-plugin',
async serverStart({fileWatcher}) {
config = await snowpack.loadConfiguration({
packageOptions: {external: ['/__web-dev-server__web-socket.js']},
devOptions: {open: 'none', output: 'stream', hmr: false},
});
// npm packages should be installed/prepared ahead of time.
console.log('[snowpack] preparing npm packages...');
await snowpack.preparePackages({config, lockfile: null});
console.log('[snowpack] starting server...');
fileWatcher.add(Object.keys(config.mount));
server = await snowpack.startServer({
config,
lockfile: null,
});
},
async serverStop() {
return server.shutdown();
},
async serve({request}) {
if (request.url.startsWith('/__web-dev-server')) {
return;
}
const reqPath = request.path;
const result = await server.loadUrl(reqPath, {isSSR: false});
return {body: result.contents, type: result.contentType};
},
transformImport({source}) {
if (!isTestFilePath(source) || source.startsWith('/__web-dev-server')) {
return;
}
// PERF(fks): https://github.com/snowpackjs/snowpack/pull/1259/files#r502963818
const reqPath = source.substring(
0,
source.indexOf('?') === -1 ? undefined : source.indexOf('?'),
);
const sourcePath = path.join(config.root || process.cwd(), reqPath);
const mountedUrl = snowpack.getUrlForFile(sourcePath, config);
if (!mountedUrl) {
throw new Error(`${source} could not be mounted!`);
}
return mountedUrl;
},
};
};