Skip to content

Commit

Permalink
init next-app-loader
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Jan 13, 2025
1 parent 36d3379 commit 517b94f
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ rspack_identifier = { version = "0.2.0", path = "crates/rsp
rspack_ids = { version = "0.2.0", path = "crates/rspack_ids" }
rspack_loader = { version = "0.2.0", path = "crates/rspack_loader" }
rspack_loader_lightningcss = { version = "0.2.0", path = "crates/rspack_loader_lightningcss" }
rspack_loader_next_app = { version = "0.2.0", path = "crates/rspack_loader_next_app" }
rspack_loader_next_swc = { version = "0.2.0", path = "crates/rspack_loader_next_swc" }
rspack_loader_preact_refresh = { version = "0.2.0", path = "crates/rspack_loader_preact_refresh" }
rspack_loader_react_refresh = { version = "0.2.0", path = "crates/rspack_loader_react_refresh" }
Expand Down
3 changes: 2 additions & 1 deletion crates/node_binding/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ rspack_fs = { workspace = true }
rspack_fs_node = { workspace = true }
rspack_hash = { workspace = true }
rspack_hook = { workspace = true }
rspack_loader_next_app = { workspace = true }
rspack_loader_next_swc = { workspace = true }
rspack_napi = { workspace = true }
rspack_paths = { workspace = true }
rspack_plugin_html = { workspace = true }
rspack_plugin_javascript = { workspace = true }
rspack_util = { workspace = true }
rspack_loader_next_swc = { workspace = true }

rspack_tracing = { workspace = true }

Expand Down
7 changes: 7 additions & 0 deletions crates/node_binding/src/plugins/js_loader/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ pub async fn get_builtin_loader(builtin: &str, options: Option<&str>) -> Result<
return Ok(loader);
}

if builtin.starts_with(rspack_loader_next_app::NEXT_APP_LOADER_IDENTIFIER) {
return Ok(Arc::new(rspack_loader_next_app::NextAppLoader::new(
rspack_loader_next_app::Options {},
builtin.into(),
)));
}

if builtin.starts_with(LIGHTNINGCSS_LOADER_IDENTIFIER) {
let config: rspack_loader_lightningcss::config::RawConfig =
serde_json::from_str(options.as_ref()).map_err(|e| {
Expand Down
21 changes: 21 additions & 0 deletions crates/rspack_loader_next_app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
description = "rspack builtin next app loader"
edition = "2021"
license = "MIT"
name = "rspack_loader_next_app"
repository = "https://github.com/web-infra-dev/rspack"
version = "0.2.0"

[dependencies]
async-trait = { workspace = true }
rspack_ast = { workspace = true }
rspack_cacheable = { workspace = true }
rspack_core = { workspace = true }
rspack_error = { workspace = true }
rspack_loader_runner = { workspace = true }
rspack_paths = { workspace = true }
rspack_plugin_javascript = { workspace = true }
rspack_swc_plugin_import = { workspace = true }
rspack_util = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
22 changes: 22 additions & 0 deletions crates/rspack_loader_next_app/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2022-present Bytedance, Inc. and its affiliates.


Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

66 changes: 66 additions & 0 deletions crates/rspack_loader_next_app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
mod create_tree_code_from_path;
mod load_entrypoint;
mod options;

use std::borrow::Cow;

use rspack_cacheable::{cacheable, cacheable_dyn};
use rspack_core::{Loader, LoaderContext, RunnerContext};
use rspack_error::Result;
use rspack_loader_runner::{Identifiable, Identifier};

pub use crate::options::Options;

pub const NEXT_APP_LOADER_IDENTIFIER: &str = "builtin:next-app-loader";

#[cacheable]
#[derive(Debug)]
pub struct NextAppLoader {
id: Identifier,
options: Options,
}

impl NextAppLoader {
pub fn new(options: Options, ident: &str) -> Self {
Self {
id: ident.into(),
options,
}
}

async fn loader_impl(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
let Some(resource_path) = loader_context.resource_path() else {
return Ok(());
};

let filename = resource_path.as_str().to_string();

let Some(content) = loader_context.take_content() else {
return Ok(());
};

let content_str = match &content {
rspack_core::Content::String(s) => Cow::Borrowed(s.as_str()),
rspack_core::Content::Buffer(buf) => String::from_utf8_lossy(buf),
};

loader_context.finish_with("".to_string());

Ok(())
}
}

impl Identifiable for NextAppLoader {
fn identifier(&self) -> rspack_loader_runner::Identifier {
self.id
}
}

#[cacheable_dyn]
#[async_trait::async_trait]
impl Loader<RunnerContext> for NextAppLoader {
async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
// for better diagnostic, as async_trait macro don't show beautiful error message
self.loader_impl(loader_context).await
}
}
1 change: 1 addition & 0 deletions crates/rspack_loader_next_app/src/load_entrypoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

5 changes: 5 additions & 0 deletions crates/rspack_loader_next_app/src/options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use rspack_cacheable::cacheable;

#[cacheable]
#[derive(Debug)]
pub struct Options {}

0 comments on commit 517b94f

Please sign in to comment.