|
| 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 | +} |
0 commit comments