This repository has been archived by the owner on Apr 8, 2021. It is now read-only.
forked from FredKSchott/snowpack-plugin-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
75 lines (63 loc) · 1.79 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { command, commandSync } = require('execa');
const { env } = require('npm-run-path');
const cwd = process.cwd();
function dataListener(chunk, log) {
let stdOutput = chunk.toString();
// In --watch mode, handle the "clear" character
if (stdOutput.includes('\u001bc') || stdOutput.includes('\x1Bc')) {
log('WORKER_RESET', {});
stdOutput = stdOutput.replace(/\x1Bc/, '').replace(/\u001bc/, '');
}
log('WORKER_MSG', { level: 'log', msg: `${stdOutput}` });
}
function runNgc(args, log) {
const { stdout, stderr } = commandSync(
`ngc ${args || '--project ./tsconfig.app.json'}`,
{
env: env(),
extendEnv: true,
windowsHide: false,
cwd,
}
);
if (stdout && stdout.trim()) dataListener(stdout, log);
if (stderr && stderr.trim()) dataListener(stderr, log);
}
function angularPlugin(_, { args } = {}) {
/**
* @type {import('snowpack').SnowpackPlugin}
*/
const plugin = {
name: 'snowpack-plugin-angular',
async run({ isDev, log }) {
if (isDev) runNgc(args, log);
const workerPromise = command(
`ngc ${args || '--project ./tsconfig.app.json'} ${
isDev ? '--watch' : ''
}`,
{
env: env(),
extendEnv: true,
windowsHide: false,
cwd,
}
);
const { stdout, stderr } = workerPromise;
stdout && stdout.on('data', (chunk) => dataListener(chunk, log));
stderr && stderr.on('data', (chunk) => dataListener(chunk, log));
return workerPromise;
},
transform({ contents, fileExt, isDev }) {
if (isDev || fileExt.trim() !== '.js' || !contents.trim())
return contents;
const {
buildOptimizer,
} = require('@angular-devkit/build-optimizer');
const transpiledContent = buildOptimizer({ content: contents })
.content;
return transpiledContent || contents;
},
};
return plugin;
}
module.exports = angularPlugin;