-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathitsl.rollup.js
135 lines (122 loc) · 4.32 KB
/
itsl.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
/* global require module */
const fs = require('fs');
const path = require('path');
const { nodeResolve } = require('@rollup/plugin-node-resolve');
const scss = require('rollup-plugin-scss');
const svelte = require('rollup-plugin-svelte');
const terser = require('@rollup/plugin-terser');
const json = require('@rollup/plugin-json');
const defaultOptions = {
legacy: false,
webComponents: false,
beforePlugins: [],
plugins: [],
};
/**
* Returns a Rollup Configuration Object for Svelte files with optional polyfills and es5 compatibility
* @param {string} src The source file
* @param {string} dest The destination file
* @param {object} options
* @param {boolean} [options.webComponents] Include polyfills for webComponents
* @param {any[]} [options.plugins] Array of plugins to run in addition to the defaults
* @returns {object} A Rollup Configuration Object
*/
const Svelte = (src, dest, options = defaultOptions) => ({
input: src,
output: {
file: dest,
format: 'esm',
sourcemap: true,
},
treeshake: true,
plugins: [
...options.beforePlugins || defaultOptions.beforePlugins,
// @ts-ignore
svelte(),
nodeResolve(
{
dedupe: ['svelte'],
browser: true
}),
json(),
// @ts-ignore
terser(),
...options.plugins || defaultOptions.plugins
],
});
const sassOptions = {
beforePlugins: [],
plugins: [],
};
/**
* Returns a Rollup Configuration Object for Scss files
* @param {string} src The source file
* @param {string} dest The destination file
* @param {object} options
* @param {any[]} options.plugins Array of plugins to run in addition to the defaults
* @returns {object} A Rollup Configuration Object
*/
const Sass = (src, dest, options = sassOptions) => ({
input: src,
// Required for Rollup, just ignore
output: {
file: dest,
format: 'esm'
},
// Script will ALWAYS render an empty file at first, ignore EMPTY_BUNDLE
onwarn: (warning, onwarn) => warning.code === 'EMPTY_BUNDLE' || onwarn(warning),
plugins: [
...options.beforePlugins || sassOptions.beforePlugins,
scss({
sass: require('sass'),
importer(path) {
return {
file: path.replace(/^~/, 'node_modules/')
.replace(/^@itslearning\//, 'node_modules/@itslearning/')
};
},
output: `${dest}.temp`,
outputStyle: 'compressed',
}),
{
name: 'Rollup Sass Cleaner Plugin',
/**
* Renames the .temp file to .css overwriting the default javascript output
*/
writeBundle: () => fs.renameSync(`${dest}.temp`, dest)
},
...options.plugins || sassOptions.plugins
]
});
/**
* Create an array of Rollup Configuration Objects
* @param {object} config The required configuration
* @param {string} config.destination The path where the generated file should be saved.
* @param {Array<string[]>} config.files The files to be processed.
* @param {any} config.plugins Additional rollup plugins to run.
* @returns {object} A Rollup Configuration Object
*/
const ItslRollup = ({ destination, files, plugins = {} }) => {
const configs = [];
files.forEach(file => {
const inFile = Array.isArray(file) ? file[0] : file;
const outFile = Array.isArray(file) ? file[1] || inFile : file;
const inPath = path.parse(inFile);
const { name } = path.parse(outFile || inFile);
if (inPath.ext !== '.js' && inPath.ext !== '.scss') {
throw (new Error(`Unknown format ${inPath.ext}`));
}
if (inPath.ext === '.js') {
configs.push(Svelte(inFile, `${destination}${name}.js`, { legacy: false, plugins: plugins.script }));
configs.push(Svelte(inFile, `${destination}${name}.es5.js`, { legacy: true, plugins: plugins.script }));
} else if (inPath.ext === '.scss') {
configs.push(Sass(inFile, `${destination}${name}.css`, plugins.style));
}
});
return configs;
};
module.exports = {
ItslRollup,
Svelte,
Sass,
};