Skip to content

Commit

Permalink
feat: support rspack.DllPlugin & rspack.DllReferencePlugin (web-infra…
Browse files Browse the repository at this point in the history
…-dev#8296)

* feat: rewrite dll-plugin by rust

* feat: support rspack.DllReferencePlugin

* test: add dll plugin tests

* refactor: improve delegated_module code

* docs: add dll-plugin & dll-refernce-plugin docs

* fix: flagAllModuleAsUsedPlugin add_extra_reason

* chore: rename some vars

* fix: typo

* fix: remove the unnecessary generics

* fix: typo

* fix: cargo clippy test

* fix: remove unused dependency rspack_macros

* docs: add dll & dll-reference plugin zh docs

* fix: resolved merge conflict

* fix: resolved some issues

- dll-reference-plugin should given a manifest struct, rather than
json-string
- we should not add a null module_graph_module_connection like webpack

* chore: add some annotations

* chore: add some annotations

* fix: update manifest content item exports type

* fix: update dll manifest types

* chore:update docs

* chore: clippy fix

* fix: bailout use build_info.module_concatenation_bailout
  • Loading branch information
GiveMe-A-Name authored Nov 11, 2024
1 parent ce5694e commit bcde793
Show file tree
Hide file tree
Showing 58 changed files with 2,856 additions and 43 deletions.
18 changes: 18 additions & 0 deletions Cargo.lock

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

66 changes: 66 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ export declare enum BuiltinPluginName {
SizeLimitsPlugin = 'SizeLimitsPlugin',
NoEmitOnErrorsPlugin = 'NoEmitOnErrorsPlugin',
ContextReplacementPlugin = 'ContextReplacementPlugin',
DllEntryPlugin = 'DllEntryPlugin',
DllReferenceAgencyPlugin = 'DllReferenceAgencyPlugin',
LibManifestPlugin = 'LibManifestPlugin',
FlagAllModulesAsUsedPlugin = 'FlagAllModulesAsUsedPlugin',
HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin',
CopyRspackPlugin = 'CopyRspackPlugin',
HtmlRspackPlugin = 'HtmlRspackPlugin',
Expand Down Expand Up @@ -451,6 +455,22 @@ export interface JsBeforeResolveArgs {
issuer: string
}

export interface JsBuildMeta {
strictEsmModule: boolean
hasTopLevelAwait: boolean
esm: boolean
exportsType: 'unset' | 'default' | 'namespace' | 'flagged' | 'dynamic'
defaultObject: 'false' | 'redirect' | JsBuildMetaDefaultObjectRedirectWarn
moduleArgument: 'module' | 'webpackModule'
exportsArgument: 'exports' | 'webpackExports'
sideEffectFree?: boolean
exportsFinalName?: Array<[string, string]> | undefined
}

export interface JsBuildMetaDefaultObjectRedirectWarn {
redirectWarn: JsDefaultObjectRedirectWarnObject
}

export interface JsBuildTimeExecutionOption {
publicPath?: string
baseUri?: string
Expand Down Expand Up @@ -524,6 +544,10 @@ export interface JsCreateData {
resource: string
}

export interface JsDefaultObjectRedirectWarnObject {
ignore: boolean
}

export interface JsDiagnostic {
message: string
help?: string
Expand Down Expand Up @@ -1267,6 +1291,35 @@ export interface RawCssParserOptions {
namedExports?: boolean
}

export interface RawDllEntryPluginOptions {
context: string
entries: Array<string>
name: string
}

export interface RawDllManifest {
content: Record<string, RawDllManifestContentItem>
name?: string
type?: string
}

export interface RawDllManifestContentItem {
buildMeta?: JsBuildMeta
exports?: string[] | true
id?: string
}

export interface RawDllReferenceAgencyPluginOptions {
context?: string
name?: string
extensions: Array<string>
scope?: string
sourceType?: string
type: string
content?: Record<string, RawDllManifestContentItem>
manifest?: RawDllManifest
}

export interface RawDraft {
customMedia: boolean
}
Expand Down Expand Up @@ -1345,6 +1398,10 @@ export interface RawFallbackCacheGroupOptions {
automaticNameDelimiter?: string
}

export interface RawFlagAllModulesAsUsedPluginOptions {
explanation: string
}

export interface RawFuncUseCtx {
resource?: string
realResource?: string
Expand Down Expand Up @@ -1473,6 +1530,15 @@ export interface RawLazyCompilationOption {
cacheable: boolean
}

export interface RawLibManifestPluginOptions {
context?: string
entryOnly?: boolean
name?: JsFilename
path: JsFilename
format?: boolean
type?: string
}

export interface RawLightningCssBrowsers {
android?: number
chrome?: number
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_binding_options/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ rspack_plugin_context_replacement = { version = "0.1.0", path = "../rspack_
rspack_plugin_copy = { version = "0.1.0", path = "../rspack_plugin_copy" }
rspack_plugin_css = { version = "0.1.0", path = "../rspack_plugin_css" }
rspack_plugin_devtool = { version = "0.1.0", path = "../rspack_plugin_devtool" }
rspack_plugin_dll = { version = "0.1.0", path = "../rspack_plugin_dll" }
rspack_plugin_dynamic_entry = { version = "0.1.0", path = "../rspack_plugin_dynamic_entry" }
rspack_plugin_ensure_chunk_conditions = { version = "0.1.0", path = "../rspack_plugin_ensure_chunk_conditions" }
rspack_plugin_entry = { version = "0.1.0", path = "../rspack_plugin_entry" }
Expand Down
38 changes: 35 additions & 3 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod raw_banner;
mod raw_bundle_info;
mod raw_copy;
mod raw_css_extract;
mod raw_dll;
mod raw_html;
mod raw_ignore;
mod raw_lazy_compilation;
Expand All @@ -15,6 +16,7 @@ mod raw_swc_js_minimizer;

use napi::{bindgen_prelude::FromNapiValue, Env, JsUnknown};
use napi_derive::napi;
use raw_dll::{RawDllReferenceAgencyPluginOptions, RawFlagAllModulesAsUsedPluginOptions};
use raw_lightning_css_minimizer::RawLightningCssMinimizerRspackPluginOptions;
use rspack_binding_values::entry::JsEntryPluginOptions;
use rspack_core::{BoxPlugin, Plugin, PluginExt};
Expand All @@ -34,6 +36,9 @@ use rspack_plugin_devtool::{
SourceMapDevToolModuleOptionsPluginOptions, SourceMapDevToolPlugin,
SourceMapDevToolPluginOptions,
};
use rspack_plugin_dll::{
DllEntryPlugin, DllReferenceAgencyPlugin, FlagAllModulesAsUsedPlugin, LibManifestPlugin,
};
use rspack_plugin_dynamic_entry::DynamicEntryPlugin;
use rspack_plugin_ensure_chunk_conditions::EnsureChunkConditionsPlugin;
use rspack_plugin_entry::EntryPlugin;
Expand Down Expand Up @@ -78,9 +83,13 @@ use rspack_plugin_web_worker_template::web_worker_template_plugin;
use rspack_plugin_worker::WorkerPlugin;

pub use self::{
raw_banner::RawBannerPluginOptions, raw_copy::RawCopyRspackPluginOptions,
raw_html::RawHtmlRspackPluginOptions, raw_ignore::RawIgnorePluginOptions,
raw_limit_chunk_count::RawLimitChunkCountPluginOptions, raw_mf::RawContainerPluginOptions,
raw_banner::RawBannerPluginOptions,
raw_copy::RawCopyRspackPluginOptions,
raw_dll::{RawDllEntryPluginOptions, RawLibManifestPluginOptions},
raw_html::RawHtmlRspackPluginOptions,
raw_ignore::RawIgnorePluginOptions,
raw_limit_chunk_count::RawLimitChunkCountPluginOptions,
raw_mf::RawContainerPluginOptions,
raw_progress::RawProgressPluginOptions,
raw_swc_js_minimizer::RawSwcJsMinimizerRspackPluginOptions,
};
Expand Down Expand Up @@ -166,6 +175,10 @@ pub enum BuiltinPluginName {
SizeLimitsPlugin,
NoEmitOnErrorsPlugin,
ContextReplacementPlugin,
DllEntryPlugin,
DllReferenceAgencyPlugin,
LibManifestPlugin,
FlagAllModulesAsUsedPlugin,

// rspack specific plugins
// naming format follow XxxRspackPlugin
Expand Down Expand Up @@ -517,6 +530,25 @@ impl BuiltinPlugin {
let options = raw_options.try_into()?;
plugins.push(ContextReplacementPlugin::new(options).boxed());
}
BuiltinPluginName::DllEntryPlugin => {
let raw_options = downcast_into::<RawDllEntryPluginOptions>(self.options)?;
let options = raw_options.into();
plugins.push(DllEntryPlugin::new(options).boxed());
}
BuiltinPluginName::LibManifestPlugin => {
let raw_options = downcast_into::<RawLibManifestPluginOptions>(self.options)?;
let options = raw_options.into();
plugins.push(LibManifestPlugin::new(options).boxed());
}
BuiltinPluginName::FlagAllModulesAsUsedPlugin => {
let raw_options = downcast_into::<RawFlagAllModulesAsUsedPluginOptions>(self.options)?;
plugins.push(FlagAllModulesAsUsedPlugin::new(raw_options.explanation).boxed())
}
BuiltinPluginName::DllReferenceAgencyPlugin => {
let raw_options = downcast_into::<RawDllReferenceAgencyPluginOptions>(self.options)?;
let options = raw_options.into();
plugins.push(DllReferenceAgencyPlugin::new(options).boxed());
}
}
Ok(())
}
Expand Down
168 changes: 168 additions & 0 deletions crates/rspack_binding_options/src/options/raw_builtins/raw_dll.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
use napi::Either;
use napi_derive::napi;
use rspack_binding_values::{JsBuildMeta, JsFilename};
use rspack_plugin_dll::{
DllEntryPluginOptions, DllManifest, DllManifestContent, DllManifestContentItem,
DllManifestContentItemExports, DllReferenceAgencyPluginOptions, LibManifestPluginOptions,
};
use rustc_hash::FxHashMap as HashMap;
use swc_core::atoms::Atom;

#[derive(Debug)]
#[napi(object)]
pub struct RawDllEntryPluginOptions {
pub context: String,
pub entries: Vec<String>,
pub name: String,
}

impl From<RawDllEntryPluginOptions> for DllEntryPluginOptions {
fn from(value: RawDllEntryPluginOptions) -> Self {
let RawDllEntryPluginOptions {
name,
context,
entries,
} = value;

Self {
name,
context: context.into(),
entries,
}
}
}

#[derive(Debug)]
#[napi(object, object_to_js = false)]
pub struct RawLibManifestPluginOptions {
pub context: Option<String>,
pub entry_only: Option<bool>,
pub name: Option<JsFilename>,
pub path: JsFilename,
pub format: Option<bool>,
pub r#type: Option<String>,
}

impl From<RawLibManifestPluginOptions> for LibManifestPluginOptions {
fn from(value: RawLibManifestPluginOptions) -> Self {
let RawLibManifestPluginOptions {
context,
entry_only,
name,
path,
r#type,
format,
} = value;

Self {
context: context.map(|c| c.into()),
format,
entry_only,
name: name.map(|n| n.into()),
path: path.into(),
r#type,
}
}
}

#[napi(object, object_to_js = false)]
pub struct RawDllReferenceAgencyPluginOptions {
pub context: Option<String>,
pub name: Option<String>,
pub extensions: Vec<String>,
pub scope: Option<String>,
pub source_type: Option<String>,
pub r#type: String,
pub content: Option<HashMap<String, RawDllManifestContentItem>>,
pub manifest: Option<RawDllManifest>,
}

#[napi(object, object_to_js = false)]
pub struct RawDllManifestContentItem {
pub build_meta: Option<JsBuildMeta>,
#[napi(ts_type = "string[] | true")]
pub exports: Option<Either<Vec<String>, bool>>,
pub id: Option<String>,
}

impl From<RawDllManifestContentItem> for DllManifestContentItem {
fn from(value: RawDllManifestContentItem) -> Self {
let raw_exports = value.exports;

let exports = raw_exports.map(|exports| match exports {
Either::A(seq) => {
DllManifestContentItemExports::Vec(seq.into_iter().map(Atom::from).collect::<Vec<_>>())
}
Either::B(bool) => {
if bool {
DllManifestContentItemExports::True
} else {
unreachable!()
}
}
});

Self {
build_meta: value.build_meta.map(|meta| meta.into()),
exports,
id: value.id,
}
}
}

#[napi(object, object_to_js = false)]
pub struct RawDllManifest {
pub content: HashMap<String, RawDllManifestContentItem>,
pub name: Option<String>,
pub r#type: Option<String>,
}

impl From<RawDllManifest> for DllManifest {
fn from(value: RawDllManifest) -> Self {
Self {
content: value
.content
.into_iter()
.map(|(k, v)| (k, v.into()))
.collect::<DllManifestContent>(),
name: value.name,
r#type: value.r#type,
}
}
}

impl From<RawDllReferenceAgencyPluginOptions> for DllReferenceAgencyPluginOptions {
fn from(value: RawDllReferenceAgencyPluginOptions) -> Self {
let RawDllReferenceAgencyPluginOptions {
context,
name,
extensions,
scope,
source_type,
r#type,
content,
manifest,
} = value;

Self {
context: context.map(|ctx| ctx.into()),
name,
extensions,
scope,
source_type,
r#type,
content: content.map(|c| {
c.into_iter()
.map(|(k, v)| (k, v.into()))
.collect::<DllManifestContent>()
}),
manifest: manifest.map(|m| m.into()),
}
}
}

#[derive(Debug)]
#[napi(object)]
pub struct RawFlagAllModulesAsUsedPluginOptions {
pub explanation: String,
}
Loading

0 comments on commit bcde793

Please sign in to comment.