diff --git a/packages/router-utils/src/compiler/analysis.ts b/packages/router-utils/src/compiler/analysis.ts index f5fd96a276..46669b2a88 100644 --- a/packages/router-utils/src/compiler/analysis.ts +++ b/packages/router-utils/src/compiler/analysis.ts @@ -266,6 +266,8 @@ export class ModuleAnalysis { readonly dynamicEdges: ReadonlyArray readonly markers: Marker readonly ownEffects: boolean + /** Whether the module's own statements have effects, module loading aside (bare imports and re-exports are followed by consumers, R36). */ + readonly codeEffects: boolean readonly sourceType: 'module' | 'script' | 'commonjs' readonly hasImportOrExport: boolean readonly purity: PurityContext @@ -292,6 +294,7 @@ export class ModuleAnalysis { const reexports: Array = [] let marker: Marker = null let ownEffects = false + let codeEffects = false const body = file.program.body for (let i = this.header.directiveCount; i < body.length; i++) { const stmt = body[i]! @@ -482,8 +485,14 @@ export class ModuleAnalysis { default: break } - if (!ownEffects && statementHasEffects(this.purity, stmt)) { + // module loading counts as an own effect, not as an effect of its own code + const loads = loadsModule(stmt) + if ( + (loads ? !ownEffects : !codeEffects) && + statementHasEffects(this.purity, stmt) + ) { ownEffects = true + codeEffects ||= !loads } } const dynamicEdges: Array = [] @@ -514,6 +523,7 @@ export class ModuleAnalysis { this.dynamicEdges = dynamicEdges this.markers = marker this.ownEffects = ownEffects + this.codeEffects = codeEffects this.sourceType = file.sourceType === 'commonjs' ? 'commonjs' : file.program.sourceType this.hasImportOrExport = imports.length > 0 || exportEntries.length > 0 @@ -858,6 +868,15 @@ export class ModuleAnalysis { } } +/** An import or re-export statement (it evaluates another module). */ +function loadsModule(stmt: Node): boolean { + return ( + stmt.type === 'ImportDeclaration' || + stmt.type === 'ExportAllDeclaration' || + (stmt.type === 'ExportNamedDeclaration' && stmt.source !== null) + ) +} + export interface ImportUses { live: boolean /** Statically read member names (namespace bindings). */ diff --git a/packages/router-utils/src/compiler/facts.ts b/packages/router-utils/src/compiler/facts.ts index 14e5a4222e..3d4e2b30ae 100644 --- a/packages/router-utils/src/compiler/facts.ts +++ b/packages/router-utils/src/compiler/facts.ts @@ -26,6 +26,8 @@ export interface TargetFacts { esm: boolean /** Own top-level effects or a bare import (transitive classification is the consumer's, R36). */ effectful: boolean + /** Its own statements have effects, module loading (`imports`) aside. */ + codeEffects: boolean effectProvenance: 'own' | 'import' | 'package' | null digest: string } @@ -64,6 +66,7 @@ const MISSING_TARGET: Omit = { imports: [], esm: false, effectful: true, + codeEffects: true, effectProvenance: null, } @@ -207,6 +210,7 @@ export class RawFactsCache { imports: [], esm: false, effectful: true, + codeEffects: true, effectProvenance: 'own' as const, } return { @@ -262,6 +266,7 @@ export class RawFactsCache { imports, esm: analysis.sourceType === 'module', effectful: own || analysis.imports.some((i) => i.bare && !i.typeOnly), + codeEffects: analysis.codeEffects, effectProvenance: own ? 'own' : analysis.imports.some((i) => i.bare && !i.typeOnly) diff --git a/packages/router-utils/tests/edges.test.ts b/packages/router-utils/tests/edges.test.ts index e398bf0901..eecae88fb7 100644 --- a/packages/router-utils/tests/edges.test.ts +++ b/packages/router-utils/tests/edges.test.ts @@ -165,6 +165,8 @@ describe('target facts (R36)', () => { export default own `), '/app/c.cjs': `module.exports = { a: 1 }\n`, + '/app/barrel.ts': `export * from './a'\nexport { b } from './b'\nimport './c'\nexport const d = 1\n`, + '/app/fx.ts': `export * from './a'\nglobalThis.x = 1\n`, }, }) const t = await host.rawFacts.get('/app/t.ts') @@ -176,6 +178,14 @@ describe('target facts (R36)', () => { esm: true, effectful: true, }) + // module loading is an own effect, not an effect of the module's own code + const barrel = await host.rawFacts.get('/app/barrel.ts') + expect(barrel.targetFacts).toMatchObject({ + effectful: true, + codeEffects: false, + }) + const fx = await host.rawFacts.get('/app/fx.ts') + expect(fx.targetFacts.codeEffects).toBe(true) const c = await host.rawFacts.get('/app/c.cjs') expect(c.targetFacts.esm).toBe(false) const missing = await host.rawFacts.get('/app/none.ts')