Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 馃悰 define plugin not ignore file names #6340

Merged
merged 6 commits into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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"'
}
}
18 changes: 10 additions & 8 deletions packages/vite/src/node/plugins/define.ts
Expand Up @@ -59,16 +59,18 @@ export function definePlugin(config: ResolvedConfig): Plugin {
...processEnv
}

const characters = '[\'"`\\/]'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const characters = '[\'"`\\/]'
const characters = '[\'"`\\/_-]'

For example, the following code

import * from "xxxx/xx(_|-)PATH


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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try to split this regex to multiple lines so the comments match, and would be more readable too.

'g'
)

Expand Down