Skip to content

Commit 032719d

Browse files
authored
Add support for more emsdk versions. (#2946)
1 parent 706c3ae commit 032719d

File tree

8 files changed

+90
-10
lines changed

8 files changed

+90
-10
lines changed

CMakeLists.txt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,13 @@ if (WEB)
513513
add_executable(pag ${PAG_FILES})
514514
list(APPEND PAG_LINK_OPTIONS --no-entry -lembind -sEXPORT_NAME='PAGInit' -sWASM=1 -sASYNCIFY
515515
-sMAX_WEBGL_VERSION=2 -sEXPORTED_RUNTIME_METHODS=['GL','Asyncify','HEAPU8'] -sALLOW_MEMORY_GROWTH=1
516-
-sMODULARIZE=1 -sENVIRONMENT='web,worker' -sEXPORT_ES6=1)
517-
if (${EMSCRIPTEN_VERSION} VERSION_LESS "4.0.4")
518-
list(APPEND PAG_LINK_OPTIONS -sUSE_ES6_IMPORT_META=0)
519-
elseif (${EMSCRIPTEN_VERSION} VERSION_GREATER_EQUAL "4.0.4" AND ${EMSCRIPTEN_VERSION} VERSION_LESS "4.0.6")
520-
message(FATAL_ERROR "Emscripten version ${EMSCRIPTEN_VERSION} is not supported. Please upgrade to 4.0.6 or higher.")
521-
endif ()
516+
-sMODULARIZE=1 -sENVIRONMENT=web,worker -sEXPORT_ES6=1)
517+
set(unsupported_emsdk_versions "4.0.11")
518+
foreach (unsupported_version IN LISTS unsupported_emsdk_versions)
519+
if (${EMSCRIPTEN_VERSION} VERSION_EQUAL ${unsupported_version})
520+
message(FATAL_ERROR "Emscripten version ${EMSCRIPTEN_VERSION} is not supported.")
521+
endif ()
522+
endforeach ()
522523
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
523524
list(APPEND PAG_COMPILE_OPTIONS -O0 -g3)
524525
list(APPEND PAG_LINK_OPTIONS -O0 -g3 -sSAFE_HEAP=1 -Wno-limited-postlink-optimizations)

web/script/plugin/replace-config.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,15 @@ export const replaceFunctionConfig = [
284284
},
285285
{
286286
name: 'replace libpag.wasm name',
287-
type: 'string',
288-
start: 'libpag.wasm',
289-
replaceStr: 'libpag.wasm.br',
287+
start: 'var wasmBinaryFile;',
288+
end: 'function getBinary(file)',
289+
replaceStr:`
290+
var wasmBinaryFile;
291+
wasmBinaryFile = "libpag.wasm.br";
292+
if (!isDataURI(wasmBinaryFile)) {
293+
wasmBinaryFile = locateFile(wasmBinaryFile);
294+
}
295+
`,
290296
},
291297
{
292298
name: 'fix get gl get framebuffer',
@@ -304,4 +310,10 @@ export const replaceFunctionConfig = [
304310
start: `if (!ext.includes("lose_context") && !ext.includes("debug"))`,
305311
replaceStr: `if (!ext.includes("lose_context") && !ext.includes("debug") && !ext.includes("WEBGL_webcodecs_video_frame"))`,
306312
},
313+
{
314+
name: 'replace _scriptDir',
315+
type: 'string',
316+
start: `var _scriptDir = import.meta.url;`,
317+
replaceStr: `var _scriptDir = typeof document !== "undefined" && document.currentScript ? document.currentScript.src : void 0;`,
318+
},
307319
];

web/script/rollup.config.dev.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ const plugins = [
1717
alias({
1818
entries: [{ find: '@tgfx', replacement: path.resolve(__dirname, '../../third_party/tgfx/web/src') }],
1919
}),
20+
{
21+
name: 'preserve-import-meta-url',
22+
resolveImportMeta(property, options) {
23+
// Preserve the original behavior of `import.meta.url`.
24+
if (property === 'url') {
25+
return 'import.meta.url';
26+
}
27+
return null;
28+
},
29+
},
2030
];
2131

2232
export default [

web/script/rollup.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ const plugins = [
1919
alias({
2020
entries: [{ find: '@tgfx', replacement: path.resolve(__dirname, '../../third_party/tgfx/web/src') }],
2121
}),
22+
{
23+
name: 'preserve-import-meta-url',
24+
resolveImportMeta(property, options) {
25+
// Preserve the original behavior of `import.meta.url`.
26+
if (property === 'url') {
27+
return 'import.meta.url';
28+
}
29+
return null;
30+
},
31+
},
2232
];
2333

2434

web/script/rollup.config.wx.dev.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ export default [
2323
alias({
2424
entries: [{ find: '@tgfx', replacement: path.resolve(__dirname, '../../third_party/tgfx/web/src') }],
2525
}),
26+
{
27+
name: 'preserve-import-meta-url',
28+
resolveImportMeta(property, options) {
29+
// Preserve the original behavior of `import.meta.url`.
30+
if (property === 'url') {
31+
return 'import.meta.url';
32+
}
33+
return null;
34+
},
35+
},
2636
],
2737
},
2838
];

web/script/rollup.config.wx.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ export default [
2121
alias({
2222
entries: [{ find: '@tgfx', replacement: path.resolve(__dirname, '../../third_party/tgfx/web/src') }],
2323
}),
24+
{
25+
name: 'preserve-import-meta-url',
26+
resolveImportMeta(property, options) {
27+
// Preserve the original behavior of `import.meta.url`.
28+
if (property === 'url') {
29+
return 'import.meta.url';
30+
}
31+
return null;
32+
},
33+
},
2434
],
2535
},
2636
];

web/script/setup.emsdk.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const path = require('path');
2+
const Utils = require("../../third_party/vendor_tools/lib/Utils");
3+
4+
const emsdkPath = path.resolve(__dirname, '../../third_party/emsdk');
5+
const emscriptenPath = path.resolve(emsdkPath, 'upstream/emscripten');
6+
process.env.PATH = process.platform === 'win32'
7+
? `${emsdkPath};${emscriptenPath};${process.env.PATH}`
8+
: `${emsdkPath}:${emscriptenPath}:${process.env.PATH}`;
9+
10+
Utils.exec("emsdk install latest", emsdkPath);
11+
Utils.exec("emsdk activate latest", emsdkPath);
12+
13+
const emsdkEnv = process.platform === 'win32' ? "emsdk_env.bat" : "source emsdk_env.sh";
14+
let result = Utils.execSafe(emsdkEnv, emsdkPath);
15+
let lines = result.split("\n");
16+
for (let line of lines) {
17+
let values = line.split("=");
18+
if (values.length > 1) {
19+
process.stdout.write(line);
20+
let key = values[0].trim();
21+
process.env[key] = values[1].trim();
22+
}
23+
}

web/src/worker/client.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ export const createPAGWorker = (pagWorkerOptions: PAGWorkerOptions = {}) => {
3030
if (pagWorkerOptions.locateFile) {
3131
option.fileUrl = pagWorkerOptions.locateFile('libpag.wasm');
3232
}
33-
const worker = new Worker(scriptUrl, pagWorkerOptions.workerOptions);
33+
const workerOptions = pagWorkerOptions.workerOptions || {};
34+
const worker = new Worker(scriptUrl, {
35+
...workerOptions,
36+
type: 'module'
37+
});
3438
return postMessage(worker, { name: WorkerMessageType.PAGInit, args: [option] }, () => {
3539
addGlobalWorkerListener(worker);
3640
return worker;

0 commit comments

Comments
 (0)