Skip to content

Commit

Permalink
fix: define plugin not ignore file names (#6340)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Mar 3, 2022
1 parent 96573db commit 7215a03
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
9 changes: 9 additions & 0 deletions packages/playground/define/__tests__/define.spec.ts
Expand Up @@ -20,4 +20,13 @@ test('string', async () => {
expect(await page.textContent('.spread-array')).toBe(
JSON.stringify([...defines.__STRING__])
)
expect(await page.textContent('.import-file')).not.toBe(
`import * from "${defines.__IMPORT_FILE_NAME__}"`
)
expect(await page.textContent('.export-file')).not.toBe(
`export * from "${defines.__EXPORT_FILE_NAME__}"`
)
expect(await page.textContent('.path')).not.toBe(
`import * from "xxxx/${defines.PATH}"`
)
})
6 changes: 6 additions & 0 deletions packages/playground/define/index.html
Expand Up @@ -9,6 +9,9 @@ <h1>Define</h1>
<p>process as property: <code class="process-as-property"></code></p>
<p>spread object: <code class="spread-object"></code></p>
<p>spread array: <code class="spread-array"></code></p>
<p>import file: <code class="import-file"></code></p>
<p>export file: <code class="export-file"></code></p>
<p>path: <code class="path"></code></p>

<script type="module">
const __VAR_NAME__ = true // ensure define doesn't replace var name
Expand All @@ -26,6 +29,9 @@ <h1>Define</h1>
})
)
text('.spread-array', JSON.stringify([...`"${__STRING__}"`]))
text('.import-file', `import * from "__IMPORT_FILE_NAME__"`)
text('.export-file', `export * from "__EXPORT_FILE_NAME__"`)
text('.path', `import * from "xxxx/PATH"`)

function text(el, text) {
document.querySelector(el).textContent = text
Expand Down
5 changes: 4 additions & 1 deletion packages/playground/define/vite.config.js
Expand Up @@ -16,6 +16,9 @@ module.exports = {
}
},
__VAR_NAME__: false,
'process.env.SOMEVAR': '"SOMEVAR"'
'process.env.SOMEVAR': '"SOMEVAR"',
__IMPORT_FILE_NAME__: '"importFileName"',
__EXPORT_FILE_NAME__: '"exportFileName"',
PATH: '"filePath"'
}
}
39 changes: 24 additions & 15 deletions packages/vite/src/node/plugins/define.ts
Expand Up @@ -53,7 +53,6 @@ export function definePlugin(config: ResolvedConfig): Plugin {
'globalThis.process.env.': `({}).`
})
}

const replacements: Record<string, string> = {
...(isNeedProcessEnv ? processNodeEnv : {}),
...userDefine,
Expand All @@ -62,20 +61,30 @@ export function definePlugin(config: ResolvedConfig): Plugin {
}

const replacementsKeys = Object.keys(replacements)
const pattern = replacementsKeys.length
? new RegExp(
// Do not allow preceding '.', but do allow preceding '...' for spread operations
'(?<!(?<!\\.\\.)\\.)\\b(' +
replacementsKeys
.map((str) => {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
})
.join('|') +
// prevent trailing assignments
')\\b(?!\\s*?=[^=])',
'g'
)
: null

if (!replacementsKeys.length) {
return [replacements, null]
}

const replacementsStr = replacementsKeys
.map((str) => {
return str.replace(/[-[\]/{}()*+?.\\^$|]/g, '\\$&')
})
.join('|')

// The following characters are not allowed because they are String boundaries
const characters = '[\'"`\\/-_]'

const pattern = new RegExp(
`(?<!${characters})` +
// Do not allow preceding '.', but do allow preceding '...' for spread operations
'(?<!(?<!\\.\\.)\\.)' +
`\\b(${replacementsStr})\\b` +
`(?!${characters})` +
// prevent trailing assignments
'(?!\\s*?=[^=])',
'g'
)

return [replacements, pattern]
}
Expand Down

0 comments on commit 7215a03

Please sign in to comment.