Skip to content

Commit

Permalink
chore: merge origin main
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Apr 29, 2024
2 parents 446b234 + 6d8ec18 commit 57af683
Show file tree
Hide file tree
Showing 102 changed files with 1,069 additions and 332 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ Thanks to:
- The [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) project created by [@jantimon](https://github.com/jantimon), `@rspack/html-plugin` is a fork of [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) to avoid some webpack API usage not supported in Rspack.
- The [Turbopack](https://github.com/vercel/turbo) project which inspired the AST path logic of Rspack.
- The [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) created by [@pmmmwh](https://github.com/pmmmwh), which inspires implement react refresh.
- The [mini-css-extract-plugin] project created by [@sokra](https://github.com/sokra) which inspired implement css extract plugin.
- The [copy-webpack-plugin] project created by [@kevlened](https://github.com/kevlened) which inspired implement copy rspack plugin.
- The [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) project created by [@sokra](https://github.com/sokra) which inspired implement css extract plugin.
- The [copy-webpack-plugin](https://github.com/webpack-contrib/copy-webpack-plugin) project created by [@kevlened](https://github.com/kevlened) which inspired implement copy rspack plugin.

## License

Expand Down
3 changes: 3 additions & 0 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
- [rolldown-legacy](https://github.com/rolldown-rs/rolldown-legacy) 项目,它是 [rolldown](https://github.com/rolldown) 项目的前身,它探索了使用 Rust 构建高性能 Bundler + 兼容 Rollup API 的可能性,启发了 Rspack 的设计方向。
- [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) 项目(由 [@jantimon](https://github.com/jantimon) 创建),Rspack 的 `@rspack/html-plugin`[html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) 的一个 fork 来避免使用在 Rspack 中尚未支持的 webpack API。
- [Turbopack](https://github.com/vercel/turbo) 项目,它启发了 Rspack 里基于 AST 的路径重写逻辑。
- [react-refresh-webpack-plugin](https://github.com/pmmmwh/react-refresh-webpack-plugin) 项目(由 [@pmmmwh](https://github.com/pmmmwh) 创建),它启发了 Rspack 内的 ReactRefreshPlugin 实现。
- [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin) 项目(由 [@sokra](https://github.com/sokra) 创建),它启发了 Rspack 内的 CssExtractPlugin 实现。
- [copy-webpack-plugin](https://github.com/webpack-contrib/copy-webpack-plugin) 项目(由 [@kevlened](https://github.com/kevlened) 创建),它启发了 Rsapck 内的 CopyPlugin 实现。

## License

Expand Down
13 changes: 11 additions & 2 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export enum BuiltinPluginName {
ModuleConcatenationPlugin = 'ModuleConcatenationPlugin',
CssModulesPlugin = 'CssModulesPlugin',
APIPlugin = 'APIPlugin',
RuntimeChunkPlugin = 'RuntimeChunkPlugin',
HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin',
CopyRspackPlugin = 'CopyRspackPlugin',
HtmlRspackPlugin = 'HtmlRspackPlugin',
Expand Down Expand Up @@ -714,7 +715,7 @@ export interface RawContainerPluginOptions {
name: string
shareScope: string
library: RawLibraryOptions
runtime?: string
runtime?: false | string
filename?: string
exposes: Array<RawExposeOptions>
enhanced: boolean
Expand Down Expand Up @@ -807,7 +808,7 @@ export interface RawEntryDynamicResult {

export interface RawEntryOptions {
name?: string
runtime?: string
runtime?: false | string
chunkLoading?: string
asyncChunks?: boolean
publicPath?: string
Expand Down Expand Up @@ -1256,6 +1257,14 @@ export interface RawRuleSetLogicalConditions {
not?: RawRuleSetCondition
}

export interface RawRuntimeChunkNameFnCtx {
name: string
}

export interface RawRuntimeChunkOptions {
name: string | ((entrypoint: { name: string }) => string)
}

export interface RawSnapshotOptions {
resolve: RawSnapshotStrategy
module: RawSnapshotStrategy
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 @@ -49,6 +49,7 @@ rspack_plugin_progress = { path = "../rspack_plugin_progress" }
rspack_plugin_real_content_hash = { path = "../rspack_plugin_real_content_hash" }
rspack_plugin_remove_empty_chunks = { path = "../rspack_plugin_remove_empty_chunks" }
rspack_plugin_runtime = { path = "../rspack_plugin_runtime" }
rspack_plugin_runtime_chunk = { path = "../rspack_plugin_runtime_chunk" }
rspack_plugin_schemes = { path = "../rspack_plugin_schemes" }
rspack_plugin_split_chunks = { path = "../rspack_plugin_split_chunks" }
rspack_plugin_swc_css_minimizer = { path = "../rspack_plugin_swc_css_minimizer" }
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod raw_ignore;
mod raw_limit_chunk_count;
mod raw_mf;
mod raw_progress;
mod raw_runtime_chunk;
mod raw_swc_js_minimizer;
mod raw_to_be_deprecated;

Expand Down Expand Up @@ -57,6 +58,7 @@ use rspack_plugin_runtime::{
enable_chunk_loading_plugin, ArrayPushCallbackChunkFormatPlugin, BundlerInfoPlugin,
ChunkPrefetchPreloadPlugin, CommonJsChunkFormatPlugin, ModuleChunkFormatPlugin, RuntimePlugin,
};
use rspack_plugin_runtime_chunk::RuntimeChunkPlugin;
use rspack_plugin_schemes::{DataUriPlugin, FileUriPlugin};
use rspack_plugin_swc_css_minimizer::SwcCssMinimizerRspackPlugin;
use rspack_plugin_swc_js_minimizer::SwcJsMinimizerRspackPlugin;
Expand All @@ -76,6 +78,7 @@ use self::{
raw_bundle_info::{RawBundlerInfoModeWrapper, RawBundlerInfoPluginOptions},
raw_css_extract::RawCssExtractPluginOption,
raw_mf::{RawConsumeSharedPluginOptions, RawContainerReferencePluginOptions, RawProvideOptions},
raw_runtime_chunk::RawRuntimeChunkOptions,
};
use crate::{
plugins::{CssExtractRspackAdditionalDataPlugin, JsLoaderResolverPlugin},
Expand Down Expand Up @@ -143,6 +146,7 @@ pub enum BuiltinPluginName {
ModuleConcatenationPlugin,
CssModulesPlugin,
APIPlugin,
RuntimeChunkPlugin,

// rspack specific plugins
// naming format follow XxxRspackPlugin
Expand Down Expand Up @@ -392,6 +396,10 @@ impl BuiltinPlugin {
}
BuiltinPluginName::CssModulesPlugin => plugins.push(CssPlugin::default().boxed()),
BuiltinPluginName::APIPlugin => plugins.push(APIPlugin::default().boxed()),
BuiltinPluginName::RuntimeChunkPlugin => plugins.push(
RuntimeChunkPlugin::new(downcast_into::<RawRuntimeChunkOptions>(self.options)?.into())
.boxed(),
),

// rspack specific plugins
BuiltinPluginName::HttpExternalsRspackPlugin => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ use rspack_plugin_mf::{
ContainerReferencePluginOptions, ExposeOptions, ProvideOptions, ProvideVersion, RemoteOptions,
};

use crate::RawLibraryOptions;
use crate::{RawEntryRuntime, RawEntryRuntimeWrapper, RawLibraryOptions};

#[derive(Debug)]
#[napi(object)]
pub struct RawContainerPluginOptions {
pub name: String,
pub share_scope: String,
pub library: RawLibraryOptions,
pub runtime: Option<String>,
#[napi(ts_type = "false | string")]
pub runtime: Option<RawEntryRuntime>,
pub filename: Option<String>,
pub exposes: Vec<RawExposeOptions>,
pub enhanced: bool,
Expand All @@ -27,7 +28,7 @@ impl From<RawContainerPluginOptions> for ContainerPluginOptions {
name: value.name,
share_scope: value.share_scope,
library: value.library.into(),
runtime: value.runtime,
runtime: value.runtime.map(|r| RawEntryRuntimeWrapper(r).into()),
filename: value.filename.map(|f| f.into()),
exposes: value.exposes.into_iter().map(|e| e.into()).collect(),
enhanced: value.enhanced,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use napi::Either;
use napi_derive::napi;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
use rspack_plugin_runtime_chunk::{RuntimeChunkName, RuntimeChunkOptions};

#[napi(object, object_to_js = false)]
pub struct RawRuntimeChunkOptions {
#[napi(ts_type = "string | ((entrypoint: { name: string }) => string)")]
pub name: RawRuntimeChunkName,
}

impl From<RawRuntimeChunkOptions> for RuntimeChunkOptions {
fn from(value: RawRuntimeChunkOptions) -> Self {
Self {
name: RawRuntimeChunkNameWrapper(value.name).into(),
}
}
}

type RawRuntimeChunkName = Either<String, ThreadsafeFunction<RawRuntimeChunkNameFnCtx, String>>;
struct RawRuntimeChunkNameWrapper(RawRuntimeChunkName);

#[napi(object)]
pub struct RawRuntimeChunkNameFnCtx {
pub name: String,
}

impl From<RawRuntimeChunkNameWrapper> for RuntimeChunkName {
fn from(value: RawRuntimeChunkNameWrapper) -> Self {
match value.0 {
Either::A(s) => {
if s == "single" {
Self::Single
} else if s == "multiple" {
Self::Multiple
} else {
Self::String(s)
}
}
Either::B(f) => RuntimeChunkName::Fn(Box::new(move |name| {
let f = f.clone();
Box::pin(async move {
f.call(RawRuntimeChunkNameFnCtx {
name: name.to_string(),
})
.await
})
})),
}
}
}
23 changes: 20 additions & 3 deletions crates/rspack_binding_options/src/options/raw_entry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use napi::Either;
use napi_derive::napi;
use rspack_core::EntryOptions;
use rspack_core::{EntryOptions, EntryRuntime};

use crate::RawLibraryOptions;

Expand All @@ -11,11 +12,27 @@ pub struct RawEntryPluginOptions {
pub options: RawEntryOptions,
}

pub type RawEntryRuntime = Either<bool, String>;
pub struct RawEntryRuntimeWrapper(pub RawEntryRuntime);

impl From<RawEntryRuntimeWrapper> for EntryRuntime {
fn from(value: RawEntryRuntimeWrapper) -> Self {
match value.0 {
Either::A(b) => {
assert!(!b, "RawEntryRuntime should be false or string");
Self::False
}
Either::B(s) => Self::String(s),
}
}
}

#[derive(Debug)]
#[napi(object)]
pub struct RawEntryOptions {
pub name: Option<String>,
pub runtime: Option<String>,
#[napi(ts_type = "false | string")]
pub runtime: Option<RawEntryRuntime>,
pub chunk_loading: Option<String>,
pub async_chunks: Option<bool>,
pub public_path: Option<String>,
Expand All @@ -29,7 +46,7 @@ impl From<RawEntryOptions> for EntryOptions {
fn from(value: RawEntryOptions) -> Self {
Self {
name: value.name,
runtime: value.runtime,
runtime: value.runtime.map(|r| RawEntryRuntimeWrapper(r).into()),
chunk_loading: value.chunk_loading.as_deref().map(Into::into),
async_chunks: value.async_chunks,
public_path: value.public_path.map(Into::into),
Expand Down
13 changes: 5 additions & 8 deletions crates/rspack_core/src/build_chunk_graph/code_splitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
add_connection_states, assign_depth, assign_depths, get_entry_runtime,
AsyncDependenciesBlockIdentifier, ChunkGroup, ChunkGroupKind, ChunkGroupOptions, ChunkGroupUkey,
ChunkLoading, ChunkUkey, Compilation, ConnectionId, ConnectionState, DependenciesBlock,
GroupOptions, Logger, ModuleGraph, ModuleIdentifier, RuntimeSpec,
EntryRuntime, GroupOptions, Logger, ModuleGraph, ModuleIdentifier, RuntimeSpec,
};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -302,7 +302,7 @@ impl<'me> CodeSplitter<'me> {
cgi
};

if options.depend_on.is_none() && options.runtime.is_none() {
if options.depend_on.is_none() && !matches!(&options.runtime, Some(EntryRuntime::String(_))) {
entrypoint.set_runtime_chunk(chunk.ukey);
}
entrypoint.set_entry_point_chunk(chunk.ukey);
Expand Down Expand Up @@ -466,7 +466,7 @@ Remove the 'runtime' option from the entrypoint."
entry_point.add_parent(parent);
}
}
} else if let Some(runtime) = &options.runtime {
} else if let Some(EntryRuntime::String(runtime)) = &options.runtime {
let ukey = self
.compilation
.entrypoints
Expand Down Expand Up @@ -1063,11 +1063,8 @@ Or do you want to use the entrypoints '{name}' and '{runtime}' independently on

let cgi = ChunkGroupInfo::new(
entrypoint.ukey,
RuntimeSpec::from_iter([entry_options
.runtime
.as_deref()
.expect("should have runtime for AsyncEntrypoint")
.into()]),
RuntimeSpec::from_entry_options(&entry_options)
.expect("should have runtime for AsyncEntrypoint"),
entry_options
.chunk_loading
.as_ref()
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl Chunk {
files: Default::default(),
auxiliary_files: Default::default(),
groups: Default::default(),
runtime: HashSet::default(),
runtime: RuntimeSpec::default(),
kind,
hash: None,
rendered_hash: None,
Expand Down
30 changes: 29 additions & 1 deletion crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,35 @@ impl ChunkGroupKind {
}
}

pub type EntryRuntime = String;
#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum EntryRuntime {
String(String),
#[default]
False,
}

impl From<&str> for EntryRuntime {
fn from(value: &str) -> Self {
Self::String(value.to_owned())
}
}

impl From<String> for EntryRuntime {
fn from(value: String) -> Self {
Self::String(value)
}
}

impl EntryRuntime {
pub fn as_string(&self) -> Option<&str> {
match self {
EntryRuntime::String(s) => Some(s),
EntryRuntime::False => None,
}
}
}

// pub type EntryRuntime = String;

#[derive(Debug, Default, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct EntryOptions {
Expand Down
Loading

0 comments on commit 57af683

Please sign in to comment.