-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrollup.js
executable file
·141 lines (113 loc) · 3.14 KB
/
rollup.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env node
'use strict'
/**
* Dependencies
*/
const path = require('path');
const rollup = require('rollup');
const chokidar = require('chokidar');
const args = require(`${__dirname}/util/args`).args;
const cnsl = require(`${__dirname}/util/console`);
const resolve = require(`${__dirname}/util/resolve`);
const lint = require(`${__dirname}/lint`);
const alerts = resolve('config/alerts');
const global = resolve('config/global');
/**
* Set options to a function for watching config changes
*
* @return {Object} Containing the script options
*/
const options = () => {
let config = resolve('config/rollup', true, false);
let source = path.join(global.base, global.src);
let ext = '.js';
let modules = config;
/**
* Development Mode
*/
if (process.env.NODE_ENV === 'development') {
modules = modules.filter(file => file.devModule);
}
return {
config: resolve('config/rollup', true, false),
source: source,
ext: ext,
globs: [
path.join(source, `/**/*${ext}`),
resolve('config/rollup', false)
],
modules: modules
}
}
/**
* Our Chokidar Watcher
*
* @type {Source} https://github.com/paulmillr/chokidar
*/
const watcher = chokidar.watch(options().globs, global.chokidar);
/**
* Main script process
*
* @param {Array} script A rollup configuration object https://rollupjs.org/guide/en/#using-config-files
*/
const main = async (script) => {
try {
/** Lint file */
if (!args.nolint) await lint.main(script.input);
if (script.hasOwnProperty('devModule'))
delete script.devModule;
const bundle = await rollup.rollup(script);
for (let i = 0; i < script.output.length; i++) {
await bundle.write(script.output[i]);
cnsl.describe(`${alerts.rollup} Rollup in ${alerts.str.path(script.input)} out ` +
`${alerts.str.path(script.output[i].file)}`);
}
bundle.close();
} catch (err) {
cnsl.error(`Rollup failed (main): ${err.stack}`);
}
};
/**
* Runner for the sample script
*/
const run = async () => {
let opts = options();
if (args.watch) {
try {
watcher.on('change', async changed => {
let scrpts = [];
cnsl.watching(`Detected change on ${alerts.str.path(changed)}`);
opts = options(); // Get fresh options
// Only run main on changed modules by default
if (process.env.NODE_ENV === 'production') {
scrpts = opts.modules;
} else {
let filtered = opts.modules.filter(s => path.basename(changed) === path.basename(s.input));
scrpts = (filtered.length) ? filtered : opts.modules;
}
for (let i = 0; i < scrpts.length; i++) {
await main(scrpts[i]);
}
});
cnsl.watching(`Rollup watching ${alerts.str.ext(opts.globs.join(', '))}`);
} catch (err) {
console.error(`${alerts.error} Rollup (run): ${err.stack}`);
}
} else {
for (let i = 0; i < opts.modules.length; i++) {
await main(opts.modules[i]);
}
cnsl.success(`Rollup finished`);
process.exit();
}
};
/**
* Export our methods
*
* @type {Object}
*/
module.exports = {
main: main,
run: run,
options: options
};