-
Notifications
You must be signed in to change notification settings - Fork 24
/
rollup.config.js
223 lines (179 loc) · 5.48 KB
/
rollup.config.js
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
// Can't use official plugin because: https://github.com/rollup/plugins/issues/287 >:-(
// import typescript from '@rollup/plugin-typescript';
import copy from 'rollup-plugin-copy';
import del from 'rollup-plugin-delete';
import dirTree from 'directory-tree';
import filesize from 'rollup-plugin-filesize';
import fs from 'fs-extra';
import resolve from '@rollup/plugin-node-resolve';
import { terser } from 'rollup-plugin-terser';
import typescript from 'rollup-plugin-typescript2';
// Another crashing plugin that used to work (and hopefully will again!):
// import visualizer from 'rollup-plugin-visualizer';
const extensions = [
'.js', '.jsx', '.ts', '.tsx'
];
const licenseInfo =
`/**
* @author Richard Davey <[email protected]>
* @copyright 2020 Photon Storm Ltd.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/`;
/**
* Custom Roll-up Plugin that copies the package.json version to the dist package.json
*/
const DistPackagePlugin = {
name: 'DistPackagePlugin',
generateBundle () {
const devPackage = fs.readJsonSync('./package.json');
const distPackage = fs.readJsonSync('./dist.package.json');
distPackage.version = devPackage.version;
fs.writeJsonSync('./dist/package.json', distPackage, { spaces: 4 });
}
};
const filterConfig = {
extensions: /\.ts/,
exclude: [
/src\\stats/,
/src\/stats/
]
};
const ESMInputBundle = {};
dirTree('src', filterConfig, (item, path) =>
{
/*
item structure:
{ path: 'src\\utils\\base64\\Base64ToArrayBuffer.ts',
name: 'Base64ToArrayBuffer.ts',
size: 2019,
extension: '.ts',
type: 'file' }
*/
// First we need to see if this is an interface and bail out
// The quickest test is if the filename starts with a capital I:
if (item.name.substring(0, 1) === 'I')
{
// Now we need to actually check inside the file *sigh*
const fileContents = fs.readFileSync(item.path, 'utf8');
if (fileContents.includes('export interface') || fileContents.includes('export type'))
{
// console.log('Ignoring interface ' + item.name);
return;
}
}
const itemPath = path.dirname(item.path);
// Take the item path and put it into an array
const parts = itemPath.split(path.sep);
// Remove the src\ part from the dir array (the first entry)
parts.shift();
// Turn back, always use / as Rollup requires it
const dir = parts.join('/');
const name = item.name.substring(0, item.name.length - item.extension.length);
const entryPoint = (dir === '') ? name : dir + '/' + name;
ESMInputBundle[entryPoint] = item.path;
});
export default [
{
// UMD Bundle
input: './src/index.ts',
output: [
{
file: './dist/Phaser4.js',
format: 'umd',
name: 'Phaser4',
sourcemap: true,
esModule: false,
plugins: [
filesize()
]
}
],
plugins: [
del({
targets: [ './dist', './stats.html' ],
runOnce: true
}),
resolve({
extensions
}),
typescript({
tsconfig: './tsconfig.json'
}),
copy({
targets: [
{ src: 'LICENSE', dest: 'dist', copyOnce: true },
{ src: 'logo.png', dest: 'dist', copyOnce: true },
{ src: 'README.dist.md', dest: 'dist', rename: 'README.md', copyOnce: true }
]
})
// Currently crashing in version 4.0.4, let's hope they fix it
// visualizer({
// "title": "Phaser 4 Package Stats",
// "sourcemap": false,
// "template": "treemap" // "circlepacking"
// })
]
},
{
// UMD Minified Bundle
input: './src/index.ts',
output: [
{
file: './dist/Phaser4.min.js',
format: 'umd',
name: 'Phaser4',
sourcemap: false,
esModule: false,
plugins: [
terser({
output: {
preamble: licenseInfo,
comments: false
}
}),
DistPackagePlugin
]
}
],
plugins: [
resolve({
extensions
}),
typescript({
tsconfig: './tsconfig.json'
})
]
},
{
// ESM Multiple Entry Point Package
input: ESMInputBundle,
onwarn: (warning, next) =>
{
// Because the TypeScript plugin will create d.ts files
// that already exist, so let's not spam the console
// with them.
if (warning.code === 'FILE_NAME_CONFLICT')
{
return;
}
else
{
next(warning);
}
},
plugins: [
resolve({
extensions
}),
typescript({
tsconfig: './tsconfig.json'
})
],
output: [
{
dir: 'dist',
format: 'esm'
}
]
}
];