-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
RuntimeNormalModuleTask
(#40)
* feat: should have different kind of module task * Add RuntimeNormalModuleTask
- Loading branch information
Showing
7 changed files
with
180 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
#[allow(clippy::module_inception)] | ||
mod module_loader; | ||
mod module_task; | ||
mod normal_module_task; | ||
mod runtime_normal_module_task; | ||
mod task_result; | ||
|
||
pub use module_loader::ModuleLoader; | ||
|
||
use self::task_result::TaskResult; | ||
use self::task_result::NormalModuleTaskResult; | ||
pub enum Msg { | ||
Done(TaskResult), | ||
NormalModuleDone(NormalModuleTaskResult), | ||
RuntimeNormalModuleDone(NormalModuleTaskResult), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
crates/rolldown/src/bundler/module_loader/runtime_normal_module_task.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use oxc::{ | ||
ast::VisitMut, | ||
semantic::{ScopeTree, SymbolTable}, | ||
span::SourceType, | ||
}; | ||
use rolldown_common::{ModuleId, ResourceId}; | ||
use rolldown_oxc::{OxcCompiler, OxcProgram}; | ||
|
||
use super::Msg; | ||
use crate::{ | ||
bundler::{ | ||
graph::symbols::SymbolMap, | ||
module::module_builder::NormalModuleBuilder, | ||
module_loader::NormalModuleTaskResult, | ||
runtime::RUNTIME_PATH, | ||
visitors::scanner::{self, ScanResult}, | ||
}, | ||
BuildError, SharedResolver, | ||
}; | ||
pub struct RuntimeNormalModuleTask { | ||
module_id: ModuleId, | ||
tx: tokio::sync::mpsc::UnboundedSender<Msg>, | ||
errors: Vec<BuildError>, | ||
warnings: Vec<BuildError>, | ||
resolver: SharedResolver, | ||
} | ||
|
||
impl RuntimeNormalModuleTask { | ||
pub fn new( | ||
id: ModuleId, | ||
resolver: SharedResolver, | ||
tx: tokio::sync::mpsc::UnboundedSender<Msg>, | ||
) -> Self { | ||
Self { | ||
module_id: id, | ||
resolver, | ||
tx, | ||
errors: Default::default(), | ||
warnings: Default::default(), | ||
} | ||
} | ||
|
||
pub async fn run(self) -> anyhow::Result<()> { | ||
let mut builder = NormalModuleBuilder::default(); | ||
// load | ||
let source = include_str!("../runtime/index.js").to_string(); | ||
|
||
let (ast, scope, scan_result, symbol) = self.make_ast(source); | ||
|
||
let mut symbol_map = SymbolMap::from_symbol_table(symbol); | ||
|
||
let ScanResult { | ||
named_imports, | ||
named_exports, | ||
stmt_infos, | ||
import_records, | ||
star_exports, | ||
export_default_symbol_id, | ||
imports, | ||
module_resolution, | ||
} = scan_result; | ||
|
||
builder.id = Some(self.module_id); | ||
builder.ast = Some(ast); | ||
builder.path = Some(ResourceId::new( | ||
RUNTIME_PATH.to_string().into(), | ||
self.resolver.cwd(), | ||
)); | ||
builder.named_imports = Some(named_imports); | ||
builder.named_exports = Some(named_exports); | ||
builder.stmt_infos = Some(stmt_infos); | ||
builder.import_records = Some(import_records); | ||
builder.imports = Some(imports); | ||
builder.star_exports = Some(star_exports); | ||
builder.default_export_symbol = export_default_symbol_id; | ||
builder.scope = Some(scope); | ||
builder.module_resolution = module_resolution; | ||
builder.initialize_namespace_binding(&mut symbol_map); | ||
|
||
self | ||
.tx | ||
.send(Msg::RuntimeNormalModuleDone(NormalModuleTaskResult { | ||
resolved_deps: Default::default(), | ||
module_id: self.module_id, | ||
errors: self.errors, | ||
warnings: self.warnings, | ||
symbol_map, | ||
builder, | ||
})) | ||
.unwrap(); | ||
Ok(()) | ||
} | ||
|
||
fn make_ast(&self, source: String) -> (OxcProgram, ScopeTree, ScanResult, SymbolTable) { | ||
let source_type = SourceType::default(); | ||
let mut program = OxcCompiler::parse(source, source_type); | ||
|
||
let semantic = program.make_semantic(source_type); | ||
let (mut symbol_table, mut scope) = semantic.into_symbol_table_and_scope_tree(); | ||
let mut scanner = scanner::Scanner::new( | ||
self.module_id, | ||
&mut scope, | ||
&mut symbol_table, | ||
"should be unreachable for runtime module", | ||
); | ||
scanner.visit_program(program.program_mut()); | ||
let scan_result = scanner.result; | ||
|
||
(program, scope, scan_result, symbol_table) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,15 @@ | ||
use rolldown_common::{ImportRecordId, ModuleId}; | ||
|
||
use crate::bundler::graph::symbols::SymbolMap; | ||
use crate::bundler::module::module_builder::ModuleBuilder; | ||
use crate::bundler::module::module_builder::NormalModuleBuilder; | ||
use crate::bundler::resolve_id::ResolvedRequestInfo; | ||
use crate::BuildError; | ||
|
||
pub struct TaskResult { | ||
pub struct NormalModuleTaskResult { | ||
pub module_id: ModuleId, | ||
pub symbol_map: SymbolMap, | ||
pub resolved_deps: Vec<(ImportRecordId, ResolvedRequestInfo)>, | ||
pub errors: Vec<BuildError>, | ||
pub warnings: Vec<BuildError>, | ||
pub builder: ModuleBuilder, | ||
pub builder: NormalModuleBuilder, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters