Skip to content

Commit

Permalink
feat: parsing with correct SourceType (#179)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->

### Test Plan

<!-- e.g. is there anything you'd like reviewers to focus on? -->

---
  • Loading branch information
hyf0 committed Nov 6, 2023
1 parent 583c858 commit aeaf38b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
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) {
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

0 comments on commit aeaf38b

Please sign in to comment.