-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathvite.config.ts
More file actions
79 lines (74 loc) · 2.02 KB
/
vite.config.ts
File metadata and controls
79 lines (74 loc) · 2.02 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
import { defineConfig } from 'vite';
import replace from '@rollup/plugin-replace';
import { readFileSync } from 'fs';
import path from 'path';
const globalDefine = { global: 'globalThis' };
// Toggle the booleans here to enable / disable Phaser 3 features:
const phaserFeatureFlags = {
'typeof CANVAS_RENDERER': JSON.stringify(true),
'typeof WEBGL_RENDERER': JSON.stringify(true),
'typeof WEBGL_DEBUG': JSON.stringify(false),
'typeof EXPERIMENTAL': JSON.stringify(true),
'typeof PLUGIN_CAMERA3D': JSON.stringify(false),
'typeof PLUGIN_FBINSTANT': JSON.stringify(false),
'typeof FEATURE_SOUND': JSON.stringify(true)
};
const applyPhaserFeatureFlags = (code: string): string => {
let next = code;
for (const [key, value] of Object.entries(phaserFeatureFlags)) {
if (next.includes(key)) {
next = next.split(key).join(value);
}
}
return next;
};
const phaserSourceFile =
/node_modules[\/\\]phaser[\/\\]src[\/\\].*\.js$/;
const phaserSourceEsbuildPlugin = {
name: 'phaser-source-transform',
setup(build: { onLoad: Function }) {
build.onLoad(
{
filter: phaserSourceFile
},
(args: { path: string }) => {
const code = readFileSync(args.path, 'utf8');
const replaced = applyPhaserFeatureFlags(code);
if (replaced !== code) {
return {
contents: replaced,
loader: 'js'
};
}
return null;
}
);
}
};
const phaserReplace = replace({
preventAssignment: true,
values: phaserFeatureFlags,
include: ['**/node_modules/phaser/src/**/*.js']
});
phaserReplace.enforce = 'pre';
export default defineConfig({
define: globalDefine,
optimizeDeps: {
esbuildOptions: {
define: globalDefine,
plugins: [phaserSourceEsbuildPlugin]
}
},
plugins: [
phaserReplace
],
resolve: {
alias: {
// Use Phaser source build so feature flags can be tree-shaken.
phaser: path.resolve(__dirname, 'node_modules/phaser/src/phaser.js')
}
},
build: {
minify: 'terser'
}
});