@@ -18,6 +18,7 @@ import { ModuleDependencyGraph } from "./module-dependency-graph.js";
1818import type { Module , Registry } from "./registry.js" ;
1919import { ScenarioRegistry } from "./scenario-registry.js" ;
2020import { uncachedImport } from "./uncached-import.js" ;
21+ import { toForwardSlashPath } from "../util/forward-slash-path.js" ;
2122import { unescapePathForWindows } from "../util/windows-escape.js" ;
2223
2324const { uncachedRequire } = await import ( "./uncached-require.cjs" ) ;
@@ -69,10 +70,13 @@ export class ModuleLoader extends EventTarget {
6970 scenarioRegistry ?: ScenarioRegistry ,
7071 ) {
7172 super ( ) ;
72- this . basePath = basePath . replaceAll ( "\\" , "/" ) ;
73+ this . basePath = toForwardSlashPath ( basePath ) ;
7374 this . registry = registry ;
7475 this . contextRegistry = contextRegistry ;
75- this . scenariosPath = scenariosPath ?. replaceAll ( "\\" , "/" ) ;
76+ this . scenariosPath =
77+ scenariosPath === undefined
78+ ? undefined
79+ : toForwardSlashPath ( scenariosPath ) ;
7680 this . scenarioRegistry = scenarioRegistry ;
7781 this . fileDiscovery = new FileDiscovery ( this . basePath ) ;
7882 }
@@ -98,7 +102,7 @@ export class ModuleLoader extends EventTarget {
98102 )
99103 return ;
100104
101- const pathName = pathNameOriginal . replaceAll ( "\\" , "/" ) ;
105+ const pathName = toForwardSlashPath ( pathNameOriginal ) ;
102106
103107 if ( pathName . includes ( "$.context" ) && eventName === "add" ) {
104108 process . stdout . write (
@@ -114,17 +118,18 @@ export class ModuleLoader extends EventTarget {
114118
115119 const parts = nodePath . parse ( pathName . replace ( this . basePath , "" ) ) ;
116120 const url = unescapePathForWindows (
117- `/${ parts . dir } /${ parts . name } `
118- . replaceAll ( "\\" , "/" )
119- . replaceAll ( / \/ + / gu, "/" ) ,
121+ toForwardSlashPath ( `/${ parts . dir } /${ parts . name } ` ) . replaceAll (
122+ / \/ + / gu,
123+ "/" ,
124+ ) ,
120125 ) ;
121126
122127 if ( eventName === "unlink" ) {
123128 this . registry . remove ( url ) ;
124129 this . dispatchEvent ( new Event ( "remove" ) ) ;
125130 if ( this . isContextFile ( pathName ) ) {
126131 this . contextRegistry . remove (
127- unescapePathForWindows ( parts . dir ) . replaceAll ( "\\" , "/" ) || "/" ,
132+ unescapePathForWindows ( toForwardSlashPath ( parts . dir ) ) || "/" ,
128133 ) ;
129134 }
130135 return ;
@@ -155,7 +160,7 @@ export class ModuleLoader extends EventTarget {
155160
156161 if ( ! [ "add" , "change" , "unlink" ] . includes ( eventName ) ) return ;
157162
158- const pathName = pathNameOriginal . replaceAll ( "\\" , "/" ) ;
163+ const pathName = toForwardSlashPath ( pathNameOriginal ) ;
159164
160165 if ( eventName === "unlink" ) {
161166 const fileKey = this . scenarioFileKey ( pathName ) ;
@@ -215,18 +220,18 @@ export class ModuleLoader extends EventTarget {
215220 }
216221
217222 private scenarioFileKey ( pathName : string ) : string {
218- const normalizedScenariosPath = ( this . scenariosPath ?? "" ) . replaceAll (
219- "\\" ,
220- "/" ,
223+ const normalizedScenariosPath = toForwardSlashPath (
224+ this . scenariosPath ?? "" ,
225+ ) ;
226+ const directory = toForwardSlashPath (
227+ dirname ( pathName . slice ( normalizedScenariosPath . length ) ) ,
221228 ) ;
222- const directory = dirname (
223- pathName . slice ( normalizedScenariosPath . length ) ,
224- ) . replaceAll ( "\\" , "/" ) ;
225229 const name = nodePath . parse ( basename ( pathName ) ) . name ;
226230 const url = unescapePathForWindows (
227- `/${ nodePath . join ( directory , name ) } `
228- . replaceAll ( "\\" , "/" )
229- . replaceAll ( / \/ + / gu, "/" ) ,
231+ toForwardSlashPath ( `/${ nodePath . join ( directory , name ) } ` ) . replaceAll (
232+ / \/ + / gu,
233+ "/" ,
234+ ) ,
230235 ) ;
231236
232237 return url . slice ( 1 ) ; // strip leading "/"
@@ -258,15 +263,14 @@ export class ModuleLoader extends EventTarget {
258263 private async loadEndpoint ( pathName : string ) {
259264 debug ( "importing module: %s" , pathName ) ;
260265
261- const directory = dirname ( pathName . slice ( this . basePath . length ) ) . replaceAll (
262- "\\" ,
263- "/" ,
266+ const directory = toForwardSlashPath (
267+ dirname ( pathName . slice ( this . basePath . length ) ) ,
264268 ) ;
265269
266270 const url = unescapePathForWindows (
267- `/ ${ nodePath . join ( directory , nodePath . parse ( basename ( pathName ) ) . name ) } `
268- . replaceAll ( "\\" , "/" )
269- . replaceAll ( / \/ + / gu, "/" ) ,
271+ toForwardSlashPath (
272+ `/ ${ nodePath . join ( directory , nodePath . parse ( basename ( pathName ) ) . name ) } ` ,
273+ ) . replaceAll ( / \/ + / gu, "/" ) ,
270274 ) ;
271275
272276 debug ( `loading pathName from dependencyGraph: ${ pathName } ` ) ;
@@ -290,9 +294,9 @@ export class ModuleLoader extends EventTarget {
290294 importError instanceof SyntaxError ||
291295 String ( importError ) . startsWith ( "SyntaxError:" ) ;
292296
293- const displayPath = nodePath
294- . relative ( process . cwd ( ) , unescapePathForWindows ( pathName ) )
295- . replaceAll ( "\\" , "/" ) ;
297+ const displayPath = toForwardSlashPath (
298+ nodePath . relative ( process . cwd ( ) , unescapePathForWindows ( pathName ) ) ,
299+ ) ;
296300
297301 const message = isSyntaxError
298302 ? `There is a syntax error in the route file: ${ displayPath } `
0 commit comments