-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! print js error in console.log
- Loading branch information
Showing
18 changed files
with
452 additions
and
92 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,98 @@ | ||
use anyhow::anyhow; | ||
use oxc_allocator::Allocator; | ||
use oxc_ast::{AstKind, Visit}; | ||
use oxc_codegen::{Codegen, CodegenOptions}; | ||
use oxc_semantic::SemanticBuilder; | ||
use oxc_span::{GetSpan, SourceType}; | ||
use oxc_transformer::{TransformOptions, TransformTarget, Transformer}; | ||
use tracing::Instrument; | ||
|
||
use crate::compile::ModuleDependency; | ||
|
||
#[derive(Debug, Default)] | ||
struct ImportParser { | ||
sync_imports: Vec<String>, | ||
async_imports: Vec<String>, | ||
} | ||
|
||
impl<'a> oxc_ast::Visit<'a> for ImportParser { | ||
fn visit_import_declaration(&mut self, decl: &oxc_ast::ast::ImportDeclaration<'a>) { | ||
let kind = AstKind::ImportDeclaration(self.alloc(decl)); | ||
self.enter_node(kind); | ||
if let Some(specifiers) = &decl.specifiers { | ||
for specifer in specifiers { | ||
self.visit_import_declaration_specifier(specifer); | ||
} | ||
} | ||
self.sync_imports.push(decl.source.value.to_string()); | ||
self.leave_node(kind); | ||
} | ||
|
||
fn visit_import_declaration_specifier( | ||
&mut self, | ||
specifier: &oxc_ast::ast::ImportDeclarationSpecifier, | ||
) { | ||
use oxc_ast::ast::ImportDeclarationSpecifier; | ||
match specifier { | ||
ImportDeclarationSpecifier::ImportSpecifier(import_specifer) => { | ||
println!("ImportDeclarationSpecifier: {:?}", import_specifer.span); | ||
} | ||
ImportDeclarationSpecifier::ImportDefaultSpecifier(import_specfier) => { | ||
println!("ImportDeclarationSpecifier: {:?}", import_specfier.span); | ||
} | ||
ImportDeclarationSpecifier::ImportNamespaceSpecifier(import_specifer) => { | ||
println!("ImportDeclarationSpecifier: {:?}", import_specifer.span); | ||
} | ||
} | ||
} | ||
fn visit_import_expression(&mut self, expr: &oxc_ast::ast::ImportExpression<'a>) { | ||
let kind = AstKind::ImportExpression(self.alloc(expr)); | ||
self.enter_node(kind); | ||
if let oxc_ast::ast::Expression::StringLiteral(v) = &expr.source { | ||
self.async_imports.push(v.value.to_string()) | ||
} | ||
self.leave_node(kind); | ||
} | ||
} | ||
|
||
pub fn compile(file_name: &str, source: &str) -> anyhow::Result<ModuleDependency> { | ||
let allo = Allocator::default(); | ||
let source_type = oxc_span::SourceType::from_path(file_name).unwrap(); | ||
|
||
let ret = oxc_parser::Parser::new(&allo, &source, source_type).parse(); | ||
|
||
if !ret.errors.is_empty() { | ||
for error in ret.errors { | ||
let error = error.with_source_code(source.to_string()); | ||
println!("{error:?}"); | ||
} | ||
return Err(anyhow!("compile error")); | ||
} | ||
|
||
let mut pass = ImportParser::default(); | ||
pass.visit_program(&ret.program); | ||
|
||
let semantic = SemanticBuilder::new(&source, source_type) | ||
.with_trivias(ret.trivias) | ||
.build(&ret.program) | ||
.semantic; | ||
|
||
let program = allo.alloc(ret.program); | ||
let transform_options = TransformOptions::default(); | ||
|
||
Transformer::new(&allo, source_type, semantic, transform_options) | ||
.build(program) | ||
.unwrap(); | ||
|
||
let code = Codegen::<false>::new(source.len(), CodegenOptions).build(program); | ||
|
||
Ok(ModuleDependency { | ||
deps: pass.sync_imports, | ||
async_deps: pass.async_imports, | ||
specifier: vec![], | ||
source: code, | ||
map: None, | ||
filename: file_name.to_string(), | ||
is_main: false, | ||
}) | ||
} |
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
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
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
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,9 +1,10 @@ | ||
// import "https://deno.land/[email protected]/examples/welcome.ts"; | ||
// import "https://deno.land/[email protected]/examples/welcome.ts"; | ||
import "./welcome.ts" | ||
|
||
const res = await import("./test1.ts"); | ||
console.log("res", res); | ||
const text = await res.json(); | ||
console.log("🚀 text", text); | ||
test.copy(); | ||
|
||
// const res = await import("./test1.ts"); | ||
// console.log("res", res); | ||
// const text = await res.json(); | ||
// console.log("🚀 text", text); | ||
// test.copy(); | ||
// import("./welcome.ts") |
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 @@ | ||
console.log("Welcome edon!"); |