@@ -2,6 +2,7 @@ import fs from 'fs';
22import path from 'path' ;
33import { loadConfigFromFile , loadEnv , normalizePath } from 'vite' ;
44import { runtime_directory } from '../core/utils.js' ;
5+ import { posixify } from '../utils/filesystem.js' ;
56
67/**
78 * @param {import('vite').ResolvedConfig } config
@@ -113,18 +114,22 @@ export function get_aliases(config) {
113114 ] ;
114115
115116 for ( let [ key , value ] of Object . entries ( config . alias ) ) {
117+ value = posixify ( value ) ;
116118 if ( value . endsWith ( '/*' ) ) {
117119 value = value . slice ( 0 , - 2 ) ;
118120 }
119121 if ( key . endsWith ( '/*' ) ) {
120122 // Doing just `{ find: key.slice(0, -2) ,..}` would mean `import .. from "key"` would also be matched, which we don't want
121123 alias . push ( {
122- find : new RegExp ( `^${ key . slice ( 0 , - 2 ) } \\/(.+)$` ) ,
124+ find : new RegExp ( `^${ escape_for_regexp ( key . slice ( 0 , - 2 ) ) } \\/(.+)$` ) ,
123125 replacement : `${ path . resolve ( value ) } /$1`
124126 } ) ;
125127 } else if ( key + '/*' in config . alias ) {
126128 // key and key/* both exist -> the replacement for key needs to happen _only_ on import .. from "key"
127- alias . push ( { find : new RegExp ( `^${ key } $` ) , replacement : path . resolve ( value ) } ) ;
129+ alias . push ( {
130+ find : new RegExp ( `^${ escape_for_regexp ( key ) } $` ) ,
131+ replacement : path . resolve ( value )
132+ } ) ;
128133 } else {
129134 alias . push ( { find : key , replacement : path . resolve ( value ) } ) ;
130135 }
@@ -133,6 +138,13 @@ export function get_aliases(config) {
133138 return alias ;
134139}
135140
141+ /**
142+ * @param {string } str
143+ */
144+ function escape_for_regexp ( str ) {
145+ return str . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, ( match ) => '\\' + match ) ;
146+ }
147+
136148/**
137149 * Given an entry point like [cwd]/src/hooks, returns a filename like [cwd]/src/hooks.js or [cwd]/src/hooks/index.js
138150 * @param {string } entry
0 commit comments