Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: using rspack to bundle #1

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
2,909 changes: 2,409 additions & 500 deletions Cargo.lock

Large diffs are not rendered by default.

22 changes: 20 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ cbc = { version = "=0.1.2", features = ["alloc"] }
# Instead use util::time::utc_now()
chrono = { version = "0.4", default-features = false, features = ["std", "serde"] }
console_static_text = "=0.8.1"
data-encoding = "2.3.3"
data-encoding = "2.6.0"
data-url = "=0.3.0"
deno_cache_dir = "=0.10.2"
deno_package_json = { version = "=0.1.1", default-features = false }
Expand Down Expand Up @@ -188,7 +188,7 @@ tower-lsp = { version = "=0.20.0", features = ["proposed"] }
tower-service = "0.3.2"
twox-hash = "=1.6.3"
# Upgrading past 2.4.1 may cause WPT failures
url = { version = "< 2.5.0", features = ["serde", "expose_internals"] }
url = { version = "2.4.1", features = ["serde", "expose_internals"] }
uuid = { version = "1.3.0", features = ["v4"] }
webpki-roots = "0.26"
which = "4.2.5"
Expand Down Expand Up @@ -217,6 +217,24 @@ winapi = "=0.3.9"
windows-sys = { version = "0.52.0", features = ["Win32_Foundation", "Win32_Media", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_WindowsProgramming", "Wdk", "Wdk_System", "Wdk_System_SystemInformation", "Win32_System_Pipes", "Wdk_Storage_FileSystem", "Win32_System_Registry"] }
winres = "=0.1.12"

# rspack deps
rspack_plugin_schemes= { version = "0.1.0"}
rspack_ids= { version="0.1.0"}
rspack_plugin_javascript= { version = "0.1.0" }
rspack_error = { version = "0.1.0"}
rspack_core = { version = "0.1.0"}
rspack_plugin_entry = { version = "0.1.0" }
rspack_fs = { version = "0.1.0" }
rspack_plugin_runtime = { version = "0.1.0"}



[patch.crates-io]
# patch to use Arc instead of Lrc to support concurrent
deno_ast = { git="https://github.com/hardfist/deno_ast", branch = "yj/concurrent_ast"}
deno_emit = { git="https://github.com/hardfist/deno_emit", branch = "concurrent"}


# NB: the `bench` and `release` profiles must remain EXACTLY the same.
[profile.release]
codegen-units = 1
Expand Down
11 changes: 11 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,17 @@ which.workspace = true
zeromq.workspace = true
zstd.workspace = true

# rspack deps
rspack_plugin_schemes= { workspace = true}
rspack_ids= { workspace = true}
rspack_plugin_javascript= { workspace=true}
rspack_error = { workspace = true}
rspack_core = { workspace = true}
rspack_plugin_entry = { workspace =true }
rspack_fs = { workspace=true}
rspack_plugin_runtime = { workspace = true}
swc_core = { version = "0.99.6"}

[target.'cfg(windows)'.dependencies]
junction.workspace = true
winapi = { workspace = true, features = ["knownfolders", "mswsock", "objbase", "shlobj", "tlhelp32", "winbase", "winerror", "winsock2"] }
Expand Down
11 changes: 7 additions & 4 deletions cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

mod args;
mod auth_tokens;
mod cache;
Expand Down Expand Up @@ -106,9 +105,13 @@ async fn run_subcommand(flags: Arc<Flags>) -> Result<i32, AnyError> {
tools::bench::run_benchmarks(flags, bench_flags).await
}
}),
DenoSubcommand::Bundle(bundle_flags) => spawn_subcommand(async {
tools::bundle::bundle(flags, bundle_flags).await
}),
DenoSubcommand::Bundle(bundle_flags) => {
let result = tokio::spawn(async move {
//tools::bundle::bundle(flags, bundle_flags).await
tools::pack::pack(flags, bundle_flags).await
}).await;
return result.map(|_| 0).map_err(|e|e.into());
},
DenoSubcommand::Doc(doc_flags) => {
spawn_subcommand(async { tools::doc::doc(flags, doc_flags).await })
}
Expand Down
3 changes: 1 addition & 2 deletions cli/tools/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

use std::path::PathBuf;
use std::sync::Arc;

Expand All @@ -15,7 +14,7 @@ use crate::factory::CliFactory;
use crate::graph_util::error_for_any_npm_specifier;
use crate::util;
use crate::util::display;

#[allow(dead_code)]
pub async fn bundle(
flags: Arc<Flags>,
bundle_flags: BundleFlags,
Expand Down
1 change: 1 addition & 0 deletions cli/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ pub mod task;
pub mod test;
pub mod upgrade;
pub mod vendor;
pub mod pack;
26 changes: 26 additions & 0 deletions cli/tools/pack.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.

mod rspack_bundle;
use std::sync::Arc;

use deno_core::error::AnyError;

use deno_terminal::colors;

use crate::args::BundleFlags;
use crate::args::Flags;

use crate::factory::CliFactory;
use crate::tools::pack::rspack_bundle::rspack;
pub async fn pack(
flags: Arc<Flags>,
bundle_flags: BundleFlags,
) -> Result<(), AnyError> {
log::info!(
"{}",
colors::yellow("⚠️ Using Rspack to Bundle"),
);
let factory = CliFactory::from_flags(flags);
rspack(factory, &bundle_flags).await?;
Ok(())
}
165 changes: 165 additions & 0 deletions cli/tools/pack/rspack_bundle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
use std::sync::Arc;
use deno_core::error::AnyError;
use rspack_core::ResolverFactory;
use rspack_ids::NaturalChunkIdsPlugin;
use rspack_ids::NamedModuleIdsPlugin;
use rspack_core::JavascriptParserOptions;
use rspack_core::ModuleType;
use rspack_core::ParserOptions;
use rspack_core::ParserOptionsByModuleType;
use rspack_plugin_runtime::{RuntimePlugin};
use rspack_core::{
CacheOptions, ChunkLoading, ChunkLoadingType, Compiler, CompilerOptions, Context,
CrossOriginLoading, DevServerOptions, EntryOptions, Environment, Experiments, Filename,
HashDigest, HashFunction, HashSalt, MangleExportsOption, Mode, ModuleOptions, Optimization,
OutputOptions, PathInfo, Plugin, PublicPath, Resolve, SideEffectOption, SnapshotOptions,
StatsOptions, Target, UsedExportsOption, WasmLoading,
};
use rspack_fs::AsyncNativeFileSystem;
use rspack_plugin_entry::EntryPlugin;
use rspack_plugin_javascript::JsPlugin;
use rspack_plugin_schemes::DataUriPlugin;
use crate::{CliFactory};
use crate::args::BundleFlags;

pub async fn rspack(
_factory: CliFactory,
bundle_flags: &BundleFlags,
) -> Result<(), AnyError> {
let output_filesystem = AsyncNativeFileSystem {};
let root = std::env::current_dir().unwrap();
let context = Context::from(root.clone());
let entry_request: String = root
.join(bundle_flags.source_file.clone())
.canonicalize()
.unwrap()
.to_string_lossy()
.to_string();
let options = CompilerOptions {
context: Context::from(root.clone()),
dev_server: DevServerOptions::default(),
output: OutputOptions {
chunk_load_timeout: Default::default(),
charset: Default::default(),
css_head_data_compression: Default::default(),
import_meta_name: Default::default(),
path: root.clone(),
pathinfo: PathInfo::Bool(false),
clean: false,
public_path: PublicPath::Auto,
asset_module_filename: Filename::from(String::from("asset-[name].js")),
wasm_loading: WasmLoading::Disable,
webassembly_module_filename: Filename::from(String::from("webassembly.js")),
unique_name: "main".into(),
chunk_loading: ChunkLoading::Enable(ChunkLoadingType::Import),
chunk_loading_global: String::new(),
filename: Filename::from(String::from(bundle_flags.out_file.clone().unwrap_or("output.js".to_string()))),
chunk_filename: Filename::from(String::from("[id].js")),
cross_origin_loading: CrossOriginLoading::Disable,
css_filename: Filename::from(String::from("[name].css")),
css_chunk_filename: Filename::from(String::from("[id].css")),
hot_update_main_filename: Filename::from(String::from("[name].[hash].hot-update.js")),
hot_update_chunk_filename: Filename::from(String::from("[id].[hash].hot-update.js")),
hot_update_global: String::new(),
library: None,
enabled_library_types: None,
strict_module_error_handling: false,
global_object: String::from("window"),
import_function_name: String::from("import"),
iife: false,
module: false,
trusted_types: None,
source_map_filename: Filename::from(String::from("[file].map")),
hash_function: HashFunction::MD4,
hash_digest: HashDigest::Hex,
hash_digest_length: 20,
hash_salt: HashSalt::Salt(String::from("salt")),
async_chunks: false,
worker_chunk_loading: ChunkLoading::Disable,
worker_wasm_loading: WasmLoading::Disable,
worker_public_path: String::new(),
script_type: String::from("text/javascript"),
environment: Environment {
r#const: Some(true),
arrow_function: Some(true),
},
},
target: Target::new(&vec!["es2022".to_string(), "node".to_string()]).unwrap(),
mode: Mode::Development,
resolve: Resolve {
extensions: Some(vec![".js".to_string()]),
..Default::default()
},
resolve_loader: Resolve {
extensions: Some(vec![".js".to_string()]),
..Default::default()
},
module: ModuleOptions {
parser: Some(ParserOptionsByModuleType::from_iter([(
ModuleType::JsAuto,
ParserOptions::Javascript(JavascriptParserOptions {
dynamic_import_fetch_priority: Default::default(),
override_strict: Default::default(),
import_meta: Default::default(),
dynamic_import_mode: rspack_core::DynamicImportMode::Eager,
dynamic_import_prefetch: rspack_core::JavascriptParserOrder::Order(1),
dynamic_import_preload: rspack_core::JavascriptParserOrder::Order(1),
url: rspack_core::JavascriptParserUrl::Disable,
expr_context_critical: false,
wrapped_context_critical: false,
exports_presence: None,
import_exports_presence: None,
reexport_exports_presence: None,
strict_export_presence: false,
worker: vec![],
}),
)])),
// generator: Some(GeneratorOptionsByModuleType::from_iter(generator.iter())),
..Default::default()
},
stats: StatsOptions::default(),
snapshot: SnapshotOptions,
cache: CacheOptions::default(),
experiments: Experiments::default(),
optimization: Optimization {
concatenate_modules: false,
remove_available_modules: false,
provided_exports: false,
mangle_exports: MangleExportsOption::False,
inner_graph: true,
used_exports: UsedExportsOption::default(),
side_effects: SideEffectOption::default(),
},
profile: false,
bail: false,
__references:Default::default(),
node: None,
};
let mut plugins: Vec<Box<dyn Plugin>> = Vec::new();

let plugin_options = EntryOptions {
name: Some("main".to_string()),
runtime: None,
chunk_loading: None,
async_chunks: None,
public_path: None,
base_uri: None,
filename:None,
library: None,
depend_on: None,
layer: Default::default(),
};

let entry_plugin = Box::new(EntryPlugin::new(context, entry_request, plugin_options));
plugins.push(Box::<JsPlugin>::default());
plugins.push(entry_plugin);
plugins.push(Box::<NaturalChunkIdsPlugin>::default());
plugins.push(Box::<NamedModuleIdsPlugin>::default());
plugins.push(Box::<DataUriPlugin>::default());
plugins.push(Box::<RuntimePlugin>::default());
let resolver_factory = Arc::new(ResolverFactory::new(options.resolve.clone()));
let loader_resolver_factory = Arc::new(ResolverFactory::new(options.resolve_loader.clone()));
let mut compiler = Compiler::new(options, plugins, output_filesystem, resolver_factory, loader_resolver_factory);
compiler.build().await.expect("build failed");
Ok(())
}
3 changes: 2 additions & 1 deletion runtime/tokio_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub fn create_basic_runtime() -> tokio::runtime::Runtime {
let (event_interval, global_queue_interval, max_io_events_per_tick) =
tokio_configuration();

tokio::runtime::Builder::new_current_thread()
tokio::runtime::Builder::new_multi_thread()
Copy link
Owner Author

Choose a reason for hiding this comment

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

rspack only support multi thread mode now, but can support single thread in the future


.enable_io()
.enable_time()
.event_interval(tokio_env("DENO_TOKIO_EVENT_INTERVAL", event_interval))
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.80.0"
channel = "nightly-2024-06-07"
Copy link
Owner Author

Choose a reason for hiding this comment

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

Rspack use nightly version of rust,but can switch to stable version in the future

components = ["rustfmt", "clippy"]
2 changes: 1 addition & 1 deletion tests/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository.workspace = true
crate-type = ["cdylib"]

[dependencies]
napi-sys = { version = "=2.2.2", default-features = false, features = ["napi7"] }
napi-sys = { version = "2.4.0", default-features = false, features = ["napi7"] }

[dev-dependencies]
test_util.workspace = true
Expand Down
1 change: 1 addition & 0 deletions tests/testdata/bundle/npm_import/answer.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const answer = 42;
2 changes: 2 additions & 0 deletions tests/testdata/bundle/npm_import/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { answer } from "./answer.mjs";
console.log(answer);
2 changes: 1 addition & 1 deletion tests/util/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ regex.workspace = true
reqwest.workspace = true
rustls-pemfile.workspace = true
rustls-tokio-stream.workspace = true
semver = "=1.0.14"
semver = "1.0.14"
serde.workspace = true
serde_json.workspace = true
sha2.workspace = true
Expand Down