-
Notifications
You must be signed in to change notification settings - Fork 290
/
rollup.config.mjs
126 lines (112 loc) · 4.2 KB
/
rollup.config.mjs
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
import fs from 'fs';
import { execSync } from 'child_process';
import babel from '@rollup/plugin-babel';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import globals from 'rollup-plugin-node-globals';
import builtins from 'rollup-plugin-node-builtins';
import replace from '@rollup/plugin-replace';
import sourcemaps from 'rollup-plugin-sourcemaps2';
import terser from '@rollup/plugin-terser';
import json from '@rollup/plugin-json';
import { importAsString } from 'rollup-plugin-string-import';
import serve from 'rollup-plugin-serve';
import livereload from 'rollup-plugin-livereload';
const ESM = (process.env.ESM !== 'false'); // default to ESM on
const MINIFY = (process.env.MINIFY === 'true');
const SERVE = (process.env.SERVE === 'true');
const outputFile = `dist/tangram.${MINIFY ? 'min' : 'debug'}.${ESM ? 'm' : ''}js`;
// Use two pass code splitting and re-bundling technique, for another example see:
// https://github.com/mapbox/mapbox-gl-js/blob/master/rollup.config.js
const config = [{
input: ['src/index.js', 'src/scene/scene_worker.js'],
output: {
dir: 'build',
format: 'amd',
sourcemap: 'inline',
indent: false,
chunkFileNames: 'shared.js',
},
plugins: [
resolve({
browser: true,
preferBuiltins: false,
}),
commonjs({
// Avoids Webpack minification errors
ignoreGlobal: true,
}),
json(), // load JSON files
importAsString({
include: ['**/*.glsl'] // inline shader files
}),
babel({
exclude: 'node_modules/**',
babelHelpers: "runtime"
}),
// These are needed for jszip node-environment compatibility,
// previously provided by browserify
globals(),
builtins(),
MINIFY ? terser() : false
]
}, {
// Second pass: combine the chunks from the first pass into a single bundle
input: 'build/bundle.mjs',
output: {
name: 'Tangram',
file: outputFile,
format: ESM ? 'esm' : 'umd',
sourcemap: MINIFY ? false : true,
indent: false,
// intro: fs.readFileSync(require.resolve('./build/intro.js'), 'utf8')
intro: `
// define() gets called for each chunk generated by the first Rollup pass.
// The order the chunks are called in is controlled by the imports in bundle.js:
//
// shared.js: shared dependencies between main and worker threads
// scene_worker.js: worker thread code
// index.js: main thread code
// Once all chunks have been provided, the worker thread code is assembled,
// incorporating the shared chunk code, then turned into a blob URL which
// can be used to instantiate the worker.
var shared, worker, Tangram = {};
function define(_, chunk) {
if (!shared) {
shared = chunk;
} else if (!worker) {
worker = chunk;
} else {
var worker_bundle = 'var shared_chunk = {}; (' + shared + ')(shared_chunk); (' + worker + ')(shared_chunk);'
var shared_chunk = {};
shared(shared_chunk);
Tangram = chunk(shared_chunk);
Tangram.workerURL = window.URL.createObjectURL(new Blob([worker_bundle], { type: 'text/javascript' }));
}
}
`
},
treeshake: false,
plugins: [
replace({
preventAssignment: true,
values: {
_ESM: ESM,
_SHA: '\'' + String(execSync('git rev-parse HEAD')).trim(1) + '\''
}
}),
sourcemaps(), // use source maps produced in the first pass
// optionally start server and watch for rebuild
SERVE ? serve({
port: 8000,
contentBase: '',
headers: {
'Access-Control-Allow-Origin': '*'
}
}): false,
SERVE ? livereload({
watch: 'dist'
}) : false
],
}];
export default config