-
-
Notifications
You must be signed in to change notification settings - Fork 191
/
rollup.config.js
186 lines (174 loc) · 5.76 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
184
185
186
// /* eslint-disable import/no-deprecated, import/namespace,
// import/default, import/no-named-as-default,
// import/no-named-as-default-member -- Problems with JSON import */
import {readFile} from 'node:fs/promises';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonJS from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import {babel} from '@rollup/plugin-babel';
import globals from 'rollup-plugin-node-globals';
import nodePolyfills from 'rollup-plugin-node-polyfills';
import filesize from 'rollup-plugin-filesize';
import terser from '@rollup/plugin-terser';
import builtins from 'builtin-modules';
const pkg = JSON.parse(await readFile(
new URL('package.json', import.meta.url)
));
const {name: pkgName} = pkg;
const babelBrowserOptions = {
// sourceMapsAbsolute: true,
babelHelpers: 'bundled',
plugins: ['add-module-exports'],
presets: [
['@babel/env', {
targets: pkg.browserslist[0] // cover 100%
}]
]
};
const babelNodeOptions = {...babelBrowserOptions,
babelHelpers: 'bundled',
presets: [
['@babel/env', {
targets: {
node: '16'
}
}]
]
};
const getRollupPlugins = (babelOptions, {addBuiltins, mainFields, min} = {}) => {
const ret = [
nodeResolve({
mainFields,
preferBuiltins: !addBuiltins
}),
commonJS({
// Gets issue with dynamic requires and we aren't
// "importing" anyways
ignore: ['sqlite3']
}),
babel(babelOptions),
filesize({
showBeforeSizes: 'build'
})
];
if (addBuiltins) {
ret.unshift(globals(), nodePolyfills());
} else {
ret.unshift(json());
}
if (min) {
ret.push(terser({
// // Not apparently working per https://github.com/TrySound/rollup-plugin-terser/issues/68
// comments (node, comment) {
// return (/\/\*!/u).test(comment.value);
// }
}));
}
return ret;
};
const browserEnvironment = ({input, name, output: file}) => {
const banner = `/*! ${pkg.name} - v${pkg.version} - ` +
`${new Intl.DateTimeFormat('en-US').format(new Date())} */\n`;
return [true, false].map((min) => {
return {
input,
output: {
name,
banner,
file: min ? file.replace(/\.js$/u, '.min.js') : file,
format: 'umd',
sourcemap: true
},
plugins: getRollupPlugins(
babelBrowserOptions,
{min, addBuiltins: true, mainFields: ['browser', 'module', 'main']} // Don't need 'jsnext'?
)
};
});
};
const nodeEnvironment = ({input, name, output: file}) => {
const banner = `/*! ${pkg.name} - v${pkg.version} - ` +
`${new Intl.DateTimeFormat('en-US').format(new Date())} */\n`;
return [false].map((min) => {
return {
input,
external: [
...builtins,
'websql/custom/index.js', 'websql/lib/sqlite/SQLiteDatabase',
// Fix from https://github.com/rollup/rollup/issues/1507#issuecomment-340550539
'readable-stream', 'readable-stream/transform'
],
output: {
file: min ? file.replace(/\.js$/u, '.min.js') : file,
name,
banner,
exports: 'default',
// Avoid using `browser` entry in package.json
format: 'cjs',
// Avoid `window` checking (link now broken)
// https://github.com/substack/node-rollup/issues/1277#issuecomment-115198436
sourcemap: true
// Notes when using browserify:
// Could try for consistency with any relative paths if still
// seeing https://github.com/axemclion/IndexedDBShim/issues/291 ;
// see also http://stackoverflow.com/a/33124979/271577
// basedir: __dirname,
},
plugins: getRollupPlugins(
babelNodeOptions,
{
min,
addBuiltins: false
// mainFields: ['module', 'main'] // Default
}
)
};
});
};
/**
* @returns {Rollup[]}
*/
export default function rollupConfig () {
// if (commandLineArgs.configBrowserOnly) {
return [
{
input: 'node_modules/@unicode/unicode-15.1.0/Binary_Property/Expands_On_NFD/regex.js',
output: {
banner: '// @ts-nocheck\n',
file: 'src/unicode-regex.js',
format: 'esm'
},
plugins: [
commonJS({
include: ['node_modules/**']
})
]
},
...browserEnvironment({
name: 'IDBKeyUtils',
input: 'src/Key.js',
output: `dist/${pkgName}-Key.js`
}),
...browserEnvironment({
input: 'src/browser-UnicodeIdentifiers.js',
output: `dist/${pkgName}-UnicodeIdentifiers.js`
}),
...nodeEnvironment({
input: 'src/node-UnicodeIdentifiers.js',
output: `dist/${pkgName}-UnicodeIdentifiers-node.cjs`
}),
...browserEnvironment({
input: 'src/browser.js',
output: `dist/${pkgName}.js`
}),
...browserEnvironment({
name: 'setGlobalVars',
input: 'src/browser-noninvasive.js',
output: `dist/${pkgName}-noninvasive.js`
}),
...nodeEnvironment({
input: 'src/node.js',
output: `dist/${pkgName}-node.cjs`
})
];
}