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

fix(turbopack): Fix edge cases of tree shaking #7986

Merged
merged 326 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
326 commits
Select commit Hold shift + click to select a range
13bfc4c
Update test refs
kdy1 Apr 23, 2024
1ac8dc3
`export` field
kdy1 Apr 23, 2024
d03d19b
Update test refs
kdy1 Apr 23, 2024
9b3969a
type
kdy1 Apr 23, 2024
29c8213
fix name
kdy1 Apr 23, 2024
f8547f2
Update test refs
kdy1 Apr 23, 2024
dc0a599
Reduce input
kdy1 Apr 23, 2024
4e56786
Print log
kdy1 Apr 23, 2024
bd3211e
dbg
kdy1 Apr 24, 2024
c1bf8d8
Two fields
kdy1 Apr 24, 2024
dbb39a5
Update test refs
kdy1 Apr 24, 2024
dd65a87
Update test refs
kdy1 Apr 24, 2024
cf2b552
import * as
kdy1 Apr 24, 2024
b789256
Update test refs
kdy1 Apr 24, 2024
1206c7c
move
kdy1 Apr 24, 2024
3054a09
Remove logging
kdy1 Apr 24, 2024
3272db0
review
kdy1 Apr 24, 2024
8e18062
Update test refs
kdy1 Apr 24, 2024
b277fb9
clippy
kdy1 Apr 24, 2024
2545d6a
facade => None
kdy1 Apr 24, 2024
56b8217
add test case
sokra Apr 25, 2024
00a110e
fix test case
sokra Apr 25, 2024
c725ab9
Remove wrong
kdy1 Apr 26, 2024
f170621
Fix
kdy1 Apr 26, 2024
c4045fc
Update test refs
kdy1 Apr 26, 2024
63ff47e
fix mermaid escape
kdy1 Apr 26, 2024
cfb0ad6
Update test refs
kdy1 Apr 26, 2024
3535853
impl EvaluatableAsset for EcmascriptModulePartAsset
kdy1 Apr 26, 2024
b253b6d
Improve assertion
kdy1 Apr 26, 2024
ce8aa8c
split_module: Handle no-dep
kdy1 Apr 26, 2024
19b50ec
magic_identifier::mangle
kdy1 Apr 26, 2024
cc59621
reexport vs export
kdy1 Apr 26, 2024
b203834
Add a debug assertion
kdy1 Apr 26, 2024
2b52de5
WIP: `default`
kdy1 Apr 26, 2024
cc25c80
Update test refs
kdy1 Apr 26, 2024
548d139
default exports
kdy1 Apr 26, 2024
220b131
Remove dbg
kdy1 Apr 26, 2024
270070c
Add assertion
kdy1 Apr 26, 2024
0f68e50
`get_var`
kdy1 Apr 26, 2024
6c6e42a
`globalThis`
kdy1 Apr 26, 2024
c40b2e9
fixup
kdy1 Apr 26, 2024
06682de
`unresolved_ctxt`
kdy1 Apr 27, 2024
c8ffa15
Fix clippy
kdy1 Apr 27, 2024
ed789b9
Remove dbg
kdy1 Apr 27, 2024
be20e46
Update test refs
kdy1 Apr 29, 2024
ed54597
[TMP] Default
kdy1 May 7, 2024
94ad2c2
bail with more details
kdy1 May 7, 2024
eedf0e4
Fail-safe references()
kdy1 May 7, 2024
3efd30f
More context on failure
kdy1 May 7, 2024
7afd594
Common JS modules have only one part.
kdy1 May 7, 2024
9894ae4
Add a test
kdy1 May 7, 2024
2f66311
Make `add_strong_deps` not lazy about from
kdy1 May 7, 2024
2ea41f1
Rename for consistency
kdy1 May 7, 2024
9aaf709
grammar
kdy1 May 7, 2024
825bce1
Add a debug assertion
kdy1 May 7, 2024
e776796
Add a debug assertion about module count
kdy1 May 7, 2024
fba455d
Convert debug assertions to non-debug assertions
kdy1 May 7, 2024
f7925e7
Add a test for the analyzer
kdy1 May 7, 2024
19c11f0
Rename for consistency
kdy1 May 7, 2024
17cfb08
Pass top_level_mark down
kdy1 May 7, 2024
3c5b18d
Use only
kdy1 May 7, 2024
bbfa83e
Document
kdy1 May 7, 2024
398f19d
fix
kdy1 May 8, 2024
08b17b8
print entrypoints while testing
kdy1 May 8, 2024
439de04
Update test refs
kdy1 May 8, 2024
ee8b84d
More context on error
kdy1 May 8, 2024
cea5733
Some
kdy1 May 8, 2024
d075fc8
Add more detail for bail
kdy1 May 8, 2024
a35b7c2
Add a test
kdy1 May 8, 2024
784da2e
Remove some debug_ignore
kdy1 May 8, 2024
006ab55
find_turbopack_part_id_in_asserts && PartId
kdy1 May 8, 2024
d5e2a3d
Use create_turbopack_part_id_assert
kdy1 May 8, 2024
15a6eb0
Rename & add dbg
kdy1 May 8, 2024
5200cf4
More information on exception
kdy1 May 8, 2024
f4641a4
dbg
kdy1 May 8, 2024
9e17374
debug_ignore
kdy1 May 8, 2024
00e982e
Use asset ident for tree shaking
kdy1 May 9, 2024
e874269
Lint
kdy1 May 9, 2024
79585ac
WIP
kdy1 May 9, 2024
8c71852
clipy
kdy1 May 9, 2024
5fe110b
revert module part
kdy1 May 9, 2024
785cdf6
pub(crate)
kdy1 May 9, 2024
0e4e65a
Use downcast in EsmAssetReference::resolve_reference
kdy1 May 9, 2024
eddcfd3
Remove dbg
kdy1 May 9, 2024
357e83e
Enable CJS test
kdy1 May 9, 2024
7083ada
Make `only` array
kdy1 May 9, 2024
c65df04
Dummy EcmascriptModulePartAsset::content
kdy1 May 9, 2024
44054ad
Add a test
kdy1 May 9, 2024
18f58dd
Update test refs
kdy1 May 9, 2024
51db37d
Revert "[TMP] Default"
kdy1 May 9, 2024
ca6adaa
Remove wrong test output
kdy1 May 9, 2024
3b9b5c7
fn decl && class decl
kdy1 May 9, 2024
bab0012
Update test refs
kdy1 May 9, 2024
f9d2f63
Revert "Revert "[TMP] Default""
kdy1 May 9, 2024
774ba64
Add a test
kdy1 May 9, 2024
c01b39c
Update test refs
kdy1 May 9, 2024
d2e7852
SplitResult::Unparseable.messages
kdy1 May 9, 2024
a144e92
Handle scripts
kdy1 May 9, 2024
3f7ff9f
SplitResult::Failed
kdy1 May 10, 2024
853a3c0
Remove dbg
kdy1 May 10, 2024
d166cb5
Script as a module
kdy1 May 10, 2024
65830b8
Check for common js
kdy1 May 10, 2024
c11dfb2
always analyze references
kdy1 May 10, 2024
36c338d
Revert useless changes
kdy1 May 10, 2024
3b83556
Replace import bindings in LHS
kdy1 May 10, 2024
7002c04
visit ident in simple assign target
kdy1 May 10, 2024
c776f51
ast_path.len() - 2
kdy1 May 10, 2024
5cc7b0a
WIP!!!
kdy1 May 10, 2024
3568177
We need to create a separate binding
kdy1 May 10, 2024
8a8b884
replace with a new variable
kdy1 May 10, 2024
736238f
Use `var` instead of `const`
kdy1 May 10, 2024
2e2acae
Remove dbg
kdy1 May 10, 2024
5269118
Use different
kdy1 May 10, 2024
b29465c
Add dbg
kdy1 May 10, 2024
dd57e8f
Add dbg
kdy1 May 10, 2024
a821dd0
dbg
kdy1 May 13, 2024
a6db1c5
SimpleAssignTarget
kdy1 May 13, 2024
19c371a
dbg
kdy1 May 13, 2024
6d10312
Add a test
kdy1 May 13, 2024
0982ccf
Revert
kdy1 May 13, 2024
b35f636
WIP: Mutable export
kdy1 May 13, 2024
de4e7eb
fixup for mutable exports
kdy1 May 13, 2024
5b8fc50
fixup: Fix compilation
kdy1 May 13, 2024
17b13c9
Patch runtime
kdy1 May 13, 2024
a882e49
Update test refs
kdy1 May 13, 2024
3579b5c
Fix input
kdy1 May 13, 2024
fc317e6
Update test refs
kdy1 May 13, 2024
f623d33
Reexport if read-write
kdy1 May 13, 2024
d894ed1
kdy1 May 13, 2024
f643220
Add a unit test
kdy1 May 13, 2024
c2e5f5d
Update test refs
kdy1 May 13, 2024
8d32329
Full test
kdy1 May 13, 2024
e795bd2
UPdate test refs
kdy1 May 13, 2024
bb2f9dd
Detect `module.hot`
kdy1 May 13, 2024
10df47f
Improve `bai!l`
kdy1 May 13, 2024
4401252
Add a test
kdy1 May 13, 2024
8b72413
syntaxcontext empty
kdy1 May 13, 2024
dd68104
Improve cjs detection
kdy1 May 14, 2024
da7add8
fixup
kdy1 May 14, 2024
b01326a
Remove dbg
kdy1 May 14, 2024
2cb7d9e
Fix wrong test refs
kdy1 May 14, 2024
c0e2cd3
Remove wrong logic
kdy1 May 14, 2024
95fe0c8
fixup
kdy1 May 14, 2024
7fe0905
Update test refs
kdy1 May 14, 2024
1d314cd
only test
kdy1 May 14, 2024
4aba6f7
Rename var
kdy1 May 14, 2024
10834bf
Hack
kdy1 May 14, 2024
7afca77
Update test refs
kdy1 May 14, 2024
1131ac9
Revert hack
kdy1 May 14, 2024
dcd3101
Exclude declared
kdy1 May 14, 2024
6229110
Update test refs
kdy1 May 14, 2024
1da1e69
read-write
kdy1 May 14, 2024
fb73df8
Update test refs
kdy1 May 14, 2024
e134e77
dbg
kdy1 May 14, 2024
541e980
log error
kdy1 May 14, 2024
2f4a65d
ChunkItem::is_self_async
kdy1 May 14, 2024
f2d4bdd
LOG
kdy1 May 16, 2024
c7ba8e8
[TMP] More log
kdy1 May 16, 2024
5023e47
add duplicate modules test case
sokra May 16, 2024
fabb65e
Revert logging
kdy1 May 16, 2024
d81c186
Fix `is_async_module`
kdy1 May 16, 2024
2aa1061
fixup: `is_async_module`
kdy1 May 16, 2024
284f8d4
ESM input
kdy1 May 17, 2024
3144c33
todo
kdy1 May 17, 2024
5efe989
Remove return (0)
kdy1 May 17, 2024
34f2771
Fix `contains_cjs`
kdy1 May 17, 2024
9a98e8c
Remove wrong
kdy1 May 17, 2024
47e0cf7
Remove needless
kdy1 May 17, 2024
4c29b39
data.var_decls.contains(var)
kdy1 May 17, 2024
b219a35
ESM
kdy1 May 17, 2024
4be803d
Fix reexports
kdy1 May 17, 2024
d91b2df
Use ModulePart::facade
kdy1 May 17, 2024
31825ab
Update test refs
kdy1 May 17, 2024
f528ab3
Remove dbg
kdy1 May 17, 2024
152da51
Remove wrong logic
kdy1 May 17, 2024
0cf94ee
bail for invalid import
kdy1 May 17, 2024
ebc9bd1
ImportedSymbol::Exports
kdy1 May 17, 2024
dc2cba6
Use ImportedSymbol::Exports for more
kdy1 May 17, 2024
1a88fcd
`visit_export_all`: parse_with
kdy1 May 17, 2024
e8a88d5
Make named re-exports
kdy1 May 17, 2024
48202bf
Make facade specific
kdy1 May 17, 2024
6594b50
Add a test
kdy1 May 17, 2024
d0a8c2f
export is read
kdy1 May 17, 2024
5bc0b47
Update test refs
kdy1 May 17, 2024
929d6bd
export default expr
kdy1 May 17, 2024
cf64144
side effect
kdy1 May 17, 2024
c0f9ef1
ㄷ테ㅐㄱㅅ
kdy1 May 17, 2024
3b96ce7
used_ids
kdy1 May 17, 2024
faf6eb0
Revert useless changes
kdy1 May 17, 2024
78a2048
lint
kdy1 May 17, 2024
e0160f9
fix lint
kdy1 May 17, 2024
f440f92
fix lint
kdy1 May 17, 2024
8ccaaf8
Update test refs
kdy1 May 17, 2024
d183b0a
split
kdy1 May 17, 2024
56c9b68
Remove part from asset ident if spliting is disabled
kdy1 May 17, 2024
e0b5c97
seal namespace object
sokra May 17, 2024
4e25fa7
add test case
sokra May 17, 2024
cbfe122
reorganize test cases
sokra May 17, 2024
374ef8a
add test case
sokra May 17, 2024
99de133
correct test case for side-effects-optimization
sokra May 17, 2024
4ab06d0
star reexports
kdy1 May 20, 2024
421ac46
Use module eval
kdy1 May 20, 2024
d32b168
Add a test
kdy1 May 20, 2024
6fe568c
fix import map
kdy1 May 21, 2024
aa7fd95
Update test refs
kdy1 May 21, 2024
5633c8a
ImportedSymbol::Exports
kdy1 May 21, 2024
8d31d00
fix resolving issue
kdy1 May 21, 2024
4be9111
ImportedSymbol::Namespace
kdy1 May 21, 2024
f56c4de
`ImportedSymbol::Exports`
kdy1 May 22, 2024
3531108
This test is wrong
kdy1 May 22, 2024
9987ab5
Remove wrong test
kdy1 May 22, 2024
30ccd10
Workaround wrong async module thing
kdy1 May 22, 2024
0117f54
Why
kdy1 May 22, 2024
cdef55c
Why not asynxc
kdy1 May 22, 2024
b429b85
I think this is crazy
kdy1 May 22, 2024
b19514f
turbo task system is...
kdy1 May 22, 2024
1cafc07
I hate this task
kdy1 May 22, 2024
a22c139
`ImportMap.has_top_level_await`
kdy1 May 22, 2024
857cd0b
dbg
kdy1 May 22, 2024
c06e792
dbg
kdy1 May 22, 2024
c983c76
dbg
kdy1 May 22, 2024
855d65a
Do not ignroe
kdy1 May 22, 2024
69760a1
TreeShakingMode::ReexportsOnly
kdy1 May 22, 2024
cd1931d
[TMP] Update tests
kdy1 May 22, 2024
3aa2b19
Remove wrong test results
kdy1 May 23, 2024
74c3441
Update test snapshots
kdy1 May 23, 2024
3a36c31
Remove issues
kdy1 May 24, 2024
601cf4e
side effect free
kdy1 May 24, 2024
212f491
ModulePart::Exports
kdy1 May 24, 2024
7230d06
Named export
kdy1 May 24, 2024
bb0cbf5
WIP: ModuleResolveResult::ignored
kdy1 May 24, 2024
71d5816
side_effect_free_packages
kdy1 May 24, 2024
e26cdce
Remove vdbg
kdy1 May 24, 2024
9935832
ImportedSymbol::Namespace
kdy1 May 24, 2024
752b483
chore: Remove type annotation
kdy1 May 27, 2024
eed457a
Comment
kdy1 May 27, 2024
8c81e17
Skip facade
kdy1 May 27, 2024
be43163
We can't skip, so revert
kdy1 May 27, 2024
9a7cf44
Use `ImportedSymbol::Exports` for `export * from './foo'`
kdy1 May 27, 2024
be8199f
revert
kdy1 May 27, 2024
156c634
Doc
kdy1 May 27, 2024
49cd5d0
Do not reexport from module eval
kdy1 May 27, 2024
102972a
I was correct
kdy1 May 27, 2024
ad7c162
revert
kdy1 May 27, 2024
888da50
fixup
kdy1 May 27, 2024
9ddbcda
Skip only module evaluation
kdy1 May 27, 2024
036f284
restore test
kdy1 May 27, 2024
0a8f3b8
Update test refs
kdy1 May 28, 2024
93c1d4a
Update test refs
kdy1 May 28, 2024
9f18a6f
Ignore for now
kdy1 May 28, 2024
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
1 change: 1 addition & 0 deletions crates/turbo-tasks-memory/src/aggregation/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ fn check_invariants<'a>(
false
}
});
#[allow(clippy::never_loop)]
for missing_upper in missing_uppers {
let upper_value = {
let upper = ctx.node(missing_upper);
Expand Down
4 changes: 3 additions & 1 deletion crates/turbopack-core/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::{bail, Result};
use turbo_tasks::{Value, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_fs::{glob::Glob, FileSystemPath};

use crate::{
compile_time_info::CompileTimeInfo,
Expand Down Expand Up @@ -76,4 +76,6 @@ pub trait AssetContext {

/// Gets a new AssetContext with the transition applied.
fn with_transition(self: Vc<Self>, transition: String) -> Vc<Box<dyn AssetContext>>;

fn side_effect_free_packages(self: Vc<Self>) -> Vc<Glob>;
}
11 changes: 11 additions & 0 deletions crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,17 @@ impl ModuleResolveResult {
}
}

pub fn ignored() -> ModuleResolveResult {
Self::ignored_with_key(RequestKey::default())
}

pub fn ignored_with_key(request_key: RequestKey) -> ModuleResolveResult {
ModuleResolveResult {
primary: indexmap! { request_key => ModuleResolveResultItem::Ignore },
affecting_sources: Vec::new(),
}
}

pub fn module(module: Vc<Box<dyn Module>>) -> ModuleResolveResult {
Self::module_with_key(RequestKey::default(), module)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,25 @@ function defineProp(
/**
* Adds the getters to the exports object.
*/
function esm(exports: Exports, getters: Record<string, () => any>) {
function esm(
exports: Exports,
getters: Record<string, (() => any) | [() => any, (v: any) => void]>
) {
defineProp(exports, "__esModule", { value: true });
if (toStringTag) defineProp(exports, toStringTag, { value: "Module" });
for (const key in getters) {
defineProp(exports, key, { get: getters[key], enumerable: true });
const item = getters[key];
if (Array.isArray(item)) {
defineProp(exports, key, {
get: item[0],
set: item[1],
enumerable: true,
});
} else {
defineProp(exports, key, { get: item, enumerable: true });
}
}
Object.seal(exports);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion crates/turbopack-ecmascript/benches/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ pub fn benchmark(c: &mut Criterion) {
let top_level_mark = Mark::new();
program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));

let eval_context = EvalContext::new(&program, unresolved_mark, None);
let eval_context =
EvalContext::new(&program, unresolved_mark, top_level_mark, false, None);
let var_graph = create_graph(&program, &eval_context);

let input = BenchInput {
Expand Down
19 changes: 16 additions & 3 deletions crates/turbopack-ecmascript/src/analyzer/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,18 +261,22 @@ pub fn create_graph(m: &Program, eval_context: &EvalContext) -> VarGraph {

pub struct EvalContext {
pub(crate) unresolved_mark: Mark,
pub(crate) top_level_mark: Mark,
pub(crate) imports: ImportMap,
}

impl EvalContext {
pub fn new(
module: &Program,
unresolved_mark: Mark,
top_level_mark: Mark,
skip_namespace: bool,
source: Option<Vc<Box<dyn Source>>>,
) -> Self {
Self {
unresolved_mark,
imports: ImportMap::analyze(module, source),
top_level_mark,
imports: ImportMap::analyze(module, skip_namespace, source),
}
}

Expand Down Expand Up @@ -1460,6 +1464,8 @@ impl VisitAstPath for Analyzer<'_> {
) {
let value = self.current_value.take();
if let SimpleAssignTarget::Ident(i) = n {
n.visit_children_with_path(self, ast_path);

self.add_value(
i.to_id(),
value.unwrap_or_else(|| {
Expand Down Expand Up @@ -1572,13 +1578,20 @@ impl VisitAstPath for Analyzer<'_> {
ident: &'ast Ident,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) {
if !matches!(
if !(matches!(
ast_path.last(),
Some(AstParentNodeRef::Expr(_, ExprField::Ident))
| Some(AstParentNodeRef::Prop(_, PropField::Shorthand))
) {
) || matches!(
ast_path.get(ast_path.len() - 2),
Some(AstParentNodeRef::SimpleAssignTarget(
_,
SimpleAssignTargetField::Ident,
))
)) {
return;
}

if let Some((esm_reference_index, export)) =
self.eval_context.imports.get_binding(&ident.to_id())
{
Expand Down
89 changes: 75 additions & 14 deletions crates/turbopack-ecmascript/src/analyzer/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ use swc_core::{
use turbo_tasks::Vc;
use turbopack_core::{issue::IssueSource, source::Source};

use super::{JsValue, ModuleValue};
use super::{top_level_await::has_top_level_await, JsValue, ModuleValue};
use crate::tree_shake::{find_turbopack_part_id_in_asserts, PartId};

#[turbo_tasks::value(serialization = "auto_for_input")]
#[derive(Default, Debug, Clone, Hash, PartialOrd, Ord)]
Expand Down Expand Up @@ -129,16 +130,22 @@ pub(crate) struct ImportMap {

/// True, when the module has exports
has_exports: bool,

/// True if the module is an ESM module due to top-level await.
has_top_level_await: bool,
}

#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) enum ImportedSymbol {
ModuleEvaluation,
Symbol(JsWord),
/// User requested the whole module
Namespace,
Exports,
Part(u32),
}

#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub(crate) struct ImportMapReference {
pub module_path: JsWord,
pub imported_symbol: ImportedSymbol,
Expand All @@ -148,7 +155,10 @@ pub(crate) struct ImportMapReference {

impl ImportMap {
pub fn is_esm(&self) -> bool {
self.has_exports || !self.imports.is_empty() || !self.namespace_imports.is_empty()
self.has_exports
|| self.has_top_level_await
|| !self.imports.is_empty()
|| !self.namespace_imports.is_empty()
}

pub fn get_import(&self, id: &Id) -> Option<JsValue> {
Expand Down Expand Up @@ -192,11 +202,16 @@ impl ImportMap {
}

/// Analyze ES import
pub(super) fn analyze(m: &Program, source: Option<Vc<Box<dyn Source>>>) -> Self {
pub(super) fn analyze(
m: &Program,
skip_namespace: bool,
source: Option<Vc<Box<dyn Source>>>,
) -> Self {
let mut data = ImportMap::default();

m.visit_with(&mut Analyzer {
data: &mut data,
skip_namespace,
source,
});

Expand All @@ -206,6 +221,7 @@ impl ImportMap {

struct Analyzer<'a> {
data: &'a mut ImportMap,
skip_namespace: bool,
source: Option<Vc<Box<dyn Source>>>,
}

Expand All @@ -216,7 +232,11 @@ impl<'a> Analyzer<'a> {
module_path: JsWord,
imported_symbol: ImportedSymbol,
annotations: ImportAnnotations,
) -> usize {
) -> Option<usize> {
if self.skip_namespace && matches!(imported_symbol, ImportedSymbol::Namespace) {
return None;
}

let issue_source = self
.source
.map(|s| IssueSource::from_swc_offsets(s, span.lo.to_usize(), span.hi.to_usize()));
Expand All @@ -228,11 +248,11 @@ impl<'a> Analyzer<'a> {
annotations,
};
if let Some(i) = self.data.references.get_index_of(&r) {
i
Some(i)
} else {
let i = self.data.references.len();
self.data.references.insert(r);
i
Some(i)
}
}
}
Expand All @@ -256,13 +276,17 @@ impl Visit for Analyzer<'_> {
);

for s in &import.specifiers {
let symbol = get_import_symbol_from_import(s);
let symbol = get_import_symbol_from_import(s, import.with.as_deref());
let i = self.ensure_reference(
import.span,
import.src.value.clone(),
symbol,
annotations.clone(),
);
let i = match i {
Some(v) => v,
None => continue,
};

let (local, orig_sym) = match s {
ImportSpecifier::Named(ImportNamedSpecifier {
Expand Down Expand Up @@ -293,13 +317,17 @@ impl Visit for Analyzer<'_> {
ImportedSymbol::ModuleEvaluation,
annotations.clone(),
);
let symbol = parse_with(export.with.as_deref());

let i = self.ensure_reference(
export.span,
export.src.value.clone(),
ImportedSymbol::Namespace,
symbol.unwrap_or(ImportedSymbol::Namespace),
annotations,
);
self.data.reexports.push((i, Reexport::Star));
if let Some(i) = i {
self.data.reexports.push((i, Reexport::Star));
}
}

fn visit_named_export(&mut self, export: &NamedExport) {
Expand All @@ -319,10 +347,14 @@ impl Visit for Analyzer<'_> {
);

for spec in export.specifiers.iter() {
let symbol = get_import_symbol_from_export(spec);
let symbol = get_import_symbol_from_export(spec, export.with.as_deref());

let i =
self.ensure_reference(export.span, src.value.clone(), symbol, annotations.clone());
let i = match i {
Some(v) => v,
None => continue,
};

match spec {
ExportSpecifier::Namespace(n) => {
Expand Down Expand Up @@ -367,6 +399,12 @@ impl Visit for Analyzer<'_> {
fn visit_stmt(&mut self, _: &Stmt) {
// don't visit children
}

fn visit_program(&mut self, m: &Program) {
self.data.has_top_level_await = has_top_level_await(m).is_some();

m.visit_children_with(self);
}
}

pub(crate) fn orig_name(n: &ModuleExportName) -> JsWord {
Expand All @@ -376,7 +414,23 @@ pub(crate) fn orig_name(n: &ModuleExportName) -> JsWord {
}
}

fn get_import_symbol_from_import(specifier: &ImportSpecifier) -> ImportedSymbol {
fn parse_with(with: Option<&ObjectLit>) -> Option<ImportedSymbol> {
find_turbopack_part_id_in_asserts(with?).map(|v| match v {
PartId::Internal(index) => ImportedSymbol::Part(index),
PartId::ModuleEvaluation => ImportedSymbol::ModuleEvaluation,
PartId::Export(e) => ImportedSymbol::Symbol(e.into()),
PartId::Exports => ImportedSymbol::Exports,
})
}

fn get_import_symbol_from_import(
specifier: &ImportSpecifier,
with: Option<&ObjectLit>,
) -> ImportedSymbol {
if let Some(part) = parse_with(with) {
return part;
}

match specifier {
ImportSpecifier::Named(ImportNamedSpecifier {
local, imported, ..
Expand All @@ -389,7 +443,14 @@ fn get_import_symbol_from_import(specifier: &ImportSpecifier) -> ImportedSymbol
}
}

fn get_import_symbol_from_export(specifier: &ExportSpecifier) -> ImportedSymbol {
fn get_import_symbol_from_export(
specifier: &ExportSpecifier,
with: Option<&ObjectLit>,
) -> ImportedSymbol {
if let Some(part) = parse_with(with) {
return part;
}

match specifier {
ExportSpecifier::Named(ExportNamedSpecifier { orig, .. }) => {
ImportedSymbol::Symbol(orig_name(orig))
Expand Down
3 changes: 2 additions & 1 deletion crates/turbopack-ecmascript/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3574,7 +3574,8 @@ mod tests {
let top_level_mark = Mark::new();
m.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));

let eval_context = EvalContext::new(&m, unresolved_mark, None);
let eval_context =
EvalContext::new(&m, unresolved_mark, top_level_mark, false, None);

let mut var_graph = create_graph(&m, &eval_context);

Expand Down
3 changes: 1 addition & 2 deletions crates/turbopack-ecmascript/src/chunk/placeable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ pub async fn is_marked_as_side_effect_free(
return Ok(Vc::cell(true));
}

let find_package_json: turbo_tasks::ReadRef<FindContextFileResult> =
find_context_file(path.parent(), package_json()).await?;
let find_package_json = find_context_file(path.parent(), package_json()).await?;

if let FindContextFileResult::Found(package_json, _) = *find_package_json {
match *side_effects_from_package_json(package_json).await? {
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-ecmascript/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ struct MemoizedSuccessfulAnalysis {
source_map: Option<ReadRef<SourceMap>>,
}

#[derive(Clone)]
pub struct EcmascriptModuleAssetBuilder {
source: Vc<Box<dyn Source>>,
asset_context: Vc<Box<dyn AssetContext>>,
Expand Down
Loading
Loading