@@ -24,6 +24,8 @@ import {
2424import { rewriteDeniedImports } from '../import-protection/rewrite'
2525import {
2626 buildCodeSnippet ,
27+ findOriginalUsageLocation ,
28+ findPostCompileUsageLocation ,
2729 normalizeSourceMap ,
2830 pickOriginalCodeFromSourcesContent ,
2931} from '../import-protection/sourceLocation'
@@ -40,6 +42,7 @@ import {
4042 loadSilentMockModule ,
4143} from '../import-protection/virtualModules'
4244import {
45+ buildSourceCandidates ,
4346 canonicalizeResolvedId ,
4447 checkFileDenial ,
4548 clearNormalizeFilePathCache ,
@@ -57,6 +60,7 @@ import type { FileMatchers } from '../import-protection/utils'
5760import type {
5861 SourceMapLike ,
5962 TransformResult ,
63+ TransformResultProvider ,
6064} from '../import-protection/sourceLocation'
6165import type { Loc , TraceStep , ViolationInfo } from '../import-protection/trace'
6266import type { CompileStartFrameworkOptions , GetConfigFn } from '../types'
@@ -237,7 +241,7 @@ interface CompilationTransformResultProvider {
237241 getTransformResult : ( module : RspackModule ) => TransformResult | undefined
238242}
239243
240- // An identity-only snapshot of one module's active compilation connections.
244+ // An identity-only snapshot of one module's compilation connections.
241245// Derived paths, requests, locations, and diagnostic indexes live elsewhere.
242246interface CompilationImport {
243247 dependency : RspackDependency
@@ -698,7 +702,7 @@ function addEntryModulesToGraph(opts: {
698702 }
699703}
700704
701- function forEachActiveModules ( opts : {
705+ function forEachModules ( opts : {
702706 compilation : RspackCompilation
703707 modules : Array < RspackModule >
704708 visitNode : ( node : RspackModuleGraphNode ) => void
@@ -717,10 +721,6 @@ function forEachActiveModules(opts: {
717721 continue
718722 }
719723
720- if ( connection . getActiveState ( undefined ) !== true ) {
721- continue
722- }
723-
724724 // Only consider modules that are not errored
725725 if ( 'error' in connectedModule && connectedModule . error ) {
726726 continue
@@ -969,9 +969,9 @@ async function mapCompilationLocation(opts: {
969969 provider : CompilationTransformResultProvider
970970 importer : string
971971 importerModule : RspackModule
972- generatedLoc ?: Loc
972+ dependencyLoc ?: Loc
973973} ) : Promise < Loc | undefined > {
974- if ( ! opts . generatedLoc ) {
974+ if ( ! opts . dependencyLoc ) {
975975 return undefined
976976 }
977977
@@ -982,8 +982,8 @@ async function mapCompilationLocation(opts: {
982982
983983 const fallback : Loc = {
984984 file : normalizeFilePath ( opts . importer ) ,
985- line : opts . generatedLoc . line ,
986- column : opts . generatedLoc . column ,
985+ line : opts . dependencyLoc . line ,
986+ column : opts . dependencyLoc . column ,
987987 }
988988 const consumer = await getCompilationSourceMapConsumer ( map )
989989 if ( ! consumer ) {
@@ -992,8 +992,8 @@ async function mapCompilationLocation(opts: {
992992
993993 try {
994994 const original = consumer . originalPositionFor ( {
995- line : opts . generatedLoc . line ,
996- column : Math . max ( 0 , opts . generatedLoc . column - 1 ) ,
995+ line : opts . dependencyLoc . line ,
996+ column : Math . max ( 0 , opts . dependencyLoc . column - 1 ) ,
997997 } )
998998 if ( original . line != null && original . column != null ) {
999999 return {
@@ -1041,6 +1041,52 @@ function getCompilationSourceMapConsumer(
10411041 return consumer
10421042}
10431043
1044+ async function resolveImporterLocation ( opts : {
1045+ config : PluginConfig
1046+ provider : CompilationTransformResultProvider
1047+ importer : string
1048+ importerModule : RspackModule
1049+ source : string
1050+ resolved ?: string
1051+ dependencyLoc ?: Loc
1052+ envType : 'client' | 'server'
1053+ } ) : Promise < Loc | undefined > {
1054+ const dependencyLoc = await mapCompilationLocation ( {
1055+ provider : opts . provider ,
1056+ importer : opts . importer ,
1057+ importerModule : opts . importerModule ,
1058+ dependencyLoc : opts . dependencyLoc ,
1059+ } )
1060+ if ( dependencyLoc ) {
1061+ return dependencyLoc
1062+ }
1063+
1064+ const provider : TransformResultProvider = {
1065+ getTransformResult : ( ) =>
1066+ opts . provider . getTransformResult ( opts . importerModule ) ,
1067+ }
1068+ for ( const source of buildSourceCandidates (
1069+ opts . source ,
1070+ opts . resolved ,
1071+ opts . config . root ,
1072+ ) ) {
1073+ const loc =
1074+ ( await findPostCompileUsageLocation ( provider , opts . importer , source ) ) ??
1075+ findOriginalUsageLocation (
1076+ provider ,
1077+ opts . importer ,
1078+ source ,
1079+ opts . envType ,
1080+ opts . config . root ,
1081+ )
1082+ if ( loc ) {
1083+ return loc
1084+ }
1085+ }
1086+
1087+ return undefined
1088+ }
1089+
10441090async function rebuildAndAnnotateTrace ( opts : {
10451091 provider : CompilationTransformResultProvider
10461092 importGraph : ImportGraph
@@ -1066,7 +1112,7 @@ async function rebuildAndAnnotateTrace(opts: {
10661112 provider : opts . provider ,
10671113 importer : step . file ,
10681114 importerModule : edge . importerModule ,
1069- generatedLoc : getDependencyLocation ( edge . dependency ) ,
1115+ dependencyLoc : getDependencyLocation ( edge . dependency ) ,
10701116 } )
10711117 : undefined
10721118 if ( loc ) {
@@ -1109,11 +1155,15 @@ async function buildViolationInfo(opts: {
11091155 opts . perf ?. count ( 'violations.enriched' )
11101156
11111157 const importerLocStartedAt = opts . perf ? performance . now ( ) : 0
1112- const importerLoc = await mapCompilationLocation ( {
1158+ const importerLoc = await resolveImporterLocation ( {
1159+ config : opts . config ,
11131160 provider : opts . provider ,
11141161 importer : opts . importer ,
11151162 importerModule : opts . importerModule ,
1116- generatedLoc : opts . importLoc ,
1163+ source : opts . source ,
1164+ resolved : opts . resolved ,
1165+ dependencyLoc : opts . importLoc ,
1166+ envType : opts . envType ,
11171167 } )
11181168 if ( opts . perf ) {
11191169 opts . perf . time ( 'violations.resolveImporterLocation' , importerLocStartedAt )
@@ -1699,7 +1749,7 @@ export function registerImportProtection(
16991749 } )
17001750 const forEachStartedAt = perf ? performance . now ( ) : 0
17011751 const moduleGraphNodes : Array < RspackModuleGraphNode > = [ ]
1702- forEachActiveModules ( {
1752+ forEachModules ( {
17031753 compilation : context . compilation ,
17041754 modules : allModules ,
17051755 visitNode ( node ) {
@@ -1708,10 +1758,10 @@ export function registerImportProtection(
17081758 } ,
17091759 } )
17101760 if ( perf ) {
1711- perf . time ( 'processAssets.forEachActiveModules ' , forEachStartedAt )
1761+ perf . time ( 'processAssets.forEachModules ' , forEachStartedAt )
17121762 perf . count ( 'processAssets.modules.collected' , moduleGraphNodes . length )
17131763 perf . count (
1714- 'processAssets.imports.active ' ,
1764+ 'processAssets.imports.collected ' ,
17151765 moduleGraphNodes . reduce (
17161766 ( total , node ) => total + node . imports . length ,
17171767 0 ,
0 commit comments