Skip to content

Commit

Permalink
refactor: re-organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Mar 3, 2024
1 parent 4200731 commit f16a358
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,31 @@ import MagicString from 'magic-string'
import { type Options, type ReplaceItem, resolveOptions } from './core/options'

export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
let {
const options = resolveOptions(rawOptions)
const {
include,
exclude,
preventAssignment,
objectGuards,
sourceMap,
delimiters,
values,
enforce,
} = resolveOptions(rawOptions)
delimiters,
objectGuards,
preventAssignment,
} = options
const filter = createFilter(include, exclude)

const stringValues = values.filter(
const stringValues = options.values.filter(
(value): value is ReplaceItem<string> => typeof value.find === 'string',
)
const regexpValues = values.filter(
const regexpValues = options.values.filter(
(value): value is ReplaceItem<RegExp> => value.find instanceof RegExp,
)

if (objectGuards) expandTypeofReplacements(stringValues)
const escapedKeys = stringValues
.map(({ find }) => find)
.sort(longest)
// eslint-disable-next-line unicorn/no-array-callback-reference
.map(escape)
const lookahead = preventAssignment ? '(?!\\s*(=[^=]|:[^:]))' : ''
const pattern = new RegExp(
`${delimiters[0]}(${escapedKeys.join('|')})${delimiters[1]}${lookahead}`,
'g',
)
const pattern = buildStringPattern()

const values = [...regexpValues]
if (pattern) {
values.unshift({ find: pattern, replacement: null! })
}

const name = 'unplugin-replace'
return {
Expand All @@ -49,7 +44,7 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
},

transformInclude(id) {
if (escapedKeys.length === 0 && regexpValues.length === 0) return false
if (values.length === 0) return false
return filter(id)
},

Expand All @@ -59,7 +54,8 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {

vite: {
configResolved(config) {
sourceMap = config.command === 'build' ? !!config.build.sourcemap : true
options.sourceMap =
config.command === 'build' ? !!config.build.sourcemap : true
},
},
}
Expand All @@ -71,7 +67,7 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
}

const result: TransformResult = { code: magicString.toString() }
if (sourceMap) {
if (options.sourceMap) {
result.map = magicString.generateMap({ hires: true })
}
return result
Expand All @@ -85,10 +81,6 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {
let has = false
let match: RegExpExecArray | null

const values: ReplaceItem<RegExp>[] = [...regexpValues]
if (escapedKeys.length > 0)
values.push({ find: pattern, replacement: null! })

for (const { find, replacement } of values) {
while ((match = find.exec(code))) {
has = true
Expand All @@ -109,6 +101,23 @@ export default createUnplugin<Options | undefined, false>((rawOptions = {}) => {

return has
}

function buildStringPattern(): RegExp | undefined {
const escapedKeys = stringValues
.map(({ find }) => find)
.sort(longest)
// eslint-disable-next-line unicorn/no-array-callback-reference
.map(escape)
const lookahead = preventAssignment ? '(?!\\s*(=[^=]|:[^:]))' : ''
const pattern = new RegExp(
`${delimiters[0]}(${escapedKeys.join('|')})${delimiters[1]}${lookahead}`,
'g',
)

if (escapedKeys.length > 0) {
return pattern
}
}
})

function escape(str: string) {
Expand Down

0 comments on commit f16a358

Please sign in to comment.