unwasm 0.3.3 → 0.3.5

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.
package/dist/plugin.cjs CHANGED
@@ -9,13 +9,13 @@ const unplugin$1 = require('unplugin');
9
9
  const node_crypto = require('node:crypto');
10
10
  const pkgTypes = require('pkg-types');
11
11
  const tools = require('./tools.cjs');
12
- require('@webassemblyjs/wasm-parser');
13
12
 
14
13
  function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
15
14
 
16
15
  const MagicString__default = /*#__PURE__*/_interopDefaultCompat(MagicString);
17
16
 
18
17
  const UNWASM_EXTERNAL_PREFIX = "\0unwasm:external:";
18
+ const UNWASM_EXTERNAL_RE = /(\0|\\0)unwasm:external:([^"']+)/gu;
19
19
  const UMWASM_HELPERS_ID = "\0unwasm:helpers";
20
20
  function sha1(source) {
21
21
  return node_crypto.createHash("sha1").update(source).digest("hex").slice(0, 16);
@@ -138,7 +138,7 @@ export function createLazyWasmModule(_instantiator) {
138
138
  }
139
139
  `;
140
140
  }
141
- async function getWasmImports(asset, opts) {
141
+ async function getWasmImports(asset, _opts) {
142
142
  const importNames = Object.keys(asset.imports || {});
143
143
  if (importNames.length === 0) {
144
144
  return {
@@ -183,17 +183,21 @@ const unplugin = unplugin$1.createUnplugin((opts) => {
183
183
  if (_parseCache[name]) {
184
184
  return _parseCache[name];
185
185
  }
186
- const parsed = tools.parseWasm(source);
187
186
  const imports = /* @__PURE__ */ Object.create(null);
188
187
  const exports = [];
189
- for (const mod of parsed.modules) {
190
- exports.push(...mod.exports.map((e) => e.name));
191
- for (const imp of mod.imports) {
192
- if (!imports[imp.module]) {
193
- imports[imp.module] = [];
188
+ try {
189
+ const parsed = tools.parseWasm(source, { name });
190
+ for (const mod of parsed.modules) {
191
+ exports.push(...mod.exports.map((e) => e.name));
192
+ for (const imp of mod.imports) {
193
+ if (!imports[imp.module]) {
194
+ imports[imp.module] = [];
195
+ }
196
+ imports[imp.module].push(imp.name);
194
197
  }
195
- imports[imp.module].push(imp.name);
196
198
  }
199
+ } catch (error) {
200
+ console.warn(`[unwasm] Failed to parse WASM module ${name}:`, error);
197
201
  }
198
202
  _parseCache[name] = {
199
203
  imports,
@@ -271,7 +275,7 @@ const unplugin = unplugin$1.createUnplugin((opts) => {
271
275
  if (!opts.esmImport) {
272
276
  return;
273
277
  }
274
- if (!(chunk.moduleIds.some((id) => id.endsWith(".wasm")) || chunk.imports.some((id) => id.endsWith(".wasm"))) || !code.includes(UNWASM_EXTERNAL_PREFIX)) {
278
+ if (!(chunk.moduleIds.some((id) => id.endsWith(".wasm")) || chunk.imports.some((id) => id.endsWith(".wasm")))) {
275
279
  return;
276
280
  }
277
281
  const s = new MagicString__default(code);
@@ -283,16 +287,18 @@ const unplugin = unplugin$1.createUnplugin((opts) => {
283
287
  if (!asset) {
284
288
  return;
285
289
  }
286
- const nestedLevel = chunk.fileName.split("/").length - 1;
290
+ const nestedLevel = chunk.fileName.split("/").filter(
291
+ Boolean
292
+ /* handle // */
293
+ ).length - 1;
287
294
  const relativeId = (nestedLevel ? "../".repeat(nestedLevel) : "./") + asset.name;
288
295
  return {
289
296
  relativeId,
290
297
  asset
291
298
  };
292
299
  };
293
- const ReplaceRE = new RegExp(`${UNWASM_EXTERNAL_PREFIX}([^"']+)`, "g");
294
- for (const match of code.matchAll(ReplaceRE)) {
295
- const resolved = resolveImport(match[1]);
300
+ for (const match of code.matchAll(UNWASM_EXTERNAL_RE)) {
301
+ const resolved = resolveImport(match[2]);
296
302
  const index = match.index;
297
303
  const len = match[0].length;
298
304
  if (!resolved || !index) {
package/dist/plugin.mjs CHANGED
@@ -5,9 +5,9 @@ import { createUnplugin } from 'unplugin';
5
5
  import { createHash } from 'node:crypto';
6
6
  import { readPackageJSON } from 'pkg-types';
7
7
  import { parseWasm } from './tools.mjs';
8
- import '@webassemblyjs/wasm-parser';
9
8
 
10
9
  const UNWASM_EXTERNAL_PREFIX = "\0unwasm:external:";
10
+ const UNWASM_EXTERNAL_RE = /(\0|\\0)unwasm:external:([^"']+)/gu;
11
11
  const UMWASM_HELPERS_ID = "\0unwasm:helpers";
12
12
  function sha1(source) {
13
13
  return createHash("sha1").update(source).digest("hex").slice(0, 16);
@@ -130,7 +130,7 @@ export function createLazyWasmModule(_instantiator) {
130
130
  }
131
131
  `;
132
132
  }
133
- async function getWasmImports(asset, opts) {
133
+ async function getWasmImports(asset, _opts) {
134
134
  const importNames = Object.keys(asset.imports || {});
135
135
  if (importNames.length === 0) {
136
136
  return {
@@ -175,17 +175,21 @@ const unplugin = createUnplugin((opts) => {
175
175
  if (_parseCache[name]) {
176
176
  return _parseCache[name];
177
177
  }
178
- const parsed = parseWasm(source);
179
178
  const imports = /* @__PURE__ */ Object.create(null);
180
179
  const exports = [];
181
- for (const mod of parsed.modules) {
182
- exports.push(...mod.exports.map((e) => e.name));
183
- for (const imp of mod.imports) {
184
- if (!imports[imp.module]) {
185
- imports[imp.module] = [];
180
+ try {
181
+ const parsed = parseWasm(source, { name });
182
+ for (const mod of parsed.modules) {
183
+ exports.push(...mod.exports.map((e) => e.name));
184
+ for (const imp of mod.imports) {
185
+ if (!imports[imp.module]) {
186
+ imports[imp.module] = [];
187
+ }
188
+ imports[imp.module].push(imp.name);
186
189
  }
187
- imports[imp.module].push(imp.name);
188
190
  }
191
+ } catch (error) {
192
+ console.warn(`[unwasm] Failed to parse WASM module ${name}:`, error);
189
193
  }
190
194
  _parseCache[name] = {
191
195
  imports,
@@ -263,7 +267,7 @@ const unplugin = createUnplugin((opts) => {
263
267
  if (!opts.esmImport) {
264
268
  return;
265
269
  }
266
- if (!(chunk.moduleIds.some((id) => id.endsWith(".wasm")) || chunk.imports.some((id) => id.endsWith(".wasm"))) || !code.includes(UNWASM_EXTERNAL_PREFIX)) {
270
+ if (!(chunk.moduleIds.some((id) => id.endsWith(".wasm")) || chunk.imports.some((id) => id.endsWith(".wasm")))) {
267
271
  return;
268
272
  }
269
273
  const s = new MagicString(code);
@@ -275,16 +279,18 @@ const unplugin = createUnplugin((opts) => {
275
279
  if (!asset) {
276
280
  return;
277
281
  }
278
- const nestedLevel = chunk.fileName.split("/").length - 1;
282
+ const nestedLevel = chunk.fileName.split("/").filter(
283
+ Boolean
284
+ /* handle // */
285
+ ).length - 1;
279
286
  const relativeId = (nestedLevel ? "../".repeat(nestedLevel) : "./") + asset.name;
280
287
  return {
281
288
  relativeId,
282
289
  asset
283
290
  };
284
291
  };
285
- const ReplaceRE = new RegExp(`${UNWASM_EXTERNAL_PREFIX}([^"']+)`, "g");
286
- for (const match of code.matchAll(ReplaceRE)) {
287
- const resolved = resolveImport(match[1]);
292
+ for (const match of code.matchAll(UNWASM_EXTERNAL_RE)) {
293
+ const resolved = resolveImport(match[2]);
288
294
  const index = match.index;
289
295
  const len = match[0].length;
290
296
  if (!resolved || !index) {