-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
72 lines (55 loc) · 1.68 KB
/
index.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
const rollup = require('rollup');
const dts = require('rollup-plugin-dts').default;
const fs = require('fs');
async function build(inputOptions, outputOptions) {
const bundle = await rollup.rollup(inputOptions);
await bundle.write(outputOptions);
await bundle.close();
}
async function watch(inputOptions, outputOptions) {
const watchOptions = {
...inputOptions,
output: [outputOptions],
watch: {
}
};
const watcher = rollup.watch(watchOptions);
watcher.on('event', event => {
});
// This will make sure that bundles are properly closed after each run
watcher.on('event', ({ result }) => {
if (result) {
result.close();
wrapWithModule(outputOptions.file);
}
});
// stop watching
// watcher.close();
}
function wrapWithModule(outputFile) {
const file = fs.readFileSync(outputFile).toString();
const noDeclare = file.split('declare ').join('');
const packageName = JSON.parse(fs.readFileSync('./package.json').toString()).name;
const withDeclaration = `declare module '${packageName}' {\n${ noDeclare.split('\n').join('\n ')}\n}\n`;
fs.writeFileSync(outputFile, withDeclaration);
}
async function cli(args) {
const inputFile = args[2];
const outputFile = args[3];
const outputOptions = {
file: outputFile,
format: 'es'
};
const inputOptions = {
input: inputFile,
plugins: [dts()],
};
console.debug(args);
if(args.includes('-w')){
watch(inputOptions, outputOptions);
} else {
await build(inputOptions, outputOptions);
wrapWithModule(outputFile);
}
}
module.exports = {cli};