Skip to content

Commit

Permalink
feat: add wasm fallback plugin (#1957)
Browse files Browse the repository at this point in the history
resolves #1919
  • Loading branch information
ikkz authored Aug 12, 2024
1 parent 41c0b00 commit 3645f38
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 7 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ rolldown_plugin_manifest = { version = "0.1.0", path = "./crates/
rolldown_plugin_module_preload_polyfill = { version = "0.1.0", path = "./crates/rolldown_plugin_module_preload_polyfill" }
rolldown_plugin_transform = { version = "0.1.0", path = "./crates/rolldown_plugin_transform" }
rolldown_plugin_wasm = { version = "0.1.0", path = "./crates/rolldown_plugin_wasm" }
rolldown_plugin_wasm_fallback = { version = "0.1.0", path = "./crates/rolldown_plugin_wasm_fallback" }
rolldown_resolver = { version = "0.1.0", path = "./crates/rolldown_resolver" }
rolldown_rstr = { version = "0.1.0", path = "./crates/rolldown_rstr" }
rolldown_sourcemap = { version = "0.1.0", path = "./crates/rolldown_sourcemap" }
Expand Down
1 change: 1 addition & 0 deletions crates/rolldown_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ rolldown_plugin_manifest = { workspace = true }
rolldown_plugin_module_preload_polyfill = { workspace = true }
rolldown_plugin_transform = { workspace = true }
rolldown_plugin_wasm = { workspace = true }
rolldown_plugin_wasm_fallback = { workspace = true }
rolldown_sourcemap = { workspace = true }
rolldown_tracing = { workspace = true }
rolldown_utils = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use rolldown_plugin_manifest::{ManifestPlugin, ManifestPluginConfig};
use rolldown_plugin_module_preload_polyfill::ModulePreloadPolyfillPlugin;
use rolldown_plugin_transform::{StringOrRegex, TransformPlugin};
use rolldown_plugin_wasm::WasmPlugin;
use rolldown_plugin_wasm_fallback::WasmFallbackPlugin;
use serde::Deserialize;
use std::sync::Arc;

Expand Down Expand Up @@ -45,6 +46,7 @@ pub enum BindingBuiltinPluginName {
ManifestPlugin,
LoadFallbackPlugin,
TransformPlugin,
WasmFallbackPlugin,
}

#[napi_derive::napi(object)]
Expand Down Expand Up @@ -132,6 +134,7 @@ impl TryFrom<BindingBuiltinPlugin> for Arc<dyn Pluginable> {
fn try_from(plugin: BindingBuiltinPlugin) -> Result<Self, Self::Error> {
Ok(match plugin.__name {
BindingBuiltinPluginName::WasmPlugin => Arc::new(WasmPlugin {}),
BindingBuiltinPluginName::WasmFallbackPlugin => Arc::new(WasmFallbackPlugin {}),
BindingBuiltinPluginName::ImportGlobPlugin => {
let config = if let Some(options) = plugin.options {
BindingGlobImportPluginConfig::from_unknown(options)?.into()
Expand Down
19 changes: 19 additions & 0 deletions crates/rolldown_plugin_wasm_fallback/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
edition.workspace = true
homepage.workspace = true
license.workspace = true
name = "rolldown_plugin_wasm_fallback"
repository.workspace = true
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
doctest = false

[lints]
workspace = true

[dependencies]
anyhow = { workspace = true }
rolldown_plugin = { workspace = true }
27 changes: 27 additions & 0 deletions crates/rolldown_plugin_wasm_fallback/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::borrow::Cow;

use rolldown_plugin::{HookLoadArgs, HookLoadReturn, Plugin, SharedPluginContext};

#[derive(Debug)]
pub struct WasmFallbackPlugin {}

impl Plugin for WasmFallbackPlugin {
fn name(&self) -> Cow<'static, str> {
Cow::Borrowed("builtin:wasm-fallback-plugin")
}

#[allow(clippy::case_sensitive_file_extension_comparisons)]
async fn load(&self, _ctx: &SharedPluginContext, args: &HookLoadArgs<'_>) -> HookLoadReturn {
if args.id.ends_with(".wasm") {
// TODO: Replace the link here after rolldown's document is ready
Err(anyhow::anyhow!(
"\"ESM integration proposal for Wasm\" is not supported currently.
Use plugin-wasm or other community plugins to handle this.
Alternatively, you can use `.wasm?init` or `.wasm?url`.
See https://vitejs.dev/guide/features.html#webassembly for more details."
))
} else {
Ok(None)
}
}
}
3 changes: 2 additions & 1 deletion packages/rolldown/src/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ export declare enum BindingBuiltinPluginName {
ModulePreloadPolyfillPlugin = 3,
ManifestPlugin = 4,
LoadFallbackPlugin = 5,
TransformPlugin = 6
TransformPlugin = 6,
WasmFallbackPlugin = 7
}

export interface BindingEmittedAsset {
Expand Down
1 change: 1 addition & 0 deletions packages/rolldown/src/experimental-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export {
modulePreloadPolyfillPlugin,
dynamicImportVarsPlugin,
wasmPlugin,
wasmFallbackPlugin,
importGlobPlugin,
manifestPlugin,
loadFallbackPlugin,
Expand Down
10 changes: 10 additions & 0 deletions packages/rolldown/src/plugin/builtin-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ export class WasmPlugin extends BuiltinPlugin {
}
}

export class WasmFallbackPlugin extends BuiltinPlugin {
constructor() {
super(BindingBuiltinPluginName.WasmFallbackPlugin)
}
}

export class LoadFallbackPlugin extends BuiltinPlugin {
constructor() {
super(BindingBuiltinPluginName.LoadFallbackPlugin)
Expand Down Expand Up @@ -85,6 +91,10 @@ export function wasmPlugin() {
return new WasmPlugin()
}

export function wasmFallbackPlugin() {
return new WasmFallbackPlugin()
}

export function transformPlugin(config?: TransformPluginConfig) {
return new TransformPlugin(config)
}
Expand Down
20 changes: 16 additions & 4 deletions packages/rolldown/tests/fixture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ function main() {
const output = await compileFixture(
nodePath.join(import.meta.dirname, dirPath),
testConfig,
)
if (testConfig.afterTest) {
).catch(async (err) => {
if (testConfig.catchError) {
await testConfig.catchError(err)
return
}
throw err
})
if (testConfig.afterTest && output) {
await testConfig.afterTest(output)
}
} catch (err) {
Expand All @@ -50,8 +56,14 @@ function main() {
const output = await compileFixture(
nodePath.join(import.meta.dirname, dirPath),
testConfig,
)
if (testConfig.afterTest) {
).catch(async (err) => {
if (testConfig.catchError) {
await testConfig.catchError(err)
return
}
throw err
})
if (testConfig.afterTest && output) {
await testConfig.afterTest(output)
}
} catch (err) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { wasmFallbackPlugin } from 'rolldown/experimental'
import { defineTest } from '@tests'

export default defineTest({
config: {
plugins: [wasmFallbackPlugin()],
},
catchError: () => {
// Errors are swallowed here
},
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import init from './add.wasm'

init().then(({ exports }) => {
exports.add(1, 2) === 3
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { wasmPlugin } from 'rolldown/experimental'
import { wasmFallbackPlugin, wasmPlugin } from 'rolldown/experimental'
import { defineTest } from '@tests'

export default defineTest({
config: {
plugins: [wasmPlugin()],
plugins: [wasmPlugin(), wasmFallbackPlugin()],
},
})
1 change: 1 addition & 0 deletions packages/rolldown/tests/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface TestConfig {
config?: RolldownOptions
beforeTest?: () => Promise<void> | void
afterTest?: (output: RolldownOutput) => Promise<void> | void
catchError?: (err: unknown) => Promise<void> | void
}

0 comments on commit 3645f38

Please sign in to comment.