forked from mapbox/mapbox-gl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrollup.config.js
More file actions
133 lines (121 loc) · 5.36 KB
/
rollup.config.js
File metadata and controls
133 lines (121 loc) · 5.36 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
import fs from 'fs';
import path from 'path';
import {fileURLToPath} from 'url';
import {readFile} from 'node:fs/promises';
import {transformSync} from 'esbuild';
import browserslistToEsbuild from 'browserslist-to-esbuild';
import {plugins} from './build/rollup_plugins.js';
import banner from './build/banner.js';
const {BUILD, MINIFY} = process.env;
const minified = MINIFY === 'true';
const bench = BUILD === 'bench';
const production = BUILD === 'production' || bench;
function buildType(build, minified) {
switch (build) {
case 'production':
if (minified) return 'dist/mapbox-gl.js';
return 'dist/mapbox-gl-unminified.js';
case 'bench':
return 'dist/mapbox-gl-bench.js';
case 'dev':
return 'dist/mapbox-gl-dev.js';
default:
return 'dist/mapbox-gl-dev.js';
}
}
const outputFile = buildType(BUILD, MINIFY);
const bundlePreludeSource = fs.readFileSync(fileURLToPath(new URL('./rollup/bundle_prelude.js', import.meta.url)), 'utf8');
const bundlePrelude = production ? transformSync(bundlePreludeSource, {target: browserslistToEsbuild(), minify: true}).code : bundlePreludeSource;
function preserveDynamicImport() {
return {
name: 'preserve-dynamic-import',
resolveDynamicImport() {
// Prevent Rollup from resolving and code-splitting dynamic imports.
// They will be loaded natively at runtime via import().
return false;
},
renderDynamicImport() {
return {left: 'import(', right: ')'};
}
};
}
export default ({watch}) => {
return [{
// First, use code splitting to bundle GL JS into three "chunks":
// - rollup/build/index.js: the main module, plus all its dependencies not shared by the worker module
// - rollup/build/worker.js: the worker module, plus all dependencies not shared by the main module
// - rollup/build/shared.js: the set of modules that are dependencies of both the main module and the worker module
//
// This is also where we do all of our source transformations:
// transpiling ES6 features, inlining shader sources as strings, etc.
input: ['src/index.ts', 'src/source/worker.ts'],
output: {
dir: 'rollup/build/mapboxgl',
format: 'amd',
sourcemap: 'inline',
indent: false,
chunkFileNames: 'shared.js',
minifyInternalExports: production
},
onwarn: production ? onwarn : false,
treeshake: production ? {preset: 'recommended', moduleSideEffects: (id) => !id.endsWith('devtools.ts')} : false,
plugins: [preserveDynamicImport()].concat(plugins({minified, production, bench, test: false, keepClassNames: false, mode: BUILD}))
}, {
// Next, bundle together the three "chunks" produced in the previous pass
// into a single, final bundle. See rollup/bundle_prelude.js and
// rollup/mapboxgl.js for details.
input: 'rollup/mapboxgl.js',
output: {
name: 'mapboxgl',
file: outputFile,
format: 'umd',
sourcemap: production ? true : 'inline',
indent: false,
intro: bundlePrelude,
banner
},
treeshake: false,
plugins: [
preserveDynamicImport(),
// Ingest the sourcemaps produced in the first step of the build.
// This is the only reason we use Rollup for this second pass
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
sourcemaps({watch}),
]
}];
};
function sourcemaps({watch}) {
const base64SourceMapRegExp = /\/\/# sourceMappingURL=data:[^,]+,([^ ]+)/;
return {
name: 'sourcemaps',
async load(id) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const code = await readFile(id, {encoding: 'utf8'});
const match = base64SourceMapRegExp.exec(code);
if (!match) return;
const base64EncodedSourceMap = match[1];
const decodedSourceMap = Buffer.from(base64EncodedSourceMap, 'base64').toString('utf-8');
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const map = JSON.parse(decodedSourceMap);
// Starting with Rollup 4.x, we need to explicitly watch files
// if their content is returned by the load hook.
// https://github.com/rollup/rollup/pull/5150
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
if (watch) this.addWatchFile(id);
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
return {code, map};
}
};
}
function onwarn(warning) {
const styleSpecPath = path.resolve('src', 'style-spec');
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (warning.code === 'CIRCULAR_DEPENDENCY') {
// Ignore circular dependencies in style-spec and throw on all others
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument
if (!warning.ids[0].startsWith(styleSpecPath)) throw new Error(warning.message);
} else {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
console.error(`(!) ${warning.message}`);
}
}