forked from pixijs/pixijs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rollup.config.mjs
218 lines (199 loc) · 6.67 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
import path from 'path';
import esbuild from 'rollup-plugin-esbuild';
import externalGlobals from 'rollup-plugin-external-globals';
import jscc from 'rollup-plugin-jscc';
import sourcemaps from 'rollup-plugin-sourcemaps';
import { string } from 'rollup-plugin-string';
import { fileURLToPath } from 'url';
import webworker from '@pixi/webworker-plugins/rollup-plugin';
import repo from './package.json' assert { type: 'json' };
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
const bundleTarget = 'es2017';
const moduleTarget = 'es2020';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
/**
* 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 = [
sourcemaps(),
resolve({
browser: true,
preferBuiltins: false,
}),
commonjs(),
json(),
string({
include: [
'**/*.frag',
'**/*.vert',
'**/*.glsl',
'**/*.wgsl',
],
}),
];
const plugins = [
webworker(),
jscc({ values: { _VERSION: repo.version, _DEBUG: true } }),
esbuild({ target: moduleTarget }),
...commonPlugins
];
const bundlePlugins = [
webworker(),
jscc({ values: { _VERSION: repo.version, _DEBUG: true } }),
esbuild({ target: bundleTarget }),
...commonPlugins
];
const bundlePluginsProd = [
webworker(),
jscc({ values: { _VERSION: repo.version, _DEBUG: false } }),
esbuild({ target: bundleTarget, minify: true }),
...commonPlugins,
];
const results = [];
const {
bundles,
dependencies = {},
peerDependencies = {},
sideEffects
} = repo;
// Check for bundle folder
const external = Object.keys(dependencies)
.concat(Object.keys(peerDependencies))
.map(convertPackageNameToRegExp);
const input = [
...sideEffects.map((name) => path.join(process.cwd(), name.replace('/lib/', '/src/').replace('.*', '.ts'))),
];
results.push({
input,
output: [
{
dir: path.join(process.cwd(), 'lib'),
entryFileNames: '[name].js',
format: 'cjs',
freeze: false,
sourcemap: true,
preserveModules: true,
preserveModulesRoot: path.join(process.cwd(), 'src'),
exports: 'named',
},
{
dir: path.join(process.cwd(), 'lib'),
entryFileNames: '[name].mjs',
format: 'esm',
freeze: false,
sourcemap: true,
preserveModules: true,
preserveModulesRoot: path.join(process.cwd(), 'src'),
exports: 'named',
},
],
treeshake: false,
external,
plugins,
});
const banner = [
`/*!`,
` * PixiJS - v${repo.version}`,
` * Compiled ${(new Date()).toUTCString().replace(/GMT/g, 'UTC')}`,
` *`,
` * PixiJS is licensed under the MIT License.`,
` * http://www.opensource.org/licenses/mit-license`,
` */`,
].join('\n');
// The package.json file has a bundle field
// we'll use this to generate the bundle file
// this will package all dependencies
if (bundles && !process.env.LIB_ONLY)
{
bundles.forEach((bundle, i) =>
{
const file = path.join(process.cwd(), bundle.target);
const moduleFile = bundle.module ? path.join(process.cwd(), bundle.module) : '';
const nsBanner = bundle.plugin ? `${banner}\nthis.PIXI = this.PIXI || {};` : banner;
const name = bundle.plugin ? bundle.target.split('/').at(-1).replace(/[^a-z]+/g, '_') : 'PIXI';
const footer = bundle.plugin ? `Object.assign(this.PIXI, ${name});` : '';
// if a bundle is a plugin then we need to exclude its imports from the bundle
// so they ca nbe added to the global scope
const external = bundle.plugin ? (id) => bundle.plugin.some((plugin) => id.includes(plugin)) : undefined;
// eslint-disable-next-line consistent-return
const externalPlugin = bundle.plugin ? externalGlobals((id) => { if (external(id)) return 'PIXI'; }) : undefined;
results.push({
input: path.join(process.cwd(), bundle.src),
external,
output: [
{
name,
banner: nsBanner,
footer,
file,
format: 'iife',
freeze: false,
sourcemap: true,
},
!bundle.plugin
&& {
banner: nsBanner,
file: moduleFile,
format: 'esm',
freeze: false,
sourcemap: true,
}
],
treeshake: false,
plugins: [...bundlePlugins, externalPlugin],
}, {
input: path.join(process.cwd(), bundle.src),
external,
output: [
{
name,
banner: nsBanner,
footer,
file: prodName(file),
format: 'iife',
freeze: false,
sourcemap: true,
},
!bundle.plugin
&& {
banner: nsBanner,
file: prodName(moduleFile),
format: 'esm',
freeze: false,
sourcemap: true,
}
],
treeshake: false,
plugins: [...bundlePluginsProd, externalPlugin]
});
});
}
return results;
}
export default main();