@cocos/ccbuild 1.1.17 → 1.1.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +37 -37
  3. package/lib/api-builder.js +1 -1
  4. package/lib/build-engine/engine-js/index.d.ts +2 -2
  5. package/lib/build-engine/engine-js/index.js +406 -406
  6. package/lib/build-engine/engine-js/rollup-plugins/external-wasm-loader.d.ts +57 -57
  7. package/lib/build-engine/engine-js/rollup-plugins/external-wasm-loader.d.ts.map +1 -1
  8. package/lib/build-engine/engine-js/rollup-plugins/external-wasm-loader.js +326 -298
  9. package/lib/build-engine/engine-js/rollup-plugins/external-wasm-loader.js.map +1 -1
  10. package/lib/build-engine/engine-js/rollup-plugins/remove-deprecated-features.d.ts +2 -2
  11. package/lib/build-engine/engine-js/rollup-plugins/remove-deprecated-features.js +37 -37
  12. package/lib/build-engine/engine-js/rollup-plugins/ts-paths.d.ts +4 -4
  13. package/lib/build-engine/engine-js/rollup-plugins/ts-paths.js +42 -42
  14. package/lib/build-engine/engine-ts/engine-builder.d.ts +57 -57
  15. package/lib/build-engine/engine-ts/engine-builder.js +631 -631
  16. package/lib/build-engine/engine-ts/field-decorator-helper.d.ts +9 -9
  17. package/lib/build-engine/engine-ts/field-decorator-helper.js +54 -54
  18. package/lib/build-engine/engine-ts/index.d.ts +2 -2
  19. package/lib/build-engine/engine-ts/index.js +36 -36
  20. package/lib/build-engine/engine-ts/plugins/external-wasm-loader.d.ts +6 -6
  21. package/lib/build-engine/engine-ts/plugins/external-wasm-loader.js +65 -65
  22. package/lib/build-engine/engine-ts/plugins/interface.d.ts +20 -20
  23. package/lib/build-engine/engine-ts/plugins/interface.js +1 -1
  24. package/lib/build-engine/index.d.ts +145 -145
  25. package/lib/build-engine/index.d.ts.map +1 -1
  26. package/lib/build-engine/index.js +167 -167
  27. package/lib/build-engine/index.js.map +1 -1
  28. package/lib/index.d.ts +4 -4
  29. package/lib/index.js +33 -33
  30. package/lib/module-resolver.d.ts +7 -7
  31. package/lib/module-resolver.js +28 -28
  32. package/lib/stats-query/config-interface.d.ts +129 -129
  33. package/lib/stats-query/config-interface.js +3 -3
  34. package/lib/stats-query/index.d.ts +183 -183
  35. package/lib/stats-query/index.js +531 -531
  36. package/lib/stats-query/path-utils.d.ts +2 -2
  37. package/lib/stats-query/path-utils.js +35 -35
  38. package/lib/transformer/babel.d.ts +5 -5
  39. package/lib/transformer/babel.js +34 -34
  40. package/lib/transformer/index.d.ts +2 -2
  41. package/lib/transformer/index.js +28 -28
  42. package/lib/utils.d.ts +4 -4
  43. package/lib/utils.js +23 -23
  44. package/package.json +1 -1
  45. package/static/helper-dynamic-constants.txt +13 -13
  46. package/static/helper-file-decorator.ts +20 -20
  47. package/static/helper-global-exporter.txt +8 -8
  48. package/static/lib.dom.d.ts +20227 -20227
@@ -1,299 +1,327 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
- return new (P || (P = Promise))(function (resolve, reject) {
28
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
- step((generator = generator.apply(thisArg, _arguments || [])).next());
32
- });
33
- };
34
- var __importDefault = (this && this.__importDefault) || function (mod) {
35
- return (mod && mod.__esModule) ? mod : { "default": mod };
36
- };
37
- Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.pathToAssetRefURL = exports.externalWasmLoader = void 0;
39
- const url_1 = require("url");
40
- const fs_extra_1 = __importDefault(require("fs-extra"));
41
- const path_1 = __importDefault(require("path"));
42
- const babel = __importStar(require("@babel/core"));
43
- // @ts-ignore
44
- const plugin_transform_modules_systemjs_1 = __importDefault(require("@babel/plugin-transform-modules-systemjs"));
45
- const externalOrigin = 'external:';
46
- function normalizePath(path) {
47
- return path.replace(/\\/g, '/');
48
- }
49
- const suffixReplaceConfig = {
50
- '.mem': '.mem.bin',
51
- '.wasm.fallback': '.wasm.fallback.bin',
52
- };
53
- /**
54
- * emit asset and return the export statement
55
- */
56
- function emitAsset(context, filePath) {
57
- return __awaiter(this, void 0, void 0, function* () {
58
- let basename = path_1.default.basename(filePath);
59
- for (let suffixToReplace in suffixReplaceConfig) {
60
- const replacement = suffixReplaceConfig[suffixToReplace];
61
- if (basename.endsWith(suffixToReplace)) {
62
- // some platforms doesn't support files with special suffix like '.mem', we replace it to '.bin'
63
- basename = basename.slice(0, -suffixToReplace.length) + replacement;
64
- }
65
- }
66
- const referenceId = context.emitFile({
67
- type: 'asset',
68
- name: basename,
69
- // fileName: path,
70
- source: yield fs_extra_1.default.readFile(filePath),
71
- });
72
- return `export default import.meta.ROLLUP_FILE_URL_${referenceId};`;
73
- });
74
- }
75
- function shouldCullBulletWasmModule(options, id) {
76
- return options.forceBanningBulletWasm && id.includes('bullet');
77
- }
78
- function shouldCullAsmJsModule(options, id) {
79
- return options.wasmSupportMode !== 0 && options.cullAsmJsModule;
80
- }
81
- const loadConfig = {
82
- '.wasm': {
83
- shouldCullModule(options, id) {
84
- return options.wasmSupportMode === 0 || shouldCullBulletWasmModule(options, id);
85
- },
86
- shouldEmitAsset(options, id) {
87
- return !this.shouldCullModule(options, id);
88
- },
89
- cullingContent: `export default '';`,
90
- },
91
- '.js.mem': {
92
- shouldCullModule(options, id) {
93
- return (options.wasmSupportMode === 1 && !shouldCullBulletWasmModule(options, id)) || shouldCullAsmJsModule(options, id);
94
- },
95
- shouldEmitAsset(options, id) {
96
- return !this.shouldCullModule(options, id);
97
- },
98
- cullingContent: `export default '';`,
99
- },
100
- '.wasm.js': {
101
- shouldCullModule(options, id) {
102
- return options.wasmSupportMode === 0 || shouldCullBulletWasmModule(options, id);
103
- },
104
- shouldEmitAsset(options, id) {
105
- return false;
106
- },
107
- cullingContent: `export default function () {}`,
108
- },
109
- '.asm.js': {
110
- shouldCullModule(options, id) {
111
- return (options.wasmSupportMode === 1 && !shouldCullBulletWasmModule(options, id)) || shouldCullAsmJsModule(options, id);
112
- },
113
- shouldEmitAsset(options, id) {
114
- return false;
115
- },
116
- cullingContent: `export default function () {}`,
117
- },
118
- '.wasm.fallback': {
119
- shouldCullModule(options, id) {
120
- return loadConfig['.wasm'].shouldCullModule(options, id) || !options.wasmFallback;
121
- },
122
- shouldEmitAsset(options, id) {
123
- return !this.shouldCullModule(options, id);
124
- },
125
- cullingContent: `export default '';`,
126
- },
127
- '.wasm.fallback.js': {
128
- shouldCullModule(options, id) {
129
- return loadConfig['.wasm.js'].shouldCullModule(options, id) || !options.wasmFallback;
130
- },
131
- shouldEmitAsset(options, id) {
132
- return false;
133
- },
134
- cullingContent: `export default function () {}`,
135
- },
136
- };
137
- /**
138
- * This is a module bundler for minigame subpacakge.
139
- * We need an entry script called 'game.js' for each subpackage.
140
- */
141
- class ExternalWasmModuleBundler {
142
- constructor(options) {
143
- this._loadedChunkMap = {}; // id to code
144
- this._options = options;
145
- }
146
- _resolveId(source) {
147
- let id = normalizePath(path_1.default.join(this._options.externalRoot, source.substring(externalOrigin.length)));
148
- return id;
149
- }
150
- _load(id) {
151
- for (const suffix in loadConfig) {
152
- if (id.endsWith(suffix)) {
153
- const config = loadConfig[suffix];
154
- if (config.shouldCullModule(this._options, id)) {
155
- return config.cullingContent;
156
- }
157
- else if (config.shouldEmitAsset(this._options, id)) {
158
- return this._emitAsset(id);
159
- }
160
- else {
161
- return fs_extra_1.default.readFileSync(id, 'utf8');
162
- }
163
- }
164
- }
165
- // fallback
166
- return fs_extra_1.default.readFileSync(id, 'utf8');
167
- }
168
- _emitAsset(id) {
169
- let basename = path_1.default.basename(id);
170
- for (let suffixToReplace in suffixReplaceConfig) {
171
- const replacement = suffixReplaceConfig[suffixToReplace];
172
- if (basename.endsWith(suffixToReplace)) {
173
- // some platforms doesn't support files with special suffix like '.mem', we replace it to '.bin'
174
- basename = basename.slice(0, -suffixToReplace.length) + replacement;
175
- }
176
- }
177
- const buffer = fs_extra_1.default.readFileSync(id);
178
- const assetsDir = path_1.default.join(this._options.outDir, 'assets');
179
- fs_extra_1.default.outputFileSync(path_1.default.join(assetsDir, basename), buffer);
180
- // output game.js
181
- const gameJs = path_1.default.join(assetsDir, 'game.js');
182
- if (!fs_extra_1.default.existsSync(gameJs)) {
183
- fs_extra_1.default.outputFileSync(gameJs, `console.log('[CC Subpackage] wasm assets loaded');`, 'utf8');
184
- }
185
- return `export default 'assets/${basename}';`;
186
- }
187
- _transform(id, code) {
188
- const systemJsModuleId = externalOrigin + path_1.default.relative(this._options.externalRoot, id).replace(/\\/g, '/');
189
- const res = babel.transformSync(code, {
190
- compact: true,
191
- plugins: [
192
- [plugin_transform_modules_systemjs_1.default, {
193
- moduleId: systemJsModuleId,
194
- }],
195
- ],
196
- });
197
- code = res.code;
198
- return code;
199
- }
200
- bundle() {
201
- // transform all external wasm modules
202
- for (const externalWasmModule of this._options.externalWasmModules) {
203
- const id = this._resolveId(externalWasmModule);
204
- const code = this._load(id);
205
- this._loadedChunkMap[id] = this._transform(id, code);
206
- }
207
- // bundle
208
- let result = [`console.log('[CC Subpackage] wasm chunks loaded');`];
209
- for (let id in this._loadedChunkMap) {
210
- const code = this._loadedChunkMap[id];
211
- result.push(code);
212
- }
213
- return result.join('\n');
214
- }
215
- }
216
- /**
217
- * This plugin enable to load script or wasm with url based on 'external://' origin.
218
- */
219
- function externalWasmLoader(options) {
220
- const externalWasmModules = [];
221
- return {
222
- name: '@cocos/ccbuild|external-loader',
223
- resolveId(source, importer) {
224
- return __awaiter(this, void 0, void 0, function* () {
225
- if (source.startsWith(externalOrigin)) {
226
- if (options.wasmSubpackage) {
227
- externalWasmModules.push(source);
228
- return {
229
- id: source,
230
- external: true,
231
- };
232
- }
233
- return source;
234
- }
235
- return null;
236
- });
237
- },
238
- load(id) {
239
- return __awaiter(this, void 0, void 0, function* () {
240
- if (id.startsWith(externalOrigin)) {
241
- let filePath = normalizePath(path_1.default.join(options.externalRoot, id.substring(externalOrigin.length)));
242
- for (const suffix in loadConfig) {
243
- if (filePath.endsWith(suffix)) {
244
- const config = loadConfig[suffix];
245
- if (config.shouldCullModule(options, id)) {
246
- return config.cullingContent;
247
- }
248
- else if (config.shouldEmitAsset(options, id)) {
249
- return emitAsset(this, filePath);
250
- }
251
- else {
252
- return yield fs_extra_1.default.readFile(filePath, 'utf8');
253
- }
254
- }
255
- }
256
- // some external module that doesn't obey the suffix specification, we return its content by default.
257
- return yield fs_extra_1.default.readFile(filePath, 'utf8');
258
- }
259
- return null;
260
- });
261
- },
262
- // Generates the `import.meta.ROLLUP_FILE_URL_referenceId`.
263
- resolveFileUrl({
264
- // > The path and file name of the emitted asset, relative to `output.dir` without a leading `./`.
265
- fileName,
266
- // > The path and file name of the emitted file,
267
- // > relative to the chunk the file is referenced from.
268
- // > This will path will contain no leading `./` but may contain a leading `../`.
269
- relativePath, }) {
270
- switch (options.format) {
271
- case 'relative-from-chunk':
272
- return `'${relativePath}'`;
273
- case 'relative-from-out':
274
- return `'${fileName}'`;
275
- case 'runtime-resolved':
276
- default:
277
- return undefined; // return `new URL('${fileName}', import.meta.url).href`;
278
- }
279
- },
280
- generateBundle(opts, bundles) {
281
- if (externalWasmModules.length !== 0) {
282
- const bundler = new ExternalWasmModuleBundler(Object.assign(Object.assign({}, options), { externalWasmModules, outDir: opts.dir }));
283
- const code = bundler.bundle();
284
- fs_extra_1.default.outputFileSync(path_1.default.join(opts.dir, 'chunks/game.js'), code, 'utf8');
285
- }
286
- },
287
- };
288
- }
289
- exports.externalWasmLoader = externalWasmLoader;
290
- /**
291
- * Convert the file path to asset ref URL.
292
- * @param file File path in absolute.
293
- */
294
- function pathToAssetRefURL(file) {
295
- return `${assetPrefix}${(0, url_1.pathToFileURL)(file).pathname}`;
296
- }
297
- exports.pathToAssetRefURL = pathToAssetRefURL;
298
- const assetPrefix = 'asset:';
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ exports.pathToAssetRefURL = exports.externalWasmLoader = void 0;
39
+ const rollup = __importStar(require("rollup"));
40
+ const plugin_commonjs_1 = __importDefault(require("@rollup/plugin-commonjs"));
41
+ const url_1 = require("url");
42
+ const fs_extra_1 = __importDefault(require("fs-extra"));
43
+ const path_1 = __importDefault(require("path"));
44
+ const babel = __importStar(require("@babel/core"));
45
+ // @ts-ignore
46
+ const plugin_transform_modules_systemjs_1 = __importDefault(require("@babel/plugin-transform-modules-systemjs"));
47
+ const externalOrigin = 'external:';
48
+ function normalizePath(path) {
49
+ return path.replace(/\\/g, '/');
50
+ }
51
+ const suffixReplaceConfig = {
52
+ '.mem': '.mem.bin',
53
+ '.wasm.fallback': '.wasm.fallback.bin',
54
+ };
55
+ /**
56
+ * emit asset and return the export statement
57
+ */
58
+ function emitAsset(context, filePath) {
59
+ return __awaiter(this, void 0, void 0, function* () {
60
+ let basename = path_1.default.basename(filePath);
61
+ for (let suffixToReplace in suffixReplaceConfig) {
62
+ const replacement = suffixReplaceConfig[suffixToReplace];
63
+ if (basename.endsWith(suffixToReplace)) {
64
+ // some platforms doesn't support files with special suffix like '.mem', we replace it to '.bin'
65
+ basename = basename.slice(0, -suffixToReplace.length) + replacement;
66
+ }
67
+ }
68
+ const referenceId = context.emitFile({
69
+ type: 'asset',
70
+ name: basename,
71
+ // fileName: path,
72
+ source: yield fs_extra_1.default.readFile(filePath),
73
+ });
74
+ return `export default import.meta.ROLLUP_FILE_URL_${referenceId};`;
75
+ });
76
+ }
77
+ function shouldCullBulletWasmModule(options, id) {
78
+ return options.forceBanningBulletWasm && id.includes('bullet');
79
+ }
80
+ function shouldCullAsmJsModule(options, id) {
81
+ return options.wasmSupportMode !== 0 && options.cullAsmJsModule;
82
+ }
83
+ const loadConfig = {
84
+ '.wasm': {
85
+ shouldCullModule(options, id) {
86
+ return options.wasmSupportMode === 0 || shouldCullBulletWasmModule(options, id);
87
+ },
88
+ shouldEmitAsset(options, id) {
89
+ return !this.shouldCullModule(options, id);
90
+ },
91
+ cullingContent: `export default '';`,
92
+ },
93
+ '.js.mem': {
94
+ shouldCullModule(options, id) {
95
+ return (options.wasmSupportMode === 1 && !shouldCullBulletWasmModule(options, id)) || shouldCullAsmJsModule(options, id);
96
+ },
97
+ shouldEmitAsset(options, id) {
98
+ return !this.shouldCullModule(options, id);
99
+ },
100
+ cullingContent: `export default '';`,
101
+ },
102
+ '.wasm.js': {
103
+ shouldCullModule(options, id) {
104
+ return options.wasmSupportMode === 0 || shouldCullBulletWasmModule(options, id);
105
+ },
106
+ shouldEmitAsset(options, id) {
107
+ return false;
108
+ },
109
+ cullingContent: `export default function () {}`,
110
+ },
111
+ '.asm.js': {
112
+ shouldCullModule(options, id) {
113
+ return (options.wasmSupportMode === 1 && !shouldCullBulletWasmModule(options, id)) || shouldCullAsmJsModule(options, id);
114
+ },
115
+ shouldEmitAsset(options, id) {
116
+ return false;
117
+ },
118
+ cullingContent: `export default function () {}`,
119
+ },
120
+ '.wasm.fallback': {
121
+ shouldCullModule(options, id) {
122
+ return loadConfig['.wasm'].shouldCullModule(options, id) || !options.wasmFallback;
123
+ },
124
+ shouldEmitAsset(options, id) {
125
+ return !this.shouldCullModule(options, id);
126
+ },
127
+ cullingContent: `export default '';`,
128
+ },
129
+ '.wasm.fallback.js': {
130
+ shouldCullModule(options, id) {
131
+ return loadConfig['.wasm.js'].shouldCullModule(options, id) || !options.wasmFallback;
132
+ },
133
+ shouldEmitAsset(options, id) {
134
+ return false;
135
+ },
136
+ cullingContent: `export default function () {}`,
137
+ },
138
+ };
139
+ /**
140
+ * This is a module bundler for minigame subpacakge.
141
+ * We need an entry script called 'game.js' for each subpackage.
142
+ */
143
+ class ExternalWasmModuleBundler {
144
+ constructor(options) {
145
+ this._loadedChunkMap = {}; // id to code
146
+ this._options = options;
147
+ }
148
+ _resolveId(source) {
149
+ let id = normalizePath(path_1.default.join(this._options.externalRoot, source.substring(externalOrigin.length)));
150
+ return id;
151
+ }
152
+ _load(id) {
153
+ return __awaiter(this, void 0, void 0, function* () {
154
+ for (const suffix in loadConfig) {
155
+ if (id.endsWith(suffix)) {
156
+ const config = loadConfig[suffix];
157
+ if (config.shouldCullModule(this._options, id)) {
158
+ return config.cullingContent;
159
+ }
160
+ else if (config.shouldEmitAsset(this._options, id)) {
161
+ return this._emitAsset(id);
162
+ }
163
+ else {
164
+ return yield this._transformSystemJs(id);
165
+ }
166
+ }
167
+ }
168
+ // fallback
169
+ return yield this._transformSystemJs(id);
170
+ });
171
+ }
172
+ // NOTE: we use rollup to transform CommonJS / ES Module to SystemJs.
173
+ _transformSystemJs(id) {
174
+ return __awaiter(this, void 0, void 0, function* () {
175
+ const systemJsModuleId = externalOrigin + path_1.default.relative(this._options.externalRoot, id).replace(/\\/g, '/');
176
+ const res = yield rollup.rollup({
177
+ input: id,
178
+ plugins: [(0, plugin_commonjs_1.default)()],
179
+ });
180
+ const output = yield res.generate({
181
+ format: 'system',
182
+ name: systemJsModuleId,
183
+ });
184
+ yield res.close();
185
+ return output.output[0].code;
186
+ });
187
+ }
188
+ _emitAsset(id) {
189
+ let basename = path_1.default.basename(id);
190
+ for (let suffixToReplace in suffixReplaceConfig) {
191
+ const replacement = suffixReplaceConfig[suffixToReplace];
192
+ if (basename.endsWith(suffixToReplace)) {
193
+ // some platforms doesn't support files with special suffix like '.mem', we replace it to '.bin'
194
+ basename = basename.slice(0, -suffixToReplace.length) + replacement;
195
+ }
196
+ }
197
+ const buffer = fs_extra_1.default.readFileSync(id);
198
+ const assetsDir = path_1.default.join(this._options.outDir, 'assets');
199
+ fs_extra_1.default.outputFileSync(path_1.default.join(assetsDir, basename), buffer);
200
+ // output game.js
201
+ const gameJs = path_1.default.join(assetsDir, 'game.js');
202
+ if (!fs_extra_1.default.existsSync(gameJs)) {
203
+ fs_extra_1.default.outputFileSync(gameJs, `console.log('[CC Subpackage] wasm assets loaded');`, 'utf8');
204
+ }
205
+ return `export default 'assets/${basename}';`;
206
+ }
207
+ _transform(id, code) {
208
+ if (code.startsWith('System.register')) {
209
+ // NOTE: if it's already SystemJS module, we don't need to transform it again.
210
+ return code;
211
+ }
212
+ const systemJsModuleId = externalOrigin + path_1.default.relative(this._options.externalRoot, id).replace(/\\/g, '/');
213
+ const res = babel.transformSync(code, {
214
+ compact: true,
215
+ plugins: [
216
+ [plugin_transform_modules_systemjs_1.default, {
217
+ moduleId: systemJsModuleId,
218
+ }],
219
+ ],
220
+ });
221
+ code = res.code;
222
+ return code;
223
+ }
224
+ bundle() {
225
+ return __awaiter(this, void 0, void 0, function* () {
226
+ // transform all external wasm modules
227
+ for (const externalWasmModule of this._options.externalWasmModules) {
228
+ const id = this._resolveId(externalWasmModule);
229
+ const code = yield this._load(id);
230
+ this._loadedChunkMap[id] = this._transform(id, code);
231
+ }
232
+ // bundle
233
+ let result = [`console.log('[CC Subpackage] wasm chunks loaded');`];
234
+ for (let id in this._loadedChunkMap) {
235
+ const code = this._loadedChunkMap[id];
236
+ result.push(code);
237
+ }
238
+ return result.join('\n');
239
+ });
240
+ }
241
+ }
242
+ /**
243
+ * This plugin enable to load script or wasm with url based on 'external://' origin.
244
+ */
245
+ function externalWasmLoader(options) {
246
+ const externalWasmModules = [];
247
+ return {
248
+ name: '@cocos/ccbuild|external-loader',
249
+ resolveId(source, importer) {
250
+ return __awaiter(this, void 0, void 0, function* () {
251
+ if (source.startsWith(externalOrigin)) {
252
+ if (options.wasmSubpackage) {
253
+ externalWasmModules.push(source);
254
+ return {
255
+ id: source,
256
+ external: true,
257
+ };
258
+ }
259
+ return source;
260
+ }
261
+ return null;
262
+ });
263
+ },
264
+ load(id) {
265
+ return __awaiter(this, void 0, void 0, function* () {
266
+ if (id.startsWith(externalOrigin)) {
267
+ let filePath = normalizePath(path_1.default.join(options.externalRoot, id.substring(externalOrigin.length)));
268
+ for (const suffix in loadConfig) {
269
+ if (filePath.endsWith(suffix)) {
270
+ const config = loadConfig[suffix];
271
+ if (config.shouldCullModule(options, id)) {
272
+ return config.cullingContent;
273
+ }
274
+ else if (config.shouldEmitAsset(options, id)) {
275
+ return emitAsset(this, filePath);
276
+ }
277
+ else {
278
+ return yield fs_extra_1.default.readFile(filePath, 'utf8');
279
+ }
280
+ }
281
+ }
282
+ // some external module that doesn't obey the suffix specification, we return its content by default.
283
+ return yield fs_extra_1.default.readFile(filePath, 'utf8');
284
+ }
285
+ return null;
286
+ });
287
+ },
288
+ // Generates the `import.meta.ROLLUP_FILE_URL_referenceId`.
289
+ resolveFileUrl({
290
+ // > The path and file name of the emitted asset, relative to `output.dir` without a leading `./`.
291
+ fileName,
292
+ // > The path and file name of the emitted file,
293
+ // > relative to the chunk the file is referenced from.
294
+ // > This will path will contain no leading `./` but may contain a leading `../`.
295
+ relativePath, }) {
296
+ switch (options.format) {
297
+ case 'relative-from-chunk':
298
+ return `'${relativePath}'`;
299
+ case 'relative-from-out':
300
+ return `'${fileName}'`;
301
+ case 'runtime-resolved':
302
+ default:
303
+ return undefined; // return `new URL('${fileName}', import.meta.url).href`;
304
+ }
305
+ },
306
+ generateBundle(opts, bundles) {
307
+ return __awaiter(this, void 0, void 0, function* () {
308
+ if (externalWasmModules.length !== 0) {
309
+ const bundler = new ExternalWasmModuleBundler(Object.assign(Object.assign({}, options), { externalWasmModules, outDir: opts.dir }));
310
+ const code = yield bundler.bundle();
311
+ fs_extra_1.default.outputFileSync(path_1.default.join(opts.dir, 'chunks/game.js'), code, 'utf8');
312
+ }
313
+ });
314
+ },
315
+ };
316
+ }
317
+ exports.externalWasmLoader = externalWasmLoader;
318
+ /**
319
+ * Convert the file path to asset ref URL.
320
+ * @param file File path in absolute.
321
+ */
322
+ function pathToAssetRefURL(file) {
323
+ return `${assetPrefix}${(0, url_1.pathToFileURL)(file).pathname}`;
324
+ }
325
+ exports.pathToAssetRefURL = pathToAssetRefURL;
326
+ const assetPrefix = 'asset:';
299
327
  //# sourceMappingURL=external-wasm-loader.js.map