-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathrollup.config.esm.ts
More file actions
86 lines (80 loc) · 3.04 KB
/
rollup.config.esm.ts
File metadata and controls
86 lines (80 loc) · 3.04 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
import {plugins} from './build/rollup_plugins.js';
import type {Plugin, RollupOptions} from 'rollup';
const {BUILD, MINIFY} = process.env;
const minified = MINIFY === 'true';
const production = BUILD === 'production';
/**
* Creates an ESM rollup config.
* @param dir - Output directory
* @param workerSuffix - Suffix for the web_worker substitution ('_esm_cdn' for cross-origin Blob workaround, '_esm_npm' for bundler-detectable pattern)
*/
function esmConfig(dir: string, workerSuffix: string): RollupOptions {
return {
input: {
'mapbox-gl': 'src/index.ts',
'worker': 'src/source/worker.ts'
},
output: {
dir,
chunkFileNames: (chunk) => {
if (chunk.name === "index") return "core.js";
if (chunk.isDynamicEntry) {
if (chunk.facadeModuleId.endsWith('hd_main_imports.ts')) return 'hd.shared.js';
if (chunk.facadeModuleId.endsWith('hd_worker_imports.ts')) return 'hd.worker.js';
}
return 'shared.js'; // catch all for deduped code
},
experimentalMinChunkSize: 5000,
format: 'esm',
compact: true,
// Do not add additional interop helpers.
interop: 'esModule',
// Never add a `__esModule` property when generating exports.
esModule: false,
// Allow using ES2015 features in Rollup wrappers and helpers.
generatedCode: 'es2015',
exports: 'named',
minifyInternalExports: true,
externalLiveBindings: false,
sourcemap: true,
},
treeshake: production ? {
preset: 'smallest',
moduleSideEffects: (id) => !id.endsWith('devtools.ts'),
} : false,
strictDeprecations: true,
preserveEntrySignatures: 'strict',
plugins: [
esmSubstitutions(workerSuffix),
].concat(plugins({production, minified, test: false, keepClassNames: false, mode: BUILD, format: 'esm'})),
};
}
export default (): RollupOptions[] => {
if (production) {
// Production: build both NPM (dist/esm/) and CDN (dist/esm-cdn/) variants
return [
esmConfig('dist/esm/', '_esm_npm'),
esmConfig('dist/esm-cdn/', '_esm_cdn'),
];
}
// Dev: build only NPM variant (dist/esm-dev/)
return [
esmConfig('dist/esm-dev/', '_esm_npm'),
];
};
const filesToSub = new Set(['hd_main', 'hd_worker']);
function esmSubstitutions(workerSuffix: string): Plugin {
return {
name: 'esm-substitution-resolver',
resolveId(source, importer) {
const name = source.slice(source.lastIndexOf('/') + 1);
if (name === 'web_worker') {
return this.resolve(`${source}${workerSuffix}.ts`, importer, {skipSelf: true});
}
if (filesToSub.has(name)) {
return this.resolve(`${source}_esm.ts`, importer, {skipSelf: true});
}
return null;
}
};
}