-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathplatfromize.js
116 lines (106 loc) · 2.77 KB
/
platfromize.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
import path from 'path';
import inject from '@rollup/plugin-inject';
import { REVISION } from '../three/src/constants.js';
export const platformVariables = [
'URL',
'atob',
'Blob',
'window',
'document',
'DOMParser',
'TextDecoder',
'XMLHttpRequest',
'OffscreenCanvas',
'HTMLCanvasElement',
'createImageBitmap',
'requestAnimationFrame',
];
export function platformize(
list = platformVariables,
platformPath = path
.resolve(__dirname, '../src/Platform')
.replaceAll('\\', '\\\\'),
) {
return inject({
exclude: /src\/platforms/,
'self.URL': [platformPath, '$URL'],
...list.reduce((acc, curr) => {
acc[curr] = [platformPath, `$${curr}`];
return acc;
}, {}),
});
}
export function injectPlatformCode() {
return {
transform(code, filePath) {
if (filePath.endsWith('Three.js')) {
code += "\nexport * from '../../src/Platform.js';\n";
}
if (filePath.endsWith('WebGLExtensions.js')) {
code =
`import { $defaultWebGLExtensions } from "../../../../src/Platform.js"\n` +
code;
code = code.replace(
'const extensions = {};',
`const extensions = Object.assign( {}, $defaultWebGLExtensions || {});`,
);
code = code.replace(
'var extensions = {};',
`var extensions = Object.assign( {}, $defaultWebGLExtensions || {});`,
);
// 淘宝小程序ios下对于不支持的扩展返回undefined
code = code.replace(
`const extension = getExtension( name );`,
`const extension = getExtension( name ) || null;`,
);
}
return {
code: code,
map: null,
};
},
};
}
export function importFromPlatfromizedThree() {
return {
transform(code, filePath) {
// code = code.replaceAll(
// /from.*('|").*three.module.js('|")/g,
// `from '${path
// .resolve(__dirname, '../build/three.module.js')
// .replaceAll('\\', '\\\\')}'`,
// );
if (REVISION >= 129) {
code = code.replaceAll(
`from 'three'`,
`from '${path
.relative(
path
.dirname(filePath)
.replace(path.sep + 'three' + path.sep, path.sep),
path.resolve(__dirname, '../build/three.module.js'),
)
.replaceAll(path.sep, '/')}'`,
);
}
return {
code: code,
map: null,
};
},
};
}
export function useRealOpentypeModule() {
return {
transform(code) {
code = code.replace(
`import { opentype } from '../libs/opentype.module.min.js'`,
`import opentype from '../libs/opentype.module.js'`,
);
return {
code: code,
map: null,
};
},
};
}