forked from pixijs/pixijs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
271 lines (250 loc) · 7.93 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
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
import path from 'path';
import resolve from '@rollup/plugin-node-resolve';
import { string } from 'rollup-plugin-string';
import esbuild from 'rollup-plugin-esbuild';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import alias from '@rollup/plugin-alias';
import workspacesRun from 'workspaces-run';
import repo from './package.json' assert { type: 'json' };
const bundleTarget = 'es2017';
const moduleTarget = 'es2020';
/**
* Convert a development file name to minified.
* @param {string} name
*/
function prodName(name)
{
return name.replace(/\.(m)?js$/, '.min.$1js');
}
/**
* Escapes the `RegExp` special characters.
* @param {string} str
*/
function escapeRegExp(str)
{
return str.replace(/[$()*+.?[\\\]^{|}]/g, '\\$&');
}
/**
* Convert the name of a package to a `RegExp` that matches the package's export names.
* @param {string} packageName
*/
function convertPackageNameToRegExp(packageName)
{
return new RegExp(`^${escapeRegExp(packageName)}(/.+)?$`);
}
async function main()
{
const commonPlugins = [
resolve({
browser: true,
preferBuiltins: false,
}),
commonjs(),
json(),
string({
include: [
'**/*.frag',
'**/*.vert',
],
}),
// Import bundle dependencies from the source files
// not from the build lib files, this will make sure
// that conditional stuff works correctly (e.g., process.env.DEBUG)
alias({
entries: [
{ find: 'pixi.js', replacement: './bundles/pixi.js/src/index.ts' },
{ find: /^@pixi\/(.+)$/, replacement: './packages/$1/src/index.ts' },
]
}),
];
const esbuildConfig = {
target: moduleTarget,
minifySyntax: true,
define: {
'process.env.VERSION': `'${repo.version}'`,
'process.env.DEBUG': 'true',
},
treeShaking: true,
tsconfigRaw: '{"compilerOptions":{"useDefineForClassFields":false}}'
}
const plugins = [
esbuild(esbuildConfig),
...commonPlugins,
];
const bundlePlugins = [
esbuild({ ...esbuildConfig, target: bundleTarget }),
...commonPlugins,
];
const bundlePluginsProd = [
esbuild({
...esbuildConfig,
target: bundleTarget,
minify: true,
define: {
...esbuildConfig.define,
'process.env.DEBUG': 'false',
},
}),
...commonPlugins,
];
const results = [];
const packages = [];
// Collect the list of packages
await workspacesRun.default({ cwd: process.cwd(), orderByDeps: true }, async (pkg) =>
{
if (!pkg.config.private)
{
packages.push(pkg);
}
});
packages.forEach((pkg) =>
{
const {
plugin,
pluginExports = true,
bundle,
bundleModule,
dependencies = {},
peerDependencies = {},
} = pkg.config;
// Check for bundle folder
const external = Object.keys(dependencies)
.concat(Object.keys(peerDependencies))
.map(convertPackageNameToRegExp);
const basePath = path.relative(process.cwd(), pkg.dir);
const input = path.join(basePath, 'src/index.ts');
results.push({
input,
output: [
{
dir: path.join(basePath, 'lib'),
entryFileNames: '[name].js',
format: 'cjs',
freeze: false,
sourcemap: true,
preserveModules: true,
preserveModulesRoot: path.join(basePath, 'src'),
exports: 'named',
},
{
dir: path.join(basePath, 'lib'),
entryFileNames: '[name].mjs',
format: 'esm',
freeze: false,
sourcemap: true,
preserveModules: true,
preserveModulesRoot: path.join(basePath, 'src'),
exports: 'named',
},
],
treeshake: false,
external,
plugins,
});
const banner = [
`/*!`,
` * ${pkg.name} - v${repo.version}`,
` * Compiled ${(new Date()).toUTCString().replace(/GMT/g, 'UTC')}`,
` *`,
` * ${pkg.name} is licensed under the MIT License.`,
` * http://www.opensource.org/licenses/mit-license`,
` */`,
].join('\n');
// There are a handful of optional packages that are not included in the bundle
// but we still want to build a browser-based version of them, like we would
// for any external plugin.
if (plugin)
{
const name = pkg.name.replace(/[^a-z]+/g, '_');
const file = path.join(basePath, plugin);
const footer = pluginExports ? `Object.assign(this.PIXI, ${name});` : '';
const nsBanner = pluginExports ? `${banner}\nthis.PIXI = this.PIXI || {};` : banner;
const globals = Object.keys(peerDependencies).reduce((obj, name) => ({ ...obj, [name]: 'PIXI' }), {});
results.push({
input,
output: {
name,
banner: nsBanner,
footer,
file,
format: 'iife',
freeze: false,
sourcemap: true,
globals,
},
treeshake: false,
external,
plugins: bundlePlugins,
}, {
input,
output: {
name,
banner: nsBanner,
footer,
file: prodName(file),
format: 'iife',
freeze: false,
sourcemap: true,
globals,
},
treeshake: false,
external,
plugins: bundlePluginsProd,
});
}
// The package.json file has a bundle field
// we'll use this to generate the bundle file
// this will package all dependencies
if (bundle)
{
const file = path.join(basePath, bundle);
const moduleFile = bundleModule ? path.join(basePath, bundleModule) : '';
results.push({
input,
output: [
{
name: 'PIXI',
banner,
file,
format: 'iife',
freeze: false,
sourcemap: true,
},
{
banner,
file: moduleFile,
format: 'esm',
freeze: false,
sourcemap: true,
}
],
treeshake: false,
plugins: bundlePlugins,
}, {
input,
output: [
{
name: 'PIXI',
banner,
file: prodName(file),
format: 'iife',
freeze: false,
sourcemap: true,
},
{
banner,
file: prodName(moduleFile),
format: 'esm',
freeze: false,
sourcemap: true,
}
],
treeshake: false,
plugins: bundlePluginsProd,
});
}
});
return results;
}
export default main();