mirror of
https://github.com/swc-project/swc.git
synced 2024-11-26 09:54:22 +03:00
fix(es/module): Rewrite import specifier in type declaration (#9577)
Some checks are pending
CI / Cargo fmt (push) Waiting to run
CI / Cargo clippy (push) Waiting to run
CI / Check license of dependencies (push) Waiting to run
CI / Check (macos-latest) (push) Waiting to run
CI / Check (ubuntu-latest) (push) Waiting to run
CI / Check (windows-latest) (push) Waiting to run
CI / Test wasm (binding_core_wasm) (push) Waiting to run
CI / Test wasm (binding_minifier_wasm) (push) Waiting to run
CI / Test wasm (binding_typescript_wasm) (push) Waiting to run
CI / List crates (push) Waiting to run
CI / Test - ${{ matrix.settings.crate }} - ${{ matrix.settings.os }} (push) Blocked by required conditions
CI / Test node bindings - ${{ matrix.os }} (macos-latest) (push) Waiting to run
CI / Test node bindings - ${{ matrix.os }} (windows-latest) (push) Waiting to run
CI / Test with @swc/cli (push) Waiting to run
CI / Miri (better_scoped_tls) (push) Waiting to run
CI / Miri (string_enum) (push) Waiting to run
CI / Miri (swc) (push) Waiting to run
CI / Miri (swc_bundler) (push) Waiting to run
CI / Miri (swc_ecma_codegen) (push) Waiting to run
CI / Miri (swc_ecma_minifier) (push) Waiting to run
CI / Done (push) Blocked by required conditions
Benchmark / Bench everything (push) Waiting to run
Some checks are pending
CI / Cargo fmt (push) Waiting to run
CI / Cargo clippy (push) Waiting to run
CI / Check license of dependencies (push) Waiting to run
CI / Check (macos-latest) (push) Waiting to run
CI / Check (ubuntu-latest) (push) Waiting to run
CI / Check (windows-latest) (push) Waiting to run
CI / Test wasm (binding_core_wasm) (push) Waiting to run
CI / Test wasm (binding_minifier_wasm) (push) Waiting to run
CI / Test wasm (binding_typescript_wasm) (push) Waiting to run
CI / List crates (push) Waiting to run
CI / Test - ${{ matrix.settings.crate }} - ${{ matrix.settings.os }} (push) Blocked by required conditions
CI / Test node bindings - ${{ matrix.os }} (macos-latest) (push) Waiting to run
CI / Test node bindings - ${{ matrix.os }} (windows-latest) (push) Waiting to run
CI / Test with @swc/cli (push) Waiting to run
CI / Miri (better_scoped_tls) (push) Waiting to run
CI / Miri (string_enum) (push) Waiting to run
CI / Miri (swc) (push) Waiting to run
CI / Miri (swc_bundler) (push) Waiting to run
CI / Miri (swc_ecma_codegen) (push) Waiting to run
CI / Miri (swc_ecma_minifier) (push) Waiting to run
CI / Done (push) Blocked by required conditions
Benchmark / Bench everything (push) Waiting to run
**Description:** Use `Arc<dyn ImportResolver>` to reduce cost **Related issue:** - Closes https://github.com/swc-project/swc/issues/9548
This commit is contained in:
parent
cf74382ba0
commit
fc0ba2a084
5
.changeset/serious-ducks-suffer.md
Normal file
5
.changeset/serious-ducks-suffer.md
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
swc_ecma_transforms_module: minor
|
||||
---
|
||||
|
||||
fix(typescript): rewrite import specifier in type declaration
|
@ -1,4 +1,4 @@
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
use std::sync::Arc;
|
||||
|
||||
use compat::es2015::regenerator;
|
||||
use either::Either;
|
||||
@ -15,16 +15,16 @@ use swc_ecma_transforms::{
|
||||
compat,
|
||||
feature::{enable_available_feature_from_es_version, FeatureFlag},
|
||||
fixer::{fixer, paren_remover},
|
||||
helpers, hygiene,
|
||||
hygiene::hygiene_with_config,
|
||||
modules,
|
||||
helpers,
|
||||
hygiene::{self, hygiene_with_config},
|
||||
modules::{self, path::ImportResolver},
|
||||
optimization::const_modules,
|
||||
pass::Optional,
|
||||
resolver, Assumptions,
|
||||
};
|
||||
use swc_ecma_visit::{as_folder, noop_visit_mut_type, VisitMut, VisitMutWith};
|
||||
|
||||
use crate::config::{CompiledPaths, GlobalPassOption, JsMinifyOptions, ModuleConfig};
|
||||
use crate::config::{GlobalPassOption, JsMinifyOptions, ModuleConfig};
|
||||
|
||||
/// Builder is used to create a high performance `Compiler`.
|
||||
pub struct PassBuilder<'a, 'b, P: swc_ecma_visit::Fold> {
|
||||
@ -168,12 +168,10 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
|
||||
/// - fixer if enabled
|
||||
pub fn finalize<'cmt>(
|
||||
self,
|
||||
base_url: PathBuf,
|
||||
paths: CompiledPaths,
|
||||
base: &FileName,
|
||||
syntax: Syntax,
|
||||
module: Option<ModuleConfig>,
|
||||
comments: Option<&'cmt dyn Comments>,
|
||||
resolver: Option<(FileName, Arc<dyn ImportResolver>)>,
|
||||
) -> impl 'cmt + swc_ecma_visit::Fold
|
||||
where
|
||||
P: 'cmt,
|
||||
@ -347,12 +345,10 @@ impl<'a, 'b, P: swc_ecma_visit::Fold> PassBuilder<'a, 'b, P> {
|
||||
ModuleConfig::build(
|
||||
self.cm.clone(),
|
||||
comments,
|
||||
base_url,
|
||||
paths,
|
||||
base,
|
||||
self.unresolved_mark,
|
||||
module,
|
||||
feature_flag
|
||||
self.unresolved_mark,
|
||||
feature_flag,
|
||||
resolver,
|
||||
),
|
||||
as_folder(MinifierPass {
|
||||
options: self.minify,
|
||||
|
@ -45,8 +45,13 @@ use swc_ecma_parser::{parse_file_as_expr, Syntax, TsSyntax};
|
||||
pub use swc_ecma_transforms::proposals::DecoratorVersion;
|
||||
use swc_ecma_transforms::{
|
||||
feature::FeatureFlag,
|
||||
hygiene, modules,
|
||||
modules::{path::NodeImportResolver, rewriter::import_rewriter, EsModuleConfig},
|
||||
hygiene,
|
||||
modules::{
|
||||
self,
|
||||
path::{ImportResolver, NodeImportResolver, Resolver},
|
||||
rewriter::import_rewriter,
|
||||
EsModuleConfig,
|
||||
},
|
||||
optimization::{const_modules, json_parse, simplifier},
|
||||
pass::{noop, Optional},
|
||||
proposals::{
|
||||
@ -540,6 +545,9 @@ impl Options {
|
||||
.unwrap_or_default()
|
||||
};
|
||||
|
||||
let paths = paths.into_iter().collect();
|
||||
let resolver = ModuleConfig::get_resolver(&base_url, paths, base, cfg.module.as_ref());
|
||||
|
||||
let pass = PassBuilder::new(
|
||||
cm,
|
||||
handler,
|
||||
@ -564,12 +572,10 @@ impl Options {
|
||||
.preset_env(cfg.env)
|
||||
.regenerator(regenerator)
|
||||
.finalize(
|
||||
base_url,
|
||||
paths.into_iter().collect(),
|
||||
base,
|
||||
syntax,
|
||||
cfg.module,
|
||||
comments.map(|v| v as _),
|
||||
resolver.clone(),
|
||||
);
|
||||
|
||||
let keep_import_attributes = experimental.keep_import_attributes.into_bool();
|
||||
@ -782,6 +788,7 @@ impl Options {
|
||||
.emit_assert_for_import_attributes
|
||||
.into_bool(),
|
||||
emit_isolated_dts: experimental.emit_isolated_dts.into_bool(),
|
||||
resolver,
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -1090,6 +1097,7 @@ pub struct BuiltInput<P: swc_ecma_visit::Fold> {
|
||||
pub emit_assert_for_import_attributes: bool,
|
||||
|
||||
pub emit_isolated_dts: bool,
|
||||
pub resolver: Option<(FileName, Arc<dyn ImportResolver>)>,
|
||||
}
|
||||
|
||||
impl<P> BuiltInput<P>
|
||||
@ -1120,6 +1128,7 @@ where
|
||||
output: self.output,
|
||||
emit_assert_for_import_attributes: self.emit_assert_for_import_attributes,
|
||||
emit_isolated_dts: self.emit_isolated_dts,
|
||||
resolver: self.resolver,
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1288,15 +1297,63 @@ impl ModuleConfig {
|
||||
pub fn build<'cmt>(
|
||||
cm: Arc<SourceMap>,
|
||||
comments: Option<&'cmt dyn Comments>,
|
||||
base_url: PathBuf,
|
||||
config: Option<ModuleConfig>,
|
||||
unresolved_mark: Mark,
|
||||
available_features: FeatureFlag,
|
||||
resolver: Option<(FileName, Arc<dyn ImportResolver>)>,
|
||||
) -> Box<dyn swc_ecma_visit::Fold + 'cmt> {
|
||||
let resolver = if let Some((base, resolver)) = resolver {
|
||||
Resolver::Real { base, resolver }
|
||||
} else {
|
||||
Resolver::Default
|
||||
};
|
||||
|
||||
match config {
|
||||
None | Some(ModuleConfig::Es6(..)) | Some(ModuleConfig::NodeNext(..)) => match resolver
|
||||
{
|
||||
Resolver::Default => Box::new(noop()),
|
||||
Resolver::Real { base, resolver } => Box::new(import_rewriter(base, resolver)),
|
||||
},
|
||||
Some(ModuleConfig::CommonJs(config)) => Box::new(modules::common_js::common_js(
|
||||
resolver,
|
||||
unresolved_mark,
|
||||
config,
|
||||
available_features,
|
||||
)),
|
||||
Some(ModuleConfig::Umd(config)) => Box::new(modules::umd::umd(
|
||||
cm,
|
||||
resolver,
|
||||
unresolved_mark,
|
||||
config,
|
||||
available_features,
|
||||
)),
|
||||
Some(ModuleConfig::Amd(config)) => Box::new(modules::amd::amd(
|
||||
resolver,
|
||||
unresolved_mark,
|
||||
config,
|
||||
available_features,
|
||||
comments,
|
||||
)),
|
||||
Some(ModuleConfig::SystemJs(config)) => Box::new(modules::system_js::system_js(
|
||||
resolver,
|
||||
unresolved_mark,
|
||||
config,
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_resolver(
|
||||
base_url: &Path,
|
||||
paths: CompiledPaths,
|
||||
base: &FileName,
|
||||
unresolved_mark: Mark,
|
||||
config: Option<ModuleConfig>,
|
||||
available_features: FeatureFlag,
|
||||
) -> Box<dyn swc_ecma_visit::Fold + 'cmt> {
|
||||
config: Option<&ModuleConfig>,
|
||||
) -> Option<(FileName, Arc<dyn ImportResolver>)> {
|
||||
let skip_resolver = base_url.as_os_str().is_empty() && paths.is_empty();
|
||||
|
||||
if skip_resolver {
|
||||
return None;
|
||||
}
|
||||
|
||||
let base = match base {
|
||||
FileName::Real(v) if !skip_resolver => {
|
||||
FileName::Real(v.canonicalize().unwrap_or_else(|_| v.to_path_buf()))
|
||||
@ -1304,100 +1361,27 @@ impl ModuleConfig {
|
||||
_ => base.clone(),
|
||||
};
|
||||
|
||||
match config {
|
||||
None => {
|
||||
if skip_resolver {
|
||||
Box::new(noop())
|
||||
} else {
|
||||
let resolver = build_resolver(base_url, paths, false);
|
||||
|
||||
Box::new(import_rewriter(base, resolver))
|
||||
}
|
||||
}
|
||||
let base_url = base_url.to_path_buf();
|
||||
let resolver = match config {
|
||||
None => build_resolver(base_url, paths, false),
|
||||
Some(ModuleConfig::Es6(config)) | Some(ModuleConfig::NodeNext(config)) => {
|
||||
if skip_resolver {
|
||||
Box::new(noop())
|
||||
} else {
|
||||
let resolver = build_resolver(base_url, paths, config.resolve_fully);
|
||||
|
||||
Box::new(import_rewriter(base, resolver))
|
||||
}
|
||||
build_resolver(base_url, paths, config.resolve_fully)
|
||||
}
|
||||
Some(ModuleConfig::CommonJs(config)) => {
|
||||
if skip_resolver {
|
||||
Box::new(modules::common_js::common_js(
|
||||
unresolved_mark,
|
||||
config,
|
||||
available_features,
|
||||
))
|
||||
} else {
|
||||
let resolver = build_resolver(base_url, paths, config.resolve_fully);
|
||||
Box::new(modules::common_js::common_js_with_resolver(
|
||||
resolver,
|
||||
base,
|
||||
unresolved_mark,
|
||||
config,
|
||||
available_features,
|
||||
))
|
||||
}
|
||||
build_resolver(base_url, paths, config.resolve_fully)
|
||||
}
|
||||
Some(ModuleConfig::Umd(config)) => {
|
||||
if skip_resolver {
|
||||
Box::new(modules::umd::umd(
|
||||
cm,
|
||||
unresolved_mark,
|
||||
config,
|
||||
available_features,
|
||||
))
|
||||
} else {
|
||||
let resolver = build_resolver(base_url, paths, config.config.resolve_fully);
|
||||
|
||||
Box::new(modules::umd::umd_with_resolver(
|
||||
cm,
|
||||
resolver,
|
||||
base,
|
||||
unresolved_mark,
|
||||
config,
|
||||
available_features,
|
||||
))
|
||||
}
|
||||
build_resolver(base_url, paths, config.config.resolve_fully)
|
||||
}
|
||||
Some(ModuleConfig::Amd(config)) => {
|
||||
if skip_resolver {
|
||||
Box::new(modules::amd::amd(
|
||||
unresolved_mark,
|
||||
config,
|
||||
available_features,
|
||||
comments,
|
||||
))
|
||||
} else {
|
||||
let resolver = build_resolver(base_url, paths, config.config.resolve_fully);
|
||||
|
||||
Box::new(modules::amd::amd_with_resolver(
|
||||
resolver,
|
||||
base,
|
||||
unresolved_mark,
|
||||
config,
|
||||
available_features,
|
||||
comments,
|
||||
))
|
||||
}
|
||||
build_resolver(base_url, paths, config.config.resolve_fully)
|
||||
}
|
||||
Some(ModuleConfig::SystemJs(config)) => {
|
||||
if skip_resolver {
|
||||
Box::new(modules::system_js::system_js(unresolved_mark, config))
|
||||
} else {
|
||||
let resolver = build_resolver(base_url, paths, config.resolve_fully);
|
||||
build_resolver(base_url, paths, config.resolve_fully)
|
||||
}
|
||||
};
|
||||
|
||||
Box::new(modules::system_js::system_js_with_resolver(
|
||||
resolver,
|
||||
base,
|
||||
unresolved_mark,
|
||||
config,
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
||||
Some((base, resolver))
|
||||
}
|
||||
}
|
||||
|
||||
@ -1712,7 +1696,7 @@ fn build_resolver(
|
||||
mut base_url: PathBuf,
|
||||
paths: CompiledPaths,
|
||||
resolve_fully: bool,
|
||||
) -> Box<SwcImportResolver> {
|
||||
) -> SwcImportResolver {
|
||||
static CACHE: Lazy<DashMap<(PathBuf, CompiledPaths, bool), SwcImportResolver, ARandomState>> =
|
||||
Lazy::new(Default::default);
|
||||
|
||||
@ -1731,7 +1715,7 @@ fn build_resolver(
|
||||
}
|
||||
|
||||
if let Some(cached) = CACHE.get(&(base_url.clone(), paths.clone(), resolve_fully)) {
|
||||
return Box::new((*cached).clone());
|
||||
return cached.clone();
|
||||
}
|
||||
|
||||
let r = {
|
||||
@ -1758,5 +1742,5 @@ fn build_resolver(
|
||||
|
||||
CACHE.insert((base_url, paths, resolve_fully), r.clone());
|
||||
|
||||
Box::new(r)
|
||||
r
|
||||
}
|
||||
|
@ -147,7 +147,7 @@ use swc_ecma_transforms::{
|
||||
fixer,
|
||||
helpers::{self, Helpers},
|
||||
hygiene,
|
||||
modules::path::NodeImportResolver,
|
||||
modules::{path::NodeImportResolver, rewriter::import_rewriter},
|
||||
pass::noop,
|
||||
resolver,
|
||||
};
|
||||
@ -982,6 +982,10 @@ impl Compiler {
|
||||
let mut checker = FastDts::new(fm.name.clone());
|
||||
let mut module = program.clone().expect_module();
|
||||
|
||||
if let Some((base, resolver)) = config.resolver {
|
||||
module = module.fold_with(&mut import_rewriter(base, resolver));
|
||||
}
|
||||
|
||||
let issues = checker.transform(&mut module);
|
||||
|
||||
for issue in issues {
|
||||
|
@ -0,0 +1,14 @@
|
||||
{
|
||||
"jsc": {
|
||||
"parser": {
|
||||
"syntax": "typescript"
|
||||
},
|
||||
"experimental": {
|
||||
"emitIsolatedDts": true
|
||||
},
|
||||
"baseUrl": "./",
|
||||
"paths": {
|
||||
"@lib/*": ["src/*"]
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
export * from "@lib/example";
|
@ -0,0 +1,2 @@
|
||||
const a = 1;
|
||||
export { a };
|
1
crates/swc/tests/ts-isolated-declaration/rewrite-import-specifier/output/index.d.ts
vendored
Normal file
1
crates/swc/tests/ts-isolated-declaration/rewrite-import-specifier/output/index.d.ts
vendored
Normal file
@ -0,0 +1 @@
|
||||
export * from "./src/example";
|
@ -0,0 +1 @@
|
||||
export * from "./src/example";
|
2
crates/swc/tests/ts-isolated-declaration/rewrite-import-specifier/output/src/example.d.ts
vendored
Normal file
2
crates/swc/tests/ts-isolated-declaration/rewrite-import-specifier/output/src/example.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
declare const a: number;
|
||||
export { a };
|
@ -0,0 +1,2 @@
|
||||
var a = 1;
|
||||
export { a };
|
@ -3568,6 +3568,7 @@ test!(
|
||||
|_| chain!(
|
||||
decorators(Default::default()),
|
||||
common_js(
|
||||
Default::default(),
|
||||
Mark::fresh(Mark::root()),
|
||||
common_js::Config {
|
||||
strict: false,
|
||||
@ -3596,6 +3597,7 @@ test!(
|
||||
|_| chain!(
|
||||
decorators(Default::default()),
|
||||
common_js::common_js(
|
||||
Default::default(),
|
||||
Mark::fresh(Mark::root()),
|
||||
common_js::Config {
|
||||
strict: false,
|
||||
@ -3829,6 +3831,7 @@ test!(
|
||||
classes(Default::default()),
|
||||
function_name(),
|
||||
common_js(
|
||||
Default::default(),
|
||||
Mark::fresh(Mark::root()),
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
|
@ -223,7 +223,12 @@ test!(
|
||||
legacy: true,
|
||||
..Default::default()
|
||||
}),
|
||||
common_js(unresolved_mark, Default::default(), Default::default(),)
|
||||
common_js(
|
||||
Default::default(),
|
||||
unresolved_mark,
|
||||
Default::default(),
|
||||
Default::default()
|
||||
)
|
||||
)
|
||||
},
|
||||
function_name_modules_3,
|
||||
@ -275,7 +280,12 @@ test!(
|
||||
arrow(unresolved_mark),
|
||||
shorthand(),
|
||||
function_name(),
|
||||
common_js(unresolved_mark, Default::default(), Default::default(),)
|
||||
common_js(
|
||||
Default::default(),
|
||||
unresolved_mark,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
)
|
||||
)
|
||||
},
|
||||
function_name_export_default_arrow_renaming,
|
||||
@ -642,7 +652,12 @@ test!(
|
||||
}),
|
||||
classes(Default::default()),
|
||||
function_name(),
|
||||
common_js(unresolved_mark, Default::default(), Default::default(),)
|
||||
common_js(
|
||||
Default::default(),
|
||||
unresolved_mark,
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
)
|
||||
)
|
||||
},
|
||||
function_name_modules_2,
|
||||
|
@ -6,7 +6,7 @@ use swc_common::{
|
||||
comments::{CommentKind, Comments},
|
||||
source_map::PURE_SP,
|
||||
util::take::Take,
|
||||
FileName, Mark, Span, SyntaxContext, DUMMY_SP,
|
||||
Mark, Span, SyntaxContext, DUMMY_SP,
|
||||
};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_transforms_base::{feature::FeatureFlag, helper_expr};
|
||||
@ -19,7 +19,7 @@ pub use super::util::Config as InnerConfig;
|
||||
use crate::{
|
||||
module_decl_strip::{Export, Link, LinkFlag, LinkItem, LinkSpecifierReducer, ModuleDeclStrip},
|
||||
module_ref_rewriter::{rewrite_import_bindings, ImportMap},
|
||||
path::{ImportResolver, Resolver},
|
||||
path::Resolver,
|
||||
top_level_this::top_level_this,
|
||||
util::{
|
||||
define_es_module, emit_export_stmts, local_name_for_src, use_strict, ImportInterop,
|
||||
@ -39,6 +39,7 @@ pub struct Config {
|
||||
}
|
||||
|
||||
pub fn amd<C>(
|
||||
resolver: Resolver,
|
||||
unresolved_mark: Mark,
|
||||
config: Config,
|
||||
available_features: FeatureFlag,
|
||||
@ -53,45 +54,7 @@ where
|
||||
module_id,
|
||||
config,
|
||||
unresolved_mark,
|
||||
resolver: Resolver::Default,
|
||||
comments,
|
||||
|
||||
support_arrow: caniuse!(available_features.ArrowFunctions),
|
||||
const_var_kind: if caniuse!(available_features.BlockScoping) {
|
||||
VarDeclKind::Const
|
||||
} else {
|
||||
VarDeclKind::Var
|
||||
},
|
||||
|
||||
dep_list: Default::default(),
|
||||
require: quote_ident!(
|
||||
SyntaxContext::empty().apply_mark(unresolved_mark),
|
||||
"require"
|
||||
),
|
||||
exports: None,
|
||||
module: None,
|
||||
found_import_meta: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn amd_with_resolver<C>(
|
||||
resolver: Box<dyn ImportResolver>,
|
||||
base: FileName,
|
||||
unresolved_mark: Mark,
|
||||
config: Config,
|
||||
available_features: FeatureFlag,
|
||||
comments: Option<C>,
|
||||
) -> impl Fold + VisitMut
|
||||
where
|
||||
C: Comments,
|
||||
{
|
||||
let Config { module_id, config } = config;
|
||||
|
||||
as_folder(Amd {
|
||||
module_id,
|
||||
config,
|
||||
unresolved_mark,
|
||||
resolver: Resolver::Real { base, resolver },
|
||||
resolver,
|
||||
comments,
|
||||
|
||||
support_arrow: caniuse!(available_features.ArrowFunctions),
|
||||
|
@ -1,6 +1,6 @@
|
||||
use swc_common::{
|
||||
collections::AHashSet, source_map::PURE_SP, util::take::Take, FileName, Mark, Span,
|
||||
SyntaxContext, DUMMY_SP,
|
||||
collections::AHashSet, source_map::PURE_SP, util::take::Take, Mark, Span, SyntaxContext,
|
||||
DUMMY_SP,
|
||||
};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_transforms_base::{feature::FeatureFlag, helper_expr};
|
||||
@ -15,7 +15,7 @@ use crate::{
|
||||
Export, ExportKV, Link, LinkFlag, LinkItem, LinkSpecifierReducer, ModuleDeclStrip,
|
||||
},
|
||||
module_ref_rewriter::{rewrite_import_bindings, ImportMap},
|
||||
path::{ImportResolver, Resolver},
|
||||
path::Resolver,
|
||||
top_level_this::top_level_this,
|
||||
util::{
|
||||
define_es_module, emit_export_stmts, local_name_for_src, prop_name, use_strict,
|
||||
@ -24,34 +24,14 @@ use crate::{
|
||||
};
|
||||
|
||||
pub fn common_js(
|
||||
resolver: Resolver,
|
||||
unresolved_mark: Mark,
|
||||
config: Config,
|
||||
available_features: FeatureFlag,
|
||||
) -> impl Fold + VisitMut {
|
||||
as_folder(Cjs {
|
||||
config,
|
||||
resolver: Resolver::Default,
|
||||
unresolved_mark,
|
||||
available_features,
|
||||
support_arrow: caniuse!(available_features.ArrowFunctions),
|
||||
const_var_kind: if caniuse!(available_features.BlockScoping) {
|
||||
VarDeclKind::Const
|
||||
} else {
|
||||
VarDeclKind::Var
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
pub fn common_js_with_resolver(
|
||||
resolver: Box<dyn ImportResolver>,
|
||||
base: FileName,
|
||||
unresolved_mark: Mark,
|
||||
config: Config,
|
||||
available_features: FeatureFlag,
|
||||
) -> impl Fold + VisitMut {
|
||||
as_folder(Cjs {
|
||||
config,
|
||||
resolver: Resolver::Real { base, resolver },
|
||||
resolver,
|
||||
unresolved_mark,
|
||||
available_features,
|
||||
support_arrow: caniuse!(available_features.ArrowFunctions),
|
||||
|
@ -17,11 +17,13 @@ use swc_ecma_loader::resolve::{Resolution, Resolve};
|
||||
use swc_ecma_utils::{quote_ident, ExprFactory};
|
||||
use tracing::{debug, info, warn, Level};
|
||||
|
||||
pub(crate) enum Resolver {
|
||||
#[derive(Default)]
|
||||
pub enum Resolver {
|
||||
Real {
|
||||
base: FileName,
|
||||
resolver: Box<dyn ImportResolver>,
|
||||
resolver: Arc<dyn ImportResolver>,
|
||||
},
|
||||
#[default]
|
||||
Default,
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use anyhow::Context;
|
||||
use swc_common::FileName;
|
||||
use swc_ecma_ast::*;
|
||||
@ -6,25 +8,16 @@ use swc_ecma_visit::{as_folder, noop_visit_mut_type, Fold, VisitMut, VisitMutWit
|
||||
use crate::path::ImportResolver;
|
||||
|
||||
/// Import rewriter, which rewrites imports as es modules.
|
||||
pub fn import_rewriter<R>(base: FileName, resolver: R) -> impl Fold + VisitMut
|
||||
where
|
||||
R: ImportResolver,
|
||||
{
|
||||
pub fn import_rewriter(base: FileName, resolver: Arc<dyn ImportResolver>) -> impl Fold + VisitMut {
|
||||
as_folder(Rewriter { base, resolver })
|
||||
}
|
||||
|
||||
struct Rewriter<R>
|
||||
where
|
||||
R: ImportResolver,
|
||||
{
|
||||
struct Rewriter {
|
||||
base: FileName,
|
||||
resolver: R,
|
||||
resolver: Arc<dyn ImportResolver>,
|
||||
}
|
||||
|
||||
impl<R> VisitMut for Rewriter<R>
|
||||
where
|
||||
R: ImportResolver,
|
||||
{
|
||||
impl VisitMut for Rewriter {
|
||||
noop_visit_mut_type!(fail);
|
||||
|
||||
fn visit_mut_call_expr(&mut self, e: &mut CallExpr) {
|
||||
|
@ -1,7 +1,7 @@
|
||||
use anyhow::Context;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use swc_atoms::JsWord;
|
||||
use swc_common::{collections::AHashMap, FileName, Mark, Span, SyntaxContext, DUMMY_SP};
|
||||
use swc_common::{collections::AHashMap, Mark, Span, SyntaxContext, DUMMY_SP};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_utils::{
|
||||
member_expr, private_ident, quote_ident, quote_str, var::VarCollector, ExprFactory,
|
||||
@ -9,7 +9,7 @@ use swc_ecma_utils::{
|
||||
use swc_ecma_visit::{standard_only_fold, Fold, FoldWith, VisitWith};
|
||||
|
||||
use crate::{
|
||||
path::{ImportResolver, Resolver},
|
||||
path::Resolver,
|
||||
top_level_this::top_level_this,
|
||||
util::{local_name_for_src, use_strict},
|
||||
};
|
||||
@ -41,35 +41,10 @@ struct SystemJs {
|
||||
context_ident: Ident,
|
||||
}
|
||||
|
||||
pub fn system_js(unresolved_mark: Mark, config: Config) -> impl Fold {
|
||||
pub fn system_js(resolver: Resolver, unresolved_mark: Mark, config: Config) -> impl Fold {
|
||||
SystemJs {
|
||||
unresolved_mark,
|
||||
resolver: Resolver::Default,
|
||||
config,
|
||||
|
||||
declare_var_idents: Vec::new(),
|
||||
export_map: Default::default(),
|
||||
export_names: Vec::new(),
|
||||
export_values: Vec::new(),
|
||||
tla: false,
|
||||
enter_async_fn: 0,
|
||||
root_fn_decl_idents: Vec::new(),
|
||||
module_item_meta_list: Vec::new(),
|
||||
import_idents: Vec::new(),
|
||||
export_ident: private_ident!("_export"),
|
||||
context_ident: private_ident!("_context"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn system_js_with_resolver(
|
||||
resolver: Box<dyn ImportResolver>,
|
||||
base: FileName,
|
||||
unresolved_mark: Mark,
|
||||
config: Config,
|
||||
) -> impl Fold {
|
||||
SystemJs {
|
||||
unresolved_mark,
|
||||
resolver: Resolver::Real { base, resolver },
|
||||
resolver,
|
||||
config,
|
||||
declare_var_idents: Vec::new(),
|
||||
export_map: Default::default(),
|
||||
|
@ -1,8 +1,8 @@
|
||||
use anyhow::Context;
|
||||
use swc_atoms::JsWord;
|
||||
use swc_common::{
|
||||
source_map::PURE_SP, sync::Lrc, util::take::Take, FileName, Mark, SourceMap, Span,
|
||||
SyntaxContext, DUMMY_SP,
|
||||
source_map::PURE_SP, sync::Lrc, util::take::Take, Mark, SourceMap, Span, SyntaxContext,
|
||||
DUMMY_SP,
|
||||
};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_transforms_base::{feature::FeatureFlag, helper_expr};
|
||||
@ -16,7 +16,7 @@ pub use self::config::Config;
|
||||
use crate::{
|
||||
module_decl_strip::{Export, Link, LinkFlag, LinkItem, LinkSpecifierReducer, ModuleDeclStrip},
|
||||
module_ref_rewriter::{rewrite_import_bindings, ImportMap},
|
||||
path::{ImportResolver, Resolver},
|
||||
path::Resolver,
|
||||
top_level_this::top_level_this,
|
||||
util::{
|
||||
define_es_module, emit_export_stmts, local_name_for_src, use_strict, ImportInterop,
|
||||
@ -29,6 +29,7 @@ mod config;
|
||||
|
||||
pub fn umd(
|
||||
cm: Lrc<SourceMap>,
|
||||
resolver: Resolver,
|
||||
unresolved_mark: Mark,
|
||||
config: Config,
|
||||
available_features: FeatureFlag,
|
||||
@ -37,7 +38,7 @@ pub fn umd(
|
||||
config: config.build(cm.clone()),
|
||||
unresolved_mark,
|
||||
cm,
|
||||
resolver: Resolver::Default,
|
||||
resolver,
|
||||
|
||||
const_var_kind: if caniuse!(available_features.BlockScoping) {
|
||||
VarDeclKind::Const
|
||||
@ -51,31 +52,6 @@ pub fn umd(
|
||||
})
|
||||
}
|
||||
|
||||
pub fn umd_with_resolver(
|
||||
cm: Lrc<SourceMap>,
|
||||
resolver: Box<dyn ImportResolver>,
|
||||
base: FileName,
|
||||
unresolved_mark: Mark,
|
||||
config: Config,
|
||||
available_features: FeatureFlag,
|
||||
) -> impl Fold + VisitMut {
|
||||
as_folder(Umd {
|
||||
config: config.build(cm.clone()),
|
||||
unresolved_mark,
|
||||
cm,
|
||||
resolver: Resolver::Real { base, resolver },
|
||||
|
||||
const_var_kind: if caniuse!(available_features.BlockScoping) {
|
||||
VarDeclKind::Const
|
||||
} else {
|
||||
VarDeclKind::Var
|
||||
},
|
||||
|
||||
dep_list: Default::default(),
|
||||
exports: None,
|
||||
})
|
||||
}
|
||||
|
||||
pub struct Umd {
|
||||
cm: Lrc<SourceMap>,
|
||||
unresolved_mark: Mark,
|
||||
|
@ -26,7 +26,13 @@ fn tr(config: amd::Config, is_ts: bool, comments: Rc<SingleThreadedComments>) ->
|
||||
chain!(
|
||||
resolver(unresolved_mark, top_level_mark, is_ts),
|
||||
typescript::typescript(Default::default(), unresolved_mark, top_level_mark),
|
||||
amd(unresolved_mark, config, avalible_set, Some(comments)),
|
||||
amd(
|
||||
Default::default(),
|
||||
unresolved_mark,
|
||||
config,
|
||||
avalible_set,
|
||||
Some(comments)
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ fn tr(config: common_js::Config, is_ts: bool) -> impl Fold {
|
||||
chain!(
|
||||
resolver(unresolved_mark, top_level_mark, is_ts),
|
||||
typescript::typescript(Default::default(), unresolved_mark, top_level_mark),
|
||||
common_js(unresolved_mark, config, available_set),
|
||||
common_js(Default::default(), unresolved_mark, config, available_set),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::{
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
use serde::Deserialize;
|
||||
@ -73,7 +76,7 @@ fn issue_4730() {
|
||||
|
||||
import_rewriter(
|
||||
FileName::Real(input_dir.join("src").join("index.js")),
|
||||
resolver,
|
||||
Arc::new(resolver),
|
||||
)
|
||||
},
|
||||
&input_dir.join("src").join("index.js"),
|
||||
@ -140,7 +143,7 @@ fn fixture(input_dir: PathBuf) {
|
||||
|
||||
let resolver = paths_resolver(&base_dir, rules);
|
||||
|
||||
import_rewriter(FileName::Real(index_path.clone()), resolver)
|
||||
import_rewriter(FileName::Real(index_path.clone()), Arc::new(resolver))
|
||||
},
|
||||
&index_path,
|
||||
&output_dir.join("index.ts"),
|
||||
|
@ -18,7 +18,7 @@ fn tr(_tester: &mut Tester<'_>, config: Config) -> impl Fold {
|
||||
let top_level_mark = Mark::new();
|
||||
chain!(
|
||||
resolver(unresolved_mark, top_level_mark, false),
|
||||
system_js(unresolved_mark, config)
|
||||
system_js(Default::default(), unresolved_mark, config)
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -25,7 +25,13 @@ fn tr(tester: &mut Tester<'_>, config: Config, typescript: bool) -> impl Fold {
|
||||
chain!(
|
||||
resolver(unresolved_mark, top_level_mark, typescript),
|
||||
typescript::typescript(Default::default(), unresolved_mark, top_level_mark),
|
||||
umd(tester.cm.clone(), unresolved_mark, config, avalible_set,),
|
||||
umd(
|
||||
tester.cm.clone(),
|
||||
Default::default(),
|
||||
unresolved_mark,
|
||||
config,
|
||||
avalible_set,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -572,6 +572,7 @@ test!(
|
||||
import_analyzer(false.into(), false),
|
||||
inject_helpers(unresolved_mark),
|
||||
common_js(
|
||||
Default::default(),
|
||||
Mark::fresh(Mark::root()),
|
||||
Default::default(),
|
||||
Default::default(),
|
||||
|
Loading…
Reference in New Issue
Block a user