-
Notifications
You must be signed in to change notification settings - Fork 27
/
rollup.js
59 lines (52 loc) · 1.43 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
const babel = require('@babel/core');
const { createFilter } = require('@rollup/pluginutils');
const babelPlugin = require('./babel.js');
const NAME = require('./package.json').name;
const processCSS = require('./src/process-css.js');
module.exports = function style9Plugin({
include,
exclude,
fileName,
name,
parserOptions = {
plugins: ['typescript', 'jsx']
},
...options
} = {}) {
// Default name required to ensure extension
if (!fileName && !name) name = 'index.css';
const filter = createFilter(include, exclude);
const styles = Object.create(null);
return {
name: NAME,
async transform(input, filename) {
if (!filter(filename)) return;
const { code, map, metadata } = await babel.transformAsync(input, {
plugins: [[babelPlugin, options]],
filename,
sourceMaps: true,
parserOpts: parserOptions,
babelrc: false
});
styles[filename] = metadata.style9 || '';
return { code, map };
},
generateBundle(options, bundles) {
let css = '';
// Collect css from files that are included in this build
for (const bundle in bundles) {
for (const id in bundles[bundle].modules) {
if (id in styles) {
css += styles[id];
}
}
}
this.emitFile({
type: 'asset',
source: processCSS(css, { from: undefined }).css,
fileName,
name
});
}
};
};