@@ -4,36 +4,31 @@ import MagicString from 'magic-string'
44import { type Options , type ReplaceItem , resolveOptions } from './core/options'
55
66export default createUnplugin < Options | undefined , false > ( ( rawOptions = { } ) => {
7- let {
7+ const options = resolveOptions ( rawOptions )
8+ const {
89 include,
910 exclude,
10- preventAssignment,
11- objectGuards,
12- sourceMap,
13- delimiters,
14- values,
1511 enforce,
16- } = resolveOptions ( rawOptions )
12+ delimiters,
13+ objectGuards,
14+ preventAssignment,
15+ } = options
1716 const filter = createFilter ( include , exclude )
1817
19- const stringValues = values . filter (
18+ const stringValues = options . values . filter (
2019 ( value ) : value is ReplaceItem < string > => typeof value . find === 'string' ,
2120 )
22- const regexpValues = values . filter (
21+ const regexpValues = options . values . filter (
2322 ( value ) : value is ReplaceItem < RegExp > => value . find instanceof RegExp ,
2423 )
2524
2625 if ( objectGuards ) expandTypeofReplacements ( stringValues )
27- const escapedKeys = stringValues
28- . map ( ( { find } ) => find )
29- . sort ( longest )
30- // eslint-disable-next-line unicorn/no-array-callback-reference
31- . map ( escape )
32- const lookahead = preventAssignment ? '(?!\\s*(=[^=]|:[^:]))' : ''
33- const pattern = new RegExp (
34- `${ delimiters [ 0 ] } (${ escapedKeys . join ( '|' ) } )${ delimiters [ 1 ] } ${ lookahead } ` ,
35- 'g' ,
36- )
26+ const pattern = buildStringPattern ( )
27+
28+ const values = [ ...regexpValues ]
29+ if ( pattern ) {
30+ values . unshift ( { find : pattern , replacement : null ! } )
31+ }
3732
3833 const name = 'unplugin-replace'
3934 return {
@@ -49,7 +44,7 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
4944 } ,
5045
5146 transformInclude ( id ) {
52- if ( escapedKeys . length === 0 && regexpValues . length === 0 ) return false
47+ if ( values . length === 0 ) return false
5348 return filter ( id )
5449 } ,
5550
@@ -59,7 +54,8 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
5954
6055 vite : {
6156 configResolved ( config ) {
62- sourceMap = config . command === 'build' ? ! ! config . build . sourcemap : true
57+ options . sourceMap =
58+ config . command === 'build' ? ! ! config . build . sourcemap : true
6359 } ,
6460 } ,
6561 }
@@ -71,7 +67,7 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
7167 }
7268
7369 const result : TransformResult = { code : magicString . toString ( ) }
74- if ( sourceMap ) {
70+ if ( options . sourceMap ) {
7571 result . map = magicString . generateMap ( { hires : true } )
7672 }
7773 return result
@@ -85,10 +81,6 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
8581 let has = false
8682 let match : RegExpExecArray | null
8783
88- const values : ReplaceItem < RegExp > [ ] = [ ...regexpValues ]
89- if ( escapedKeys . length > 0 )
90- values . push ( { find : pattern , replacement : null ! } )
91-
9284 for ( const { find, replacement } of values ) {
9385 while ( ( match = find . exec ( code ) ) ) {
9486 has = true
@@ -109,6 +101,23 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
109101
110102 return has
111103 }
104+
105+ function buildStringPattern ( ) : RegExp | undefined {
106+ const escapedKeys = stringValues
107+ . map ( ( { find } ) => find )
108+ . sort ( longest )
109+ // eslint-disable-next-line unicorn/no-array-callback-reference
110+ . map ( escape )
111+ const lookahead = preventAssignment ? '(?!\\s*(=[^=]|:[^:]))' : ''
112+ const pattern = new RegExp (
113+ `${ delimiters [ 0 ] } (${ escapedKeys . join ( '|' ) } )${ delimiters [ 1 ] } ${ lookahead } ` ,
114+ 'g' ,
115+ )
116+
117+ if ( escapedKeys . length > 0 ) {
118+ return pattern
119+ }
120+ }
112121} )
113122
114123function escape ( str : string ) {
0 commit comments