Repository navigation
Expand file tree
/
Copy pathrollup.config.mjs
More file actions
317 lines (282 loc) · 10.8 KB
/
Copy pathrollup.config.mjs
File metadata and controls
317 lines (282 loc) · 10.8 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/**
* Copyright 2020-2022, 2025-2026 Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import commonjs from '@rollup/plugin-commonjs';
import nodeResolve from '@rollup/plugin-node-resolve';
import terser from '@rollup/plugin-terser';
import alias from '@rollup/plugin-alias';
import { createRequire } from 'module';
import { fileURLToPath } from 'url';
import { readdirSync } from 'fs';
import { dirname, join, relative, resolve } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const require = createRequire(import.meta.url);
const { dependencies, peerDependencies } = require('./package.json');
const resolvePlugin = nodeResolve();
const resolvePluginNode = nodeResolve({
exportConditions: ['node'],
});
const resolvePluginBrowser = nodeResolve({
browser: true,
exportConditions: ['browser'],
});
const resolvePluginByPlatform = {
node: resolvePluginNode,
browser: resolvePluginBrowser,
};
const aliasPlugin = alias({
entries: [
{ find: 'error_message', replacement: join(__dirname, '.build/message/error_message.gen.js') },
{ find: 'log_message', replacement: join(__dirname, '.build/message/log_message.gen.js') },
]
});
const externalDeps = ['https', 'http', 'url'].concat(Object.keys({ ...dependencies, ...peerDependencies } || {}));
// uuid@13 is ESM-only and does not provide a CommonJS export.
// Since our CJS bundles must work in CommonJS environments, we bundle uuid
// into the CJS output instead of leaving it as an external dependency.
const externalDepsWithoutUuid = externalDeps.filter(dep => dep !== 'uuid');
// React Native optional import shims (lib/import.react_native) are not bundled into the main bundles.
// Each is shipped as its own module at the same relative location in dist, because Metro before 0.82.5
// shifts every later require in a module when an optional dependency is missing.
// See lib/import.react_native/README.md.
const BUILD_DIR = join(__dirname, '.build');
const RN_IMPORT_SHIM_DIR = 'import.react_native';
// Shim paths relative to the shim directory, without extension. Shims are named after their package, so
// a scoped package's shim is in a subdirectory, e.g. '@react-native-async-storage/async-storage'.
const listShims = (subdir = '') =>
readdirSync(join(__dirname, 'lib', RN_IMPORT_SHIM_DIR, subdir), { withFileTypes: true }).flatMap(entry => {
const path = subdir ? `${subdir}/${entry.name}` : entry.name;
if (entry.isDirectory()) return listShims(path);
return entry.name.endsWith('.ts') && !entry.name.endsWith('.spec.ts') ? [path.replace(/\.ts$/, '')] : [];
});
const RN_IMPORT_SHIMS = listShims();
// Leaves shim imports out of the main bundles and writes them as a path relative to the bundle, e.g.
// import '../../../import.react_native/@react-native-async-storage/async-storage' (from a file deep in .build)
// -> require("./import.react_native/@react-native-async-storage/async-storage.js") (in dist/index.react_native.min.js)
// The bundles are at the root of dist and the shims are built to dist/import.react_native,
// so that path points to the built shim.
// Marking the imports external via the `external` option is not enough: Rollup would resolve them to the
// file in .build and write a path from the bundle back into .build (../.build/import.react_native/...).
// Returning a relative id from resolveId makes Rollup write it exactly as given.
const rnImportShimExternalPlugin = {
name: 'rn-import-shim-external',
resolveId(source, importer) {
if (!importer || !source.startsWith('.')) return null;
const pathInBuild = relative(BUILD_DIR, resolve(dirname(importer), source)).replace(/\.js$/, '');
if (!pathInBuild.startsWith(`${RN_IMPORT_SHIM_DIR}/`)) return null;
return { id: `./${pathInBuild}.js`, external: true };
},
};
// Shims are built as CommonJS with __esModule set, so a default import of a shim must read its `default`
// export ('esModule'). Rollup's 'default' interop, used for the other externals, would read the whole module.
const rnImportShimInterop = id => (id && id.startsWith(`./${RN_IMPORT_SHIM_DIR}/`) ? 'esModule' : 'default');
// Fails the build unless every built shim contains exactly one require(), of its own package (the shim's
// name). Any other require in a shim could be shifted by the Metro bug when the package is missing. This
// catches imports of other packages, tslib helpers, and code shared between shims (Rollup moves it into a
// shared chunk that the shims require). The shim's own require() is plain code, so Rollup doesn't list it in
// chunk.imports; the generated code is parsed instead, with Rollup's parser.
const rnImportShimCheckPlugin = {
name: 'rn-import-shim-check',
generateBundle(_, bundle) {
// Module names of every require(...) call in an ESTree AST; a call whose argument is not a string is
// listed as '<non-literal>'.
const findRequireCalls = (node, found = []) => {
if (Array.isArray(node)) {
node.forEach(child => findRequireCalls(child, found));
} else if (node && typeof node.type === 'string') {
if (node.type === 'CallExpression' && node.callee.type === 'Identifier' && node.callee.name === 'require') {
const [arg] = node.arguments;
found.push(arg && arg.type === 'Literal' && typeof arg.value === 'string' ? arg.value : '<non-literal>');
}
for (const value of Object.values(node)) {
if (value && typeof value === 'object') findRequireCalls(value, found);
}
}
return found;
};
for (const chunk of Object.values(bundle)) {
if (chunk.type !== 'chunk') continue;
const required = findRequireCalls(this.parse(chunk.code));
if (!chunk.isEntry || required.length !== 1 || required[0] !== chunk.name) {
this.error(
`${RN_IMPORT_SHIM_DIR}/${chunk.fileName}: a shim must contain exactly one require(), of its own package` +
` '${chunk.name}' (found: ${required.join(', ') || 'none'}). See lib/${RN_IMPORT_SHIM_DIR}/README.md.`
);
}
}
},
};
const rnImportShimsBundle = {
plugins: [rnImportShimCheckPlugin],
external: externalDeps,
input: Object.fromEntries(
RN_IMPORT_SHIMS.map(name => [name, join(BUILD_DIR, RN_IMPORT_SHIM_DIR, `${name}.js`)])
),
output: {
exports: 'named',
format: 'cjs',
dir: join('dist', RN_IMPORT_SHIM_DIR),
entryFileNames: '[name].js',
},
};
const cjsBundleFor = (platform, opt = {}) => {
const { minify, ext } = {
minify: true,
ext: '.js',
...opt,
};
const min = minify ? '.min' : '';
return {
plugins: [rnImportShimExternalPlugin, aliasPlugin, resolvePluginByPlatform[platform] || resolvePlugin, commonjs()],
external: externalDepsWithoutUuid,
input: `.build/index.${platform}.js`,
output: {
exports: 'named',
format: 'cjs',
file: `dist/index.${platform}${min}${ext}`,
interop: rnImportShimInterop,
plugins: minify ? [terser()] : undefined,
sourcemap: true,
},
}
};
const esmBundleFor = (platform, opt) => {
const { minify, ext } = {
minify: true,
ext: '.js',
...opt,
};
const min = minify ? '.min' : '';
return {
...cjsBundleFor(platform),
external: externalDeps,
output: [
{
format: 'es',
file: `dist/index.${platform}.es${min}${ext}`,
plugins: minify ? [terser()] : undefined,
sourcemap: true,
},
],
}
};
const cjsBundleForUAParser = (opt = {}) => {
const { minify, ext } = {
minify: true,
ext: '.js',
...opt,
};
const min = minify ? '.min' : '';
return {
plugins: [aliasPlugin, resolvePlugin, commonjs()],
external: externalDepsWithoutUuid,
input: `.build/odp/ua_parser/ua_parser.js`,
output: {
exports: 'named',
format: 'cjs',
file: `dist/ua_parser${min}${ext}`,
plugins: minify ? [terser()] : undefined,
sourcemap: true,
},
};
};
const esmBundleForUAParser = (opt = {}) => {
const { minify, ext } = {
minify: true,
ext: '.js',
...opt,
};
const min = minify ? '.min' : '';
return {
...cjsBundleForUAParser(),
external: externalDeps,
output: [
{
format: 'es',
file: `dist/ua_parser.es${min}${ext}`,
plugins: minify ? [terser()] : undefined,
sourcemap: true,
},
],
};
};
const umdBundle = {
plugins: [
aliasPlugin,
resolvePluginBrowser,
commonjs(),
],
input: '.build/index.browser.js',
output: [
{
name: 'optimizelySdk',
format: 'umd',
file: 'dist/optimizely.browser.umd.js',
exports: 'named',
},
{
name: 'optimizelySdk',
format: 'umd',
file: 'dist/optimizely.browser.umd.min.js',
exports: 'named',
plugins: [terser()],
sourcemap: true,
},
],
};
// A separate bundle for json schema validator.
const jsonSchemaBundle = {
plugins: [aliasPlugin, resolvePlugin, commonjs()],
external: ['json-schema'],
input: '.build/utils/json_schema_validator/index.js',
output: {
exports: 'named',
format: 'cjs',
file: 'dist/optimizely.json_schema_validator.min.js',
plugins: [terser()],
sourcemap: true,
},
};
const bundles = {
'cjs-node-min': cjsBundleFor('node'),
'cjs-browser-min': cjsBundleFor('browser'),
'cjs-react-native-min': cjsBundleFor('react_native'),
'cjs-universal': cjsBundleFor('universal'),
'esm-browser-min': esmBundleFor('browser'),
'esm-node-min': esmBundleFor('node', { ext: '.mjs' }),
'esm-react-native-min': esmBundleFor('react_native'),
'esm-universal': esmBundleFor('universal'),
'cjs-react-native-import-shims': rnImportShimsBundle,
'json-schema': jsonSchemaBundle,
'cjs-ua-parser-min': cjsBundleForUAParser(),
'esm-ua-parser-min': esmBundleForUAParser(),
umd: umdBundle,
};
// Collect all --config-* options and return the matching bundle configs
// Builds all bundles if no --config-* option given
// --config-cjs will build all three cjs-* bundles
// --config-umd will build only the umd bundle
// --config-umd --config-json will build both umd and the json-schema bundles
export default args => {
const patterns = Object.keys(args)
.filter(arg => arg.startsWith('config-'))
.map(arg => arg.replace(/config-/, ''));
// default to matching all bundles
if (!patterns.length) patterns.push(/.*/);
return Object.entries(bundles)
.filter(([name, config]) => patterns.some(pattern => name.match(pattern)))
.map(([name, config]) => config);
};