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

feat: parsing with correct SourceType #179

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions crates/rolldown/src/bundler/module_loader/normal_module_task.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{path::Path, sync::Arc};

use futures::future::join_all;
use index_vec::IndexVec;
Expand Down Expand Up @@ -66,7 +66,7 @@ impl<T: FileSystemExt> NormalModuleTask<T> {
let source = self.fs.read_to_string(self.path.as_path())?;
// TODO: transform

let (ast, scope, scan_result, symbol, namespace_symbol) = self.make_ast(source);
let (ast, scope, scan_result, ast_symbol, namespace_symbol) = self.scan(source);

let res = self.resolve_dependencies(&scan_result.import_records).await?;

Expand Down Expand Up @@ -105,15 +105,37 @@ impl<T: FileSystemExt> NormalModuleTask<T> {
module_id: self.module_id,
errors: self.errors,
warnings: self.warnings,
ast_symbol: symbol,
ast_symbol,
builder,
}))
.unwrap();
Ok(())
}

fn make_ast(&self, source: String) -> (OxcProgram, AstScope, ScanResult, AstSymbol, SymbolRef) {
let source_type = SourceType::from_path(self.path.as_ref()).unwrap();
fn scan(&self, source: String) -> (OxcProgram, AstScope, ScanResult, AstSymbol, SymbolRef) {
Boshen marked this conversation as resolved.
Show resolved Hide resolved
fn determine_oxc_source_type(path: impl AsRef<Path>, ty: ModuleType) -> SourceType {
// Determine oxc source type for parsing
let mut default = SourceType::default().with_module(true);
// Rolldown considers module as esm by default.
debug_assert!(default.is_module());
debug_assert!(default.is_javascript());
debug_assert!(!default.is_jsx());
let extension = path.as_ref().extension().and_then(std::ffi::OsStr::to_str);
default = match ty {
ModuleType::CJS | ModuleType::CjsPackageJson => default.with_script(true),
_ => default,
};
if let Some(ext) = extension {
default = match ext {
"cjs" => default.with_script(true),
"jsx" => default.with_jsx(true),
_ => default,
};
};
default
}

let source_type = determine_oxc_source_type(self.path.as_path(), self.module_type);
let mut program = OxcCompiler::parse(source, source_type);

let semantic = program.make_semantic(source_type);
Expand Down
12 changes: 0 additions & 12 deletions crates/rolldown_oxc/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ unsafe impl Send for OxcProgram {}
unsafe impl Sync for OxcProgram {}

impl OxcProgram {
pub fn from_source(source: String, ty: SourceType) -> Self {
let source = Box::pin(source);
let allocator = Box::pin(oxc::allocator::Allocator::default());
let program = unsafe {
let source = std::mem::transmute::<_, &'static String>(source.as_ref());
let alloc = std::mem::transmute::<_, &'static Allocator>(allocator.as_ref());
Parser::new(alloc, source, ty).parse().program
};

Self { program, source, allocator }
}

pub fn source(&self) -> &str {
self.source.as_str()
}
Expand Down