Skip to content

Commit

Permalink
fix: dont replace define in json (#7294)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Mar 12, 2022
1 parent 61fc601 commit fc5c937
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/playground/define/__tests__/define.spec.ts
Expand Up @@ -22,4 +22,5 @@ test('string', async () => {
)
// html would't need to define replacement
expect(await page.textContent('.exp-define')).toBe('__EXP__')
expect(await page.textContent('.import-json')).toBe('__EXP__')
})
3 changes: 3 additions & 0 deletions packages/playground/define/data.json
@@ -0,0 +1,3 @@
{
"foo": "__EXP__"
}
4 changes: 4 additions & 0 deletions packages/playground/define/index.html
Expand Up @@ -10,6 +10,7 @@ <h1>Define</h1>
<p>spread object: <code class="spread-object"></code></p>
<p>spread array: <code class="spread-array"></code></p>
<p>define variable in html: <code class="exp-define">__EXP__</code></p>
<p>import json: <code class="import-json"></code></p>

<script type="module">
const __VAR_NAME__ = true // ensure define doesn't replace var name
Expand All @@ -28,6 +29,9 @@ <h1>Define</h1>
)
text('.spread-array', JSON.stringify([...`"${__STRING__}"`]))

import dataJson from './data.json'
text('.import-json', dataJson.foo)

function text(el, text) {
document.querySelector(el).textContent = text
}
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/plugins/define.ts
Expand Up @@ -5,6 +5,9 @@ import type { Plugin } from '../plugin'
import { isCSSRequest } from './css'
import { isHTMLRequest } from './html'

const nonJsRe = /\.(json)($|\?)/
const isNonJsRequest = (request: string): boolean => nonJsRe.test(request)

export function definePlugin(config: ResolvedConfig): Plugin {
const isBuild = config.command === 'build'

Expand Down Expand Up @@ -99,6 +102,7 @@ export function definePlugin(config: ResolvedConfig): Plugin {
// exclude html, css and static assets for performance
isHTMLRequest(id) ||
isCSSRequest(id) ||
isNonJsRequest(id) ||
config.assetsInclude(id)
) {
return
Expand Down

0 comments on commit fc5c937

Please sign in to comment.