Skip to content

Commit

Permalink
feat: export FetchCompileAsyncWasmPlugin for worker loader (#6832)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Jun 18, 2024
1 parent 66dcc09 commit ced815e
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 7 deletions.
1 change: 1 addition & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export enum BuiltinPluginName {
EnableChunkLoadingPlugin = 'EnableChunkLoadingPlugin',
EnableLibraryPlugin = 'EnableLibraryPlugin',
EnableWasmLoadingPlugin = 'EnableWasmLoadingPlugin',
FetchCompileAsyncWasmPlugin = 'FetchCompileAsyncWasmPlugin',
ChunkPrefetchPreloadPlugin = 'ChunkPrefetchPreloadPlugin',
CommonJsChunkFormatPlugin = 'CommonJsChunkFormatPlugin',
ArrayPushCallbackChunkFormatPlugin = 'ArrayPushCallbackChunkFormatPlugin',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ use rspack_plugin_size_limits::SizeLimitsPlugin;
use rspack_plugin_swc_css_minimizer::SwcCssMinimizerRspackPlugin;
use rspack_plugin_swc_js_minimizer::SwcJsMinimizerRspackPlugin;
use rspack_plugin_warn_sensitive_module::WarnCaseSensitiveModulesPlugin;
use rspack_plugin_wasm::{enable_wasm_loading_plugin, AsyncWasmPlugin};
use rspack_plugin_wasm::{
enable_wasm_loading_plugin, AsyncWasmPlugin, FetchCompileAsyncWasmPlugin,
};
use rspack_plugin_web_worker_template::web_worker_template_plugin;
use rspack_plugin_worker::WorkerPlugin;

Expand Down Expand Up @@ -112,6 +114,7 @@ pub enum BuiltinPluginName {
EnableChunkLoadingPlugin,
EnableLibraryPlugin,
EnableWasmLoadingPlugin,
FetchCompileAsyncWasmPlugin,
ChunkPrefetchPreloadPlugin,
CommonJsChunkFormatPlugin,
ArrayPushCallbackChunkFormatPlugin,
Expand Down Expand Up @@ -256,6 +259,9 @@ impl BuiltinPlugin {
wasm_loading_type.as_str().into(),
));
}
BuiltinPluginName::FetchCompileAsyncWasmPlugin => {
plugins.push(FetchCompileAsyncWasmPlugin::default().boxed())
}
BuiltinPluginName::ChunkPrefetchPreloadPlugin => {
plugins.push(ChunkPrefetchPreloadPlugin::default().boxed());
}
Expand Down
21 changes: 21 additions & 0 deletions packages/rspack/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -3164,6 +3164,17 @@ export type Falsy = z.infer<typeof falsy>;
// @public (undocumented)
const falsy: z.ZodUnion<[z.ZodLiteral<false>, z.ZodLiteral<0>, z.ZodLiteral<"">, z.ZodNull, z.ZodUndefined]>;

// @public (undocumented)
const FetchCompileAsyncWasmPlugin: {
new (): {
name: BuiltinPluginName;
_options: void;
affectedHooks: "make" | "compile" | "emit" | "afterEmit" | "invalid" | "done" | "thisCompilation" | "afterDone" | "compilation" | "normalModuleFactory" | "contextModuleFactory" | "initialize" | "shouldEmit" | "infrastructureLog" | "beforeRun" | "run" | "assetEmitted" | "failed" | "shutdown" | "watchRun" | "watchClose" | "environment" | "afterEnvironment" | "afterPlugins" | "afterResolvers" | "beforeCompile" | "afterCompile" | "finishMake" | "entryOption" | undefined;
raw(): BuiltinPlugin;
apply(compiler: Compiler_2): void;
};
};

// @public (undocumented)
export type Filename = z.infer<typeof filename>;

Expand Down Expand Up @@ -8253,6 +8264,7 @@ declare namespace rspackExports {
LoaderOptionsPlugin,
LoaderTargetPlugin,
NormalModuleReplacementPlugin,
web,
node,
electron,
library,
Expand Down Expand Up @@ -13350,6 +13362,15 @@ namespace Watchpack {
}
}

// @public (undocumented)
interface Web {
// (undocumented)
FetchCompileAsyncWasmPlugin: typeof FetchCompileAsyncWasmPlugin;
}

// @public (undocumented)
export const web: Web;

// @public (undocumented)
export type WebassemblyModuleFilename = z.infer<typeof webassemblyModuleFilename>;

Expand Down
1 change: 0 additions & 1 deletion packages/rspack/src/Compiler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import fs from "fs";
/**
* The following code is modified based on
* https://github.com/webpack/webpack/blob/4b4ca3bb53f36a5b8fc6bc1bd976ed7af161bd80/lib/Compiler.js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { BuiltinPluginName } from "@rspack/binding";

import { create } from "./base";

export const FetchCompileAsyncWasmPlugin = create(
BuiltinPluginName.FetchCompileAsyncWasmPlugin,
() => {},
"thisCompilation"
);
1 change: 1 addition & 0 deletions packages/rspack/src/builtin-plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export * from "./SwcJsMinimizerPlugin";
export * from "./WarnCaseSensitiveModulesPlugin";
export * from "./WebWorkerTemplatePlugin";
export * from "./WorkerPlugin";
export * from "./FetchCompileAsyncWasmPlugin";

///// DEPRECATED /////
import { RawBuiltins } from "@rspack/binding";
Expand Down
8 changes: 8 additions & 0 deletions packages/rspack/src/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ export { LoaderOptionsPlugin } from "./lib/LoaderOptionsPlugin";
export { LoaderTargetPlugin } from "./lib/LoaderTargetPlugin";
export { NormalModuleReplacementPlugin } from "./lib/NormalModuleReplacementPlugin";

import { FetchCompileAsyncWasmPlugin } from "./builtin-plugin";
interface Web {
FetchCompileAsyncWasmPlugin: typeof FetchCompileAsyncWasmPlugin;
}
export const web: Web = {
FetchCompileAsyncWasmPlugin
};

import { NodeTargetPlugin } from "./builtin-plugin";
import NodeEnvironmentPlugin from "./node/NodeEnvironmentPlugin";
import NodeTemplatePlugin from "./node/NodeTemplatePlugin";
Expand Down
7 changes: 2 additions & 5 deletions website/components/PluginSupportStatusTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -372,11 +372,8 @@ const pluginSupportStatusList: PluginSupportStatus[] = [
},
{
name: 'FetchCompileAsyncWasmPlugin',
status: SupportStatus.PartiallySupported,
notes: {
en: 'Temporarily not exported from the JavaScript side',
zh: '暂时未从 JavaScript 侧导出',
},
url: '/plugins/webpack/fetch-compile-async-wasm-plugin',
status: SupportStatus.FullySupported,
},
{
name: 'FetchCompileWasmPlugin',
Expand Down
22 changes: 22 additions & 0 deletions website/docs/en/guide/features/web-workers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,25 @@ The syntax was chosen to allow running code without bundler, it is also availabl
- Not supported `/* webpackEntryOptions: { filename: "workers/[name].js" } */` magic comments
:::
## worker-loader
<ApiMeta addedVersion={'0.7.4'} />
:::warning
worker-loader is provided only as a temporary solution to facilitate project migration to Rspack. It is recommended to use the `new Worker()` syntax instead.
:::
Rspack also supports worker-loader. However, since [worker-loader](https://github.com/webpack-contrib/worker-loader) is no longer maintained, please use [worker-rspack-loader](https://github.com/rspack-contrib/worker-rspack-loader) as a replacement.
Use [resolveLoader](/config/resolve-loader) to replace worker-loader with worker-rspack-loader:
```js
module.exports = {
resolveLoader: {
alias: {
'worker-loader': require.resolve('worker-rspack-loader'),
},
},
};
```
8 changes: 8 additions & 0 deletions website/docs/en/plugins/webpack/internal-plugins.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ Chunks are loaded by `importScripts`.

`options` are the output options.

### FetchCompileAsyncWasmPlugin

<ApiMeta addedVersion={'0.7.4'} />

This plugin is used to provide runtime code for WASM bundling and is often used together with a child compiler.

`web.FetchCompileAsyncWasmPlugin()`

## source

Plugins affecting the source code of modules.
Expand Down
22 changes: 22 additions & 0 deletions website/docs/zh/guide/features/web-workers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,25 @@ new Worker(new URL('./worker.js', import.meta.url), {
- 不支持 `/* webpackEntryOptions: { filename: "workers/[name].js" } */` 魔法注释
:::
## worker-loader
<ApiMeta addedVersion={'0.7.4'} />
:::warning
worker-loader 仅供作为方便项目迁移至 Rspack 的临时方案,推荐使用 `new Worker()` 语法。
:::
Rspack 也支持了 worker-loader,不过由于 [worker-loader](https://github.com/webpack-contrib/worker-loader) 已不再维护,请使用 [worker-rspack-loader](https://github.com/rspack-contrib/worker-rspack-loader) 进行替换。
使用 [resolveLoader](/config/resolve-loader) 替换 worker-loader 为 worker-rspack-loader:
```js
module.exports = {
resolveLoader: {
alias: {
'worker-loader': require.resolve('worker-rspack-loader'),
},
},
};
```
8 changes: 8 additions & 0 deletions website/docs/zh/plugins/webpack/internal-plugins.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ import WebpackLicense from '@components/webpack-license';

`options` 是输出配置。

### FetchCompileAsyncWasmPlugin

<ApiMeta addedVersion={'0.7.4'} />

用来提供 WASM 打包的运行时代码,经常配合 childCompiler 一起使用。

`web.FetchCompileAsyncWasmPlugin()`

## source

影响模块源代码的插件。
Expand Down

2 comments on commit ced815e

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs ❌ failure
_selftest ✅ success
nx ✅ success
rspress ✅ success
rsbuild ✅ success
compat ✅ success
examples ✅ success

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-06-18 553f785) Current Change
10000_development-mode + exec 2.21 s ± 22 ms 2.22 s ± 31 ms +0.24 %
10000_development-mode_hmr + exec 737 ms ± 12 ms 736 ms ± 3.7 ms -0.07 %
10000_production-mode + exec 2.57 s ± 22 ms 2.57 s ± 24 ms +0.27 %
arco-pro_development-mode + exec 1.94 s ± 68 ms 1.95 s ± 75 ms +0.30 %
arco-pro_development-mode_hmr + exec 441 ms ± 2.1 ms 442 ms ± 1.8 ms +0.23 %
arco-pro_production-mode + exec 3.53 s ± 85 ms 3.53 s ± 61 ms +0.13 %
threejs_development-mode_10x + exec 1.46 s ± 11 ms 1.48 s ± 11 ms +1.17 %
threejs_development-mode_10x_hmr + exec 799 ms ± 3.3 ms 803 ms ± 5.8 ms +0.52 %
threejs_production-mode_10x + exec 4.76 s ± 33 ms 4.78 s ± 31 ms +0.35 %

Please sign in to comment.