Skip to content

Commit

Permalink
feat(wasm): migrate to unjs/unwasm (#2037)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Dec 29, 2023
1 parent 78b3e77 commit 1aec53e
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 208 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@
"unctx": "^2.3.1",
"unenv": "^1.8.0",
"unimport": "^3.7.0",
"unstorage": "^1.10.1"
"unstorage": "^1.10.1",
"unwasm": "^0.3.2"
},
"devDependencies": {
"@azure/functions": "^3.5.1",
Expand Down
84 changes: 84 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/presets/vercel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export const vercelEdge = defineNitroPreset({
},
wasm: {
lazy: true,
esmImport: true,
esmImport: false,
},
hooks: {
"rollup:before": (nitro: Nitro) => {
Expand Down
15 changes: 5 additions & 10 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import { pathToFileURL } from "node:url";
import { createRequire, builtinModules } from "node:module";
import { dirname, join, normalize, relative, resolve } from "pathe";
import type {
InputOptions,
OutputOptions,
Plugin,
PreRenderedChunk,
} from "rollup";
import { join, normalize, resolve } from "pathe";
import type { InputOptions, OutputOptions, Plugin } from "rollup";
import { defu } from "defu";
// import terser from "@rollup/plugin-terser"; // TODO: Investigate jiti issue
import commonjs from "@rollup/plugin-commonjs";
Expand All @@ -18,17 +13,17 @@ import { isWindows } from "std-env";
import { visualizer } from "rollup-plugin-visualizer";
import * as unenv from "unenv";
import type { Preset } from "unenv";
import { sanitizeFilePath, resolvePath, parseNodeModulePath } from "mlly";
import { sanitizeFilePath, resolvePath } from "mlly";
import unimportPlugin from "unimport/unplugin";
import { hash } from "ohash";
import { rollup as unwasm } from "unwasm/plugin";
import type { Nitro, NitroStaticBuildFlags } from "../types";
import { resolveAliases } from "../utils";
import { runtimeDir } from "../dirs";
import nitroPkg from "../../package.json";
import { nitroRuntimeDependencies } from "../deps";
import { replace } from "./plugins/replace";
import { virtual } from "./plugins/virtual";
import { wasm } from "./plugins/wasm";
import { dynamicRequire } from "./plugins/dynamic-require";
import { NodeExternalsOptions, externals } from "./plugins/externals";
import { externals as legacyExternals } from "./plugins/externals-legacy";
Expand Down Expand Up @@ -174,7 +169,7 @@ export const getRollupConfig = (nitro: Nitro): RollupConfig => {

// WASM support
if (nitro.options.experimental.wasm) {
rollupConfig.plugins.push(wasm(nitro.options.wasm || {}));
rollupConfig.plugins.push(unwasm(nitro.options.wasm || {}));
}

// Build-time environment variables
Expand Down
137 changes: 0 additions & 137 deletions src/rollup/plugins/wasm.ts

This file was deleted.

28 changes: 9 additions & 19 deletions src/types/nitro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { Storage, BuiltinDriverName } from "unstorage";
import type { ProxyServerOptions } from "httpxy";
import type { ProxyOptions, RouterMethod } from "h3";
import type { ResolvedConfig, ConfigWatcher } from "c12";
import type { UnwasmPluginOptions } from "unwasm/plugin";
import type { TSConfig } from "pkg-types";
import type { NodeExternalsOptions } from "../rollup/plugins/externals";
import type { RollupConfig } from "../rollup/config";
Expand Down Expand Up @@ -188,23 +189,6 @@ export interface NitroRouteRules
proxy?: { to: string } & ProxyOptions;
}

export interface WasmOptions {
/**
* Direct import the wasm file instead of bundling, required in Cloudflare Workers
*
* @default false
*/
esmImport?: boolean;

/**
* Import `.wasm` files using a lazily evaluated promise for compatibility
*/
lazy?: boolean;

/** @deprecated */
rollup?: unknown;
}

export interface NitroFrameworkInfo {
// eslint-disable-next-line @typescript-eslint/ban-types
name?: "nitro" | (string & {});
Expand Down Expand Up @@ -265,8 +249,12 @@ export interface NitroOptions extends PresetOptions {
renderer?: string;
serveStatic: boolean | "node" | "deno" | "inline";
noPublicDir: boolean;
/** @experimental Requires `experimental.wasm` to be effective */
wasm?: WasmOptions;
/**
* @experimental Requires `experimental.wasm` to work
*
* @see https://github.com/unjs/unwasm
*/
wasm?: UnwasmPluginOptions;
experimental?: {
legacyExternals?: boolean;
openAPI?: boolean;
Expand All @@ -280,6 +268,8 @@ export interface NitroOptions extends PresetOptions {
asyncContext?: boolean;
/**
* Enable Experimental WebAssembly Support
*
* @see https://github.com/unjs/unwasm
*/
wasm?: boolean;
/**
Expand Down
17 changes: 0 additions & 17 deletions test/fixture/node_modules/@fixture/wasm/package.json

This file was deleted.

6 changes: 0 additions & 6 deletions test/fixture/node_modules/@fixture/wasm/sum.asc.ts

This file was deleted.

Binary file removed test/fixture/node_modules/@fixture/wasm/sum.wasm
Binary file not shown.
1 change: 0 additions & 1 deletion test/fixture/node_modules/@fixture/wasm/sum.wasm.d.ts

This file was deleted.

Loading

0 comments on commit 1aec53e

Please sign in to comment.