-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbin.js
75 lines (68 loc) · 1.72 KB
/
bin.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
#!/usr/bin/env node
const path = require('path');
const minimist = require('minimist');
const chokidar = require('chokidar');
const { performance } = require('perf_hooks');
const readConfig = require('./src/readConfig');
const createConfig = require('./src/createConfig');
const run = require('./src/run');
const args = minimist(process.argv.slice(2), {
alias: {
root: 'r',
config: 'c',
output: 'o',
name: 'n',
script: 's',
file: 'f',
wait: 'w',
build: 'b'
}
});
const config = createConfig(
readConfig(path.resolve(process.cwd(), (args.config || ''))) ||
readConfig(path.resolve(process.cwd(), '.ticbundle.js')) ||
readConfig(path.resolve(process.cwd(), '.ticbundle.json')) ||
{
root: args.root,
wait: typeof args.wait === 'string' && args.wait.length > 0 ?
+args.wait :
undefined,
metadata: {
script: args.script
},
output: {
path: args.output,
extension: args.file,
name: args.name,
}
}
);
const files = [
...config.files.map(file => path.resolve(config.root, file)),
...config.assets.map(file => path.resolve(config.root, file))
];
const bundle = () => {
const ts = performance.now();
run(config);
console.info(`Generated bundle in: ${Math.round(performance.now() - ts)}ms`);
};
if (args.build) {
console.info('[tic-bundle] creating bundle');
bundle();
} else {
chokidar
.watch(files, {
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: config.wait
}
})
.on('ready', () => {
console.group('[tic-bundle] watching files');
files.forEach(file => console.info(file));
console.groupEnd();
bundle();
})
.on('add', bundle)
.on('change', bundle);
}