Skip to content

Commit 517b94f

Browse files
committed
init next-app-loader
1 parent 36d3379 commit 517b94f

File tree

10 files changed

+145
-1
lines changed

10 files changed

+145
-1
lines changed

Cargo.lock

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ rspack_identifier = { version = "0.2.0", path = "crates/rsp
128128
rspack_ids = { version = "0.2.0", path = "crates/rspack_ids" }
129129
rspack_loader = { version = "0.2.0", path = "crates/rspack_loader" }
130130
rspack_loader_lightningcss = { version = "0.2.0", path = "crates/rspack_loader_lightningcss" }
131+
rspack_loader_next_app = { version = "0.2.0", path = "crates/rspack_loader_next_app" }
131132
rspack_loader_next_swc = { version = "0.2.0", path = "crates/rspack_loader_next_swc" }
132133
rspack_loader_preact_refresh = { version = "0.2.0", path = "crates/rspack_loader_preact_refresh" }
133134
rspack_loader_react_refresh = { version = "0.2.0", path = "crates/rspack_loader_react_refresh" }

crates/node_binding/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ rspack_fs = { workspace = true }
2222
rspack_fs_node = { workspace = true }
2323
rspack_hash = { workspace = true }
2424
rspack_hook = { workspace = true }
25+
rspack_loader_next_app = { workspace = true }
26+
rspack_loader_next_swc = { workspace = true }
2527
rspack_napi = { workspace = true }
2628
rspack_paths = { workspace = true }
2729
rspack_plugin_html = { workspace = true }
2830
rspack_plugin_javascript = { workspace = true }
2931
rspack_util = { workspace = true }
30-
rspack_loader_next_swc = { workspace = true }
3132

3233
rspack_tracing = { workspace = true }
3334

crates/node_binding/src/plugins/js_loader/resolver.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,13 @@ pub async fn get_builtin_loader(builtin: &str, options: Option<&str>) -> Result<
115115
return Ok(loader);
116116
}
117117

118+
if builtin.starts_with(rspack_loader_next_app::NEXT_APP_LOADER_IDENTIFIER) {
119+
return Ok(Arc::new(rspack_loader_next_app::NextAppLoader::new(
120+
rspack_loader_next_app::Options {},
121+
builtin.into(),
122+
)));
123+
}
124+
118125
if builtin.starts_with(LIGHTNINGCSS_LOADER_IDENTIFIER) {
119126
let config: rspack_loader_lightningcss::config::RawConfig =
120127
serde_json::from_str(options.as_ref()).map_err(|e| {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
description = "rspack builtin next app loader"
3+
edition = "2021"
4+
license = "MIT"
5+
name = "rspack_loader_next_app"
6+
repository = "https://github.com/web-infra-dev/rspack"
7+
version = "0.2.0"
8+
9+
[dependencies]
10+
async-trait = { workspace = true }
11+
rspack_ast = { workspace = true }
12+
rspack_cacheable = { workspace = true }
13+
rspack_core = { workspace = true }
14+
rspack_error = { workspace = true }
15+
rspack_loader_runner = { workspace = true }
16+
rspack_paths = { workspace = true }
17+
rspack_plugin_javascript = { workspace = true }
18+
rspack_swc_plugin_import = { workspace = true }
19+
rspack_util = { workspace = true }
20+
serde = { workspace = true, features = ["derive"] }
21+
serde_json = { workspace = true }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2022-present Bytedance, Inc. and its affiliates.
4+
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
mod create_tree_code_from_path;
2+
mod load_entrypoint;
3+
mod options;
4+
5+
use std::borrow::Cow;
6+
7+
use rspack_cacheable::{cacheable, cacheable_dyn};
8+
use rspack_core::{Loader, LoaderContext, RunnerContext};
9+
use rspack_error::Result;
10+
use rspack_loader_runner::{Identifiable, Identifier};
11+
12+
pub use crate::options::Options;
13+
14+
pub const NEXT_APP_LOADER_IDENTIFIER: &str = "builtin:next-app-loader";
15+
16+
#[cacheable]
17+
#[derive(Debug)]
18+
pub struct NextAppLoader {
19+
id: Identifier,
20+
options: Options,
21+
}
22+
23+
impl NextAppLoader {
24+
pub fn new(options: Options, ident: &str) -> Self {
25+
Self {
26+
id: ident.into(),
27+
options,
28+
}
29+
}
30+
31+
async fn loader_impl(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
32+
let Some(resource_path) = loader_context.resource_path() else {
33+
return Ok(());
34+
};
35+
36+
let filename = resource_path.as_str().to_string();
37+
38+
let Some(content) = loader_context.take_content() else {
39+
return Ok(());
40+
};
41+
42+
let content_str = match &content {
43+
rspack_core::Content::String(s) => Cow::Borrowed(s.as_str()),
44+
rspack_core::Content::Buffer(buf) => String::from_utf8_lossy(buf),
45+
};
46+
47+
loader_context.finish_with("".to_string());
48+
49+
Ok(())
50+
}
51+
}
52+
53+
impl Identifiable for NextAppLoader {
54+
fn identifier(&self) -> rspack_loader_runner::Identifier {
55+
self.id
56+
}
57+
}
58+
59+
#[cacheable_dyn]
60+
#[async_trait::async_trait]
61+
impl Loader<RunnerContext> for NextAppLoader {
62+
async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
63+
// for better diagnostic, as async_trait macro don't show beautiful error message
64+
self.loader_impl(loader_context).await
65+
}
66+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use rspack_cacheable::cacheable;
2+
3+
#[cacheable]
4+
#[derive(Debug)]
5+
pub struct Options {}

0 commit comments

Comments
 (0)