-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrollup.config.js
More file actions
151 lines (141 loc) · 4.63 KB
/
rollup.config.js
File metadata and controls
151 lines (141 loc) · 4.63 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
const { nodeResolve: resolve } = require('@rollup/plugin-node-resolve');
const commonjs = require('@rollup/plugin-commonjs');
const terser = require('rollup-plugin-terser').terser;
const pkg = require('./package.json');
function glsl() {
return {
transform(code, id) {
if (/\.vert$/.test(id) === false && /\.frag$/.test(id) === false && /\.glsl$/.test(id) === false) return null;
let transformedCode = JSON.stringify(code.trim()
.replace(/\r/g, '')
.replace(/[ \t]*\/\/.*\n/g, '') // remove //
.replace(/[ \t]*\/\*[\s\S]*?\*\//g, '') // remove /* */
.replace(/\n{2,}/g, '\n')); // # \n+ to \n;;
transformedCode = `export default ${transformedCode};`;
return {
code: transformedCode,
map: { mappings: '' }
};
}
};
}
function wgsl() {
return {
transform(code, id) {
if (/\.wgsl$/.test(id) === false) return null;
let transformedCode = JSON.stringify(code.trim()
// .replace(/(^\s*)|(\s*$)/gm, '')
.replace(/\r/g, '')
.replace(/[ \t]*\/\/.*\n/g, '') // remove //
.replace(/[ \t]*\/\*[\s\S]*?\*\//g, '') // remove /* */
.replace(/\n{2,}/g, '\n')); // # \n+ to \n;;
transformedCode = `export default ${transformedCode};`;
return {
code: transformedCode,
map: { mappings: '' }
};
}
};
}
const production = process.env.BUILD === 'production';
const outputFile = pkg.main;
const outputESFile = pkg.module;
const plugins = [
].concat(production ? [
removeGlobal(),
terser({
output : {
keep_quoted_props: true,
beautify: false,
comments : '/^!/'
}
})
] : []);
//worker.js中的global可能被webpack替换为全局变量,造成worker代码执行失败,所以这里统一把typeof global替换为typeof undefined
function removeGlobal() {
return {
transform(code, id) {
if (id.indexOf('worker.js') === -1) return null;
const commonjsCode = /typeof global/g;
const transformedCode = code.replace(commonjsCode, 'typeof undefined');
return {
code: transformedCode,
map: { mappings: '' }
};
}
};
}
const banner = `/*!\n * ${pkg.name} v${pkg.version}\n * LICENSE : ${pkg.license}\n * (c) 2016-${new Date().getFullYear()} maptalks.org\n */`;
let outro = pkg.name + ' v' + pkg.version;
if (pkg.peerDependencies && pkg.peerDependencies['maptalks']) {
outro += `, requires maptalks@${pkg.peerDependencies.maptalks}.`;
}
outro = `typeof console !== 'undefined' && console.log('${outro}');`;
module.exports = [
{
input: 'index.js',
plugins: [
glsl(),
wgsl(),
resolve({
browser: true,
preferBuiltins: false
}),
commonjs({
// global keyword handling causes Webpack compatibility issues, so we disabled it:
// https://github.com/mapbox/mapbox-gl-js/pull/6956
ignoreGlobal: true
})
].concat(plugins),
external: ['maptalks', '@maptalks/gl'],
output: {
globals: {
'maptalks': 'maptalks',
'@maptalks/gl': 'maptalks'
},
banner,
outro,
extend: true,
name: 'maptalks',
file: outputFile,
format: 'umd',
sourcemap: true,
},
watch: {
include: ['index.js', '**/*/*.vert', '**/*/*.frag', '**/*/*.wgsl']
}
},
{
input: 'index.js',
plugins: [
glsl(),
wgsl(),
resolve({
browser: true,
preferBuiltins: false
}),
commonjs({
// global keyword handling causes Webpack compatibility issues, so we disabled it:
// https://github.com/mapbox/mapbox-gl-js/pull/6956
ignoreGlobal: true
})
].concat(plugins),
external: ['maptalks', '@maptalks/gl'],
output: {
globals: {
'maptalks': 'maptalks',
'@maptalks/gl': 'maptalks'
},
banner,
outro,
extend: true,
name: 'maptalks',
file: outputESFile,
format: 'es',
sourcemap: true,
},
watch: {
include: ['index.js']
}
}
];