-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
rollup.config.js
183 lines (181 loc) · 6.2 KB
/
rollup.config.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Note:
* This Rollup config is used to build both the `wmr` and `create-wmr` packages.
*/
import { resolve, sep } from 'path';
import shebangPlugin from 'rollup-plugin-preserve-shebang';
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import alias from '@rollup/plugin-alias';
import json from '@rollup/plugin-json';
import replace from '@rollup/plugin-replace';
import builtins from 'builtin-modules';
import { minify } from 'terser';
/** @type {import('rollup').RollupOptions} */
const config = {
input: 'src/cli.js',
inlineDynamicImports: true,
output: {
file: 'wmr.cjs',
format: 'cjs',
compact: true,
freeze: false,
interop: false,
namespaceToStringTag: false,
externalLiveBindings: false,
preferConst: true,
plugins: [
{
name: 'minify',
async renderChunk(code) {
const result = await minify(code, {
ecma: 2019,
module: true,
compress: {
toplevel: true
},
mangle: {
eval: true
},
sourceMap: false,
output: {
comments: false,
inline_script: false,
ecma: 2019
}
});
if (typeof result.code === 'string') code = result.code;
return { code };
}
}
]
},
external: [...builtins, 'less'],
// /* Logs all included npm dependencies: */
// external(source, importer) {
// const ch = source[0];
// if (ch === '.' || ch === '/') return false;
// if (builtins.includes(source)) return true;
// const mod = source.match(/^(@[^/]+\/)?[^/]+/)[0];
// const mods = global.mods || (global.mods = new Set());
// if (!mods.has(mod)) {
// mods.add(mod);
// console.log(mod, 'imported by', importer);
// }
// },
plugins: [
shebangPlugin(),
replace({
'process.env.VERSION': JSON.stringify(require('./package.json').version)
}),
{
// This inlines some fs.promises.readFile() calls, while allowing them to run unbundled in Node.
name: 'inline-fs-readfile',
transform(code, id) {
if (/\/\/\s*rollup-inline-files/.test(code)) {
code = code.replace(
/fs\.readFile\(new\s+URL\s*\(\s*(['"`])(.+?)\1\s*,\s*__filename\s*\)\s*,\s*'utf-8'\s*\)/g,
(str, quote, filename) => {
const path = require('path');
const fs = require('fs');
const filepath = path.resolve(path.dirname(id), filename);
try {
const text = fs.readFileSync(filepath, 'utf-8');
// console.log('inlined ' + filename + ' into ' + id + ': ' + text.length + 'b');
return `Promise.resolve(${JSON.stringify(text)})`;
} catch (err) {
this.warn(`Failed to inline ${filename} into ${id}:\n${err.message}`);
return `Promise.reject(Error(${JSON.stringify(err.message)}))`;
}
}
);
return { code, map: null };
}
}
},
{
// This fixes DevCert breaking in Rollup due to dynamic require usage.
// https://github.com/davewasmer/devcert/blob/master/src/platforms/index.ts
name: 'fix-devcert',
transform(code, id) {
if (/devcert[/\\]dist[/\\]platforms[/\\]index\.js$/.test(id)) {
const platforms = require('fs')
.readdirSync('../../node_modules/devcert/dist/platforms')
.reduce((str, p) => {
const name = p.replace(/\.js$/, '');
if (name !== p && name !== 'index') {
if (str) str += ',';
str += `"${name}": require("./${p}")`;
}
return str;
}, '');
return code.replace('require(`./${process.platform}`)', `({${platforms}})[process.platform]`);
}
}
},
{
// This ensures the template files for rollup-plugin-visualizer are inlined
// rather than bundleds as fs.readFile()
name: 'fix-visualizer',
transform(code, id) {
if (/rollup-plugin-visualizer[/\\]dist[/\\]plugin[/\\]build-stats\.js$/.test(id)) {
code = code.replace(/fs.*readFile.*\(__dirname,\s*(.+?)\)\s*,\s*"utf8"\s*\)/g, (_str, stringifiedJoin) => {
const path = require('path');
const fs = require('fs');
const filePathParts = stringifiedJoin
.replace(/['"`]+/g, '')
.replace(/\$\{template\}/g, 'treemap')
.split(', ');
const filepath = path.resolve(path.dirname(id), ...filePathParts);
try {
const text = fs.readFileSync(filepath, 'utf-8');
return `Promise.resolve(${JSON.stringify(text)})`;
} catch (err) {
this.warn(`Failed to inline ${filepath} into ${id}:\n${err.message}`);
return `Promise.reject(Error(${JSON.stringify(err.message)}))`;
}
});
return { code, map: null };
}
}
},
alias({
entries: [
{ find: /^@babel\/plugin-syntax-jsx$/, replacement: require.resolve('./src/lib/~empty.js') },
{ find: /^postcss$/, replacement: 'postcss-es6' },
{ find: /^postcss[/\\]$/, replacement: `postcss-es6${sep}` },
// bypass native modules aimed at production WS performance:
{ find: /^bufferutil$/, replacement: `bufferutil${sep}fallback.js` },
{ find: /^utf-8-validate$/, replacement: `utf-8-validate${sep}fallback.js` },
// just use native streams:
{ find: /(^|[/\\])readable-stream$/, replacement: require.resolve('./src/lib/~readable-stream.js') },
{
find: /(^|[/\\])readable-stream[/\\]duplex/,
replacement: require.resolve('./src/lib/~readable-stream-duplex.js')
},
// just use util:
{ find: /^inherits$/, replacement: require.resolve('./src/lib/~inherits.js') },
// only pull in fsevents when its exports are accessed (avoids exceptions):
{ find: /^fsevents$/, replacement: require.resolve('./src/lib/~fsevents.js') },
// avoid pulling in 50kb of "editions" dependencies to resolve one file:
{ find: /^istextorbinary$/, replacement: 'istextorbinary/edition-node-0.12/index.js' }, // 2.6.0
{ find: /^acorn-import-assertions$/, replacement: require.resolve('acorn-import-assertions') }
]
}),
commonjs({
exclude: [/\.mjs$/, /\/rollup\//, resolve('src')],
ignore: builtins,
transformMixedEsModules: true,
requireReturnsDefault: 'preferred'
}),
nodeResolve({
preferBuiltins: true,
// Rollup prefers "default" by default and rollup itself points to a
// browser build there...
exportConditions: ['node', 'import', 'module', 'default'],
extensions: ['.mjs', '.js', '.json', '.es6', '.node']
}),
json()
]
};
export default config;