unwasm 0.4.1 → 0.5.0

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.
@@ -16,6 +16,12 @@ interface UnwasmPluginOptions {
16
16
  * @default false
17
17
  */
18
18
  lazy?: boolean;
19
+ /**
20
+ * Suppress all warnings from the plugin.
21
+ *
22
+ * @default false
23
+ */
24
+ silent?: boolean;
19
25
  }
20
26
  //#endregion
21
27
  //#region src/plugin/index.d.ts
@@ -5563,16 +5563,13 @@ async function getWasmImports(asset, _opts) {
5563
5563
  for (const moduleName of importNames) {
5564
5564
  const importNames$1 = asset.imports[moduleName];
5565
5565
  const importAlias = pkg.imports?.[moduleName] || pkg.imports?.[`#${moduleName}`];
5566
- const resolved$1 = importAlias && typeof importAlias === "string" ? importAlias : resolveModulePath(moduleName, {
5567
- from: asset.id,
5568
- try: true
5569
- });
5566
+ const resolved$1 = importAlias && typeof importAlias === "string" ? importAlias : resolveModulePath(moduleName, { from: asset.id });
5570
5567
  const importName = "_imports_" + genSafeVariableName(moduleName);
5571
- if (resolved$1) imports.push(genImport(resolved$1, {
5568
+ imports.push(genImport(resolved$1, {
5572
5569
  name: "*",
5573
5570
  as: importName
5574
5571
  }));
5575
- importsObject[moduleName] = Object.fromEntries(importNames$1.map((name) => [name, resolved$1 ? `${importName}[${genString(name)}]` : `() => { throw new Error(${genString(moduleName + "." + importName)} + " is not provided!")}`]));
5572
+ importsObject[moduleName] = Object.fromEntries(importNames$1.map((name) => [name, `${importName}[${genString(name)}]`]));
5576
5573
  }
5577
5574
  return {
5578
5575
  code: `${imports.join("\n")}\n\nconst _imports = ${genObjectFromRaw(importsObject)}`,
@@ -5784,34 +5781,57 @@ function unwasm(opts) {
5784
5781
  fileName: asset.name
5785
5782
  });
5786
5783
  },
5787
- async load(id) {
5788
- if (id === UMWASM_HELPERS_ID) return getPluginUtils();
5789
- if (!WASM_ID_RE.test(id)) return;
5790
- const idPath = id.split("?")[0];
5791
- if (!existsSync(idPath)) return;
5792
- this.addWatchFile(idPath);
5793
- return (await promises.readFile(idPath)).toString("binary");
5784
+ load: {
5785
+ order: "pre",
5786
+ async handler(id) {
5787
+ if (id === UMWASM_HELPERS_ID) return getPluginUtils();
5788
+ if (!WASM_ID_RE.test(id)) return;
5789
+ const idPath = id.split("?")[0];
5790
+ if (!existsSync(idPath)) return;
5791
+ this.addWatchFile(idPath);
5792
+ return (await promises.readFile(idPath)).toString("binary");
5793
+ }
5794
5794
  },
5795
- async transform(code, id) {
5796
- if (!WASM_ID_RE.test(id)) return;
5797
- const buff = Buffer.from(code, "binary");
5798
- const isModule = id.endsWith("?module");
5799
- const name = `wasm/${basename(id.split("?")[0], ".wasm")}-${sha1(buff)}.wasm`;
5800
- const parsed = isModule ? {
5801
- imports: [],
5802
- exports: ["default"]
5803
- } : parse(name, buff);
5804
- const asset = assets[name] = {
5805
- name,
5806
- id,
5807
- source: buff,
5808
- imports: parsed.imports,
5809
- exports: parsed.exports
5810
- };
5811
- return {
5812
- code: isModule ? await getWasmModuleBinding(asset, opts) : await getWasmESMBinding(asset, opts),
5813
- map: { mappings: "" }
5814
- };
5795
+ transform: {
5796
+ order: "pre",
5797
+ async handler(code, id) {
5798
+ if (!WASM_ID_RE.test(id)) return;
5799
+ const buff = Buffer.from(code, "binary");
5800
+ let isModule = id.endsWith("?module");
5801
+ const name = `wasm/${basename(id.split("?")[0], ".wasm")}-${sha1(buff)}.wasm`;
5802
+ let parsed = {
5803
+ imports: {},
5804
+ exports: ["default"]
5805
+ };
5806
+ if (!isModule) try {
5807
+ parsed = parse(name, buff);
5808
+ } catch (error) {
5809
+ if (!opts.silent) this.warn({
5810
+ id,
5811
+ cause: error,
5812
+ message: "Failed to parse the WebAssembly module; falling back to module mode."
5813
+ });
5814
+ isModule = true;
5815
+ }
5816
+ const asset = assets[name] = {
5817
+ name,
5818
+ id,
5819
+ source: buff,
5820
+ imports: parsed.imports,
5821
+ exports: parsed.exports
5822
+ };
5823
+ return {
5824
+ code: isModule ? await getWasmModuleBinding(asset, opts) : await getWasmESMBinding(asset, opts).catch((error) => {
5825
+ if (!opts.silent) this.warn({
5826
+ id,
5827
+ cause: error,
5828
+ message: "Failed to load the WebAssembly module; falling back to module mode: " + error.message
5829
+ });
5830
+ return getWasmModuleBinding(asset, opts);
5831
+ }),
5832
+ map: { mappings: "" }
5833
+ };
5834
+ }
5815
5835
  },
5816
5836
  renderChunk(code, chunk) {
5817
5837
  if (!opts.esmImport) return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "unwasm",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "WebAssembly tools for JavaScript",
5
5
  "repository": "unjs/unwasm",
6
6
  "license": "MIT",
@@ -24,12 +24,12 @@
24
24
  "lint": "eslint --cache . && prettier -c src test",
25
25
  "lint:fix": "eslint --cache . --fix && prettier -c src test -w",
26
26
  "prepack": "pnpm build",
27
- "release": "pnpm test && pnpm build && changelogen --release --publish",
27
+ "release": "pnpm test && pnpm build && changelogen --release --push --publish",
28
28
  "test": "pnpm lint && pnpm test:types && vitest run --coverage",
29
29
  "test:types": "tsc --noEmit --skipLibCheck"
30
30
  },
31
31
  "dependencies": {
32
- "exsolve": "^1.0.7",
32
+ "exsolve": "^1.0.8",
33
33
  "knitwork": "^1.2.0",
34
34
  "magic-string": "^0.30.21",
35
35
  "mlly": "^1.8.0",
@@ -37,27 +37,27 @@
37
37
  "pkg-types": "^2.3.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@prisma/client": "^6.18.0",
40
+ "@prisma/client": "^6.19.0",
41
41
  "@rollup/plugin-node-resolve": "^16.0.3",
42
- "@types/node": "^24.10.0",
43
- "@vitest/coverage-v8": "^4.0.6",
42
+ "@types/node": "^24.10.1",
43
+ "@vitest/coverage-v8": "^4.0.8",
44
44
  "@webassemblyjs/wasm-parser": "^1.14.1",
45
45
  "assemblyscript": "^0.28.9",
46
46
  "automd": "^0.4.2",
47
47
  "changelogen": "^0.6.2",
48
- "esbuild": "^0.25.12",
48
+ "esbuild": "^0.27.0",
49
49
  "eslint": "^9.39.1",
50
50
  "eslint-config-unjs": "^0.5.0",
51
51
  "exsolve": "^1.0.7",
52
52
  "jiti": "^2.6.1",
53
- "miniflare": "^4.20251011.1",
54
- "obuild": "^0.3.2",
53
+ "miniflare": "^4.20251109.0",
54
+ "obuild": "^0.4.1",
55
55
  "prettier": "^3.6.2",
56
- "rollup": "^4.52.5",
56
+ "rollup": "^4.53.2",
57
57
  "typescript": "^5.9.3",
58
- "vite": "^7.1.12",
59
- "vitest": "^4.0.6",
60
- "wabt": "^1.0.38"
58
+ "vite": "^7.2.2",
59
+ "vitest": "^4.0.8",
60
+ "wabt": "^1.0.39"
61
61
  },
62
62
  "resolutions": {
63
63
  "@webassemblyjs/helper-wasm-bytecode": "1.14.1",