-
Notifications
You must be signed in to change notification settings - Fork 217
/
index.mjs
175 lines (163 loc) · 5.46 KB
/
index.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
// from https://github.com/pixijs/extension-scripts, adapted to this monorepo
import path from 'node:path';
import rename from '@pixi/rollup-plugin-rename-node-modules';
import esbuild from 'rollup-plugin-esbuild';
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import { string } from 'rollup-plugin-string';
import replace from '@rollup/plugin-replace';
// function that eats arguments and spits rollup configs
export default (extensionConfig, pkg) => {
const compiled = new Date().toUTCString().replace(/GMT/g, 'UTC');
const banner = [
`/*!`,
` * ${pkg.name} - v${pkg.version}`,
` * Compiled ${compiled}`,
` *`,
` * ${pkg.name} is licensed under the MIT License.`,
` * http://www.opensource.org/licenses/mit-license`,
` * `,
` * Copyright ${new Date().getFullYear()}, ${pkg.author}, All Rights Reserved`,
` */`,
].join('\n');
const pixiPackages = [
'accessibility',
'app',
'assets',
'basis',
'canvas-display',
'canvas-extract',
'canvas-graphics',
'canvas-mesh',
'canvas-particle-container',
'canvas-renderer',
'canvas-prepare',
'canvas-sprite',
'canvas-sprite-tiling',
'canvas-text',
'compressed-textures',
'core',
'display',
'events',
'extensions',
'extract',
'graphics-extras',
'graphics',
'math-extras',
'math',
'mesh-extras',
'mesh',
'mixin-cache-as-bitmap',
'mixin-get-child-by-name',
'mixin-get-global-position',
'particle-container',
'prepare',
'runner',
'settings',
'sprite-animated',
'sprite-tiling',
'sprite',
'spritesheet',
'text-bitmap',
'text',
'ticker',
'unsafe-eval',
];
// Create the PIXI.* namespace for the browser bundle
const builtInPackages = pixiPackages.reduce((acc, name) => ({ ...acc, [`@pixi/${name}`]: 'PIXI' }), {});
// Create the @pixi/* array so we NEVER EVER bundle anything that belongs to pixi
const externalPixi = pixiPackages.map((name) => `@pixi/${name}`);
// External dependencies, not bundled
const externalBrowser = externalPixi.concat(Object.keys(pkg.peerDependencies || {}));
const externalNpm = externalPixi.concat(Object.keys(pkg.peerDependencies || {})).concat(Object.keys(pkg.dependencies || {}));
// Plugins for module and browser output
const plugins = [
commonjs(),
resolve(),
string({
include: ['**/*.frag', '**/*.vert'],
}),
replace({
__VERSION__: pkg.version,
}),
];
// These are the PixiJS built-in default globals
// for the browser bundle when referencing other core packages
const globals = {
...builtInPackages,
...extensionConfig.globals,
};
const source = pkg.source ?? 'src/index.ts';
const basePath = path.dirname(path.join(process.cwd(), source));
const bundle = path.join(process.cwd(), extensionConfig.bundle);
const bundleModule = path.join(process.cwd(), extensionConfig.bundleModule);
const mainDir = path.dirname(path.join(process.cwd(), pkg.main));
const moduleDir = path.dirname(path.join(process.cwd(), pkg.module));
let namespace = extensionConfig.namespace;
let footer;
// If we're adding to the main PIXI namespace, we need to
// make sure we don't override the PIXI global, so we'll do this
// to insert the output of the extension into the PIXI global
if (namespace === 'PIXI') {
namespace = pkg.name.replace(/[^a-z-]/gi, '_').replace(/-/g, '');
footer = `Object.assign(PIXI, ${namespace});`;
}
return [
{
plugins: [...plugins, rename(), esbuild({ target: 'ES2020' })],
external: externalNpm,
input: source,
output: [
{
dir: mainDir,
entryFileNames: '[name].js',
format: 'cjs',
preserveModules: true,
preserveModulesRoot: basePath,
sourcemap: true,
exports: 'named',
},
{
dir: moduleDir,
entryFileNames: '[name].mjs',
format: 'esm',
preserveModules: true,
preserveModulesRoot: basePath,
sourcemap: true,
exports: 'named',
},
],
},
{
plugins: [
...plugins,
esbuild({
target: 'ES2017',
minify: true,
}),
],
external: externalBrowser,
input: source,
treeshake: false,
output: [
{
banner,
file: bundle,
format: 'iife',
name: namespace,
footer,
sourcemap: true,
globals,
exports: 'named',
},
{
banner,
file: bundleModule,
format: 'esm',
sourcemap: true,
exports: 'named',
},
],
},
];
};