-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathwasm.js
More file actions
135 lines (120 loc) · 5.93 KB
/
wasm.js
File metadata and controls
135 lines (120 loc) · 5.93 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
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
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors. ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
const fs = require("fs");
const path = require("path");
exports.WasmPlugin = function WasmPlugin(inline, webpack_hack) {
function setup(build) {
const options = build.initialOptions;
options.metafile = true;
const KEYSET = [];
build.onResolve({ filter: /\.wasm$/ }, (args) => {
if (
args.namespace === "wasm-stub" ||
args.namespace === "wasm-inline"
) {
const entryPoint = path.join(
args.pluginData.resolveDir,
args.path
);
return {
path: entryPoint,
namespace: "wasm",
};
}
return {
path: args.path,
namespace: inline ? "wasm-inline" : "wasm-stub",
pluginData: {
resolveDir: args.resolveDir,
},
};
});
build.onLoad(
{ filter: /.*/, namespace: "wasm-inline" },
async (args) => ({
pluginData: args.pluginData,
contents: `
import wasm from ${JSON.stringify(args.path)};
export default function() {
return Promise.resolve(wasm.buffer);
};
`,
})
);
build.onLoad({ filter: /.*/, namespace: "wasm-stub" }, async (args) => {
const key = `__PSP_INLINE_WASM__${Math.random()
.toString()
.slice(2)}__`;
KEYSET.push(key);
const url = webpack_hack ? `${key}(wasm)` : `wasm`;
return {
pluginData: args.pluginData,
contents: `
import wasm from ${JSON.stringify(args.path)};
export default function() {
return fetch(new URL(${url}, import.meta.url));
};
`,
};
});
build.onLoad({ filter: /.*/, namespace: "wasm" }, async (args) => {
const path = require.resolve(args.path);
const contents = await fs.promises.readFile(path);
return {
pluginData: args.pluginData,
contents,
loader: inline ? "binary" : "file",
};
});
build.onEnd(({ metafile }) => {
if (webpack_hack) {
for (const file of Object.keys(metafile.outputs)) {
if (file.endsWith(".js")) {
let contents = fs.readFileSync(file).toString();
let updated = false;
for (const key of KEYSET) {
const symbol = contents.match(
new RegExp(`${key}\\(([a-zA-Z0-9_\$]+?)\\)`)
);
if (symbol?.[1]) {
updated = true;
const escapedSymbol = symbol[1].replace(
/\$/g,
"\\$"
);
const filename = contents.match(
new RegExp(
`(?<![a-zA-Z0-9_\$])${escapedSymbol}\\s*?=\\s*?\\"(.+?)\\"`
)
);
contents = contents.replace(
new RegExp(
`${key}\\(([a-zA-Z0-9_\$]+?)\\)`,
"g"
),
`"${filename[1]}"`
);
}
}
if (updated) {
fs.writeFileSync(file, contents);
}
}
}
}
});
}
return {
name: "wasm",
setup,
};
};