-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
fix: tree shake stringified JSON imports #19189
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+68
−5
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import { readFileSync } from 'node:fs' | ||
| import { expect, test } from 'vitest' | ||
| import deepJson from 'vue/package.json' | ||
| import testJson from '../../test.json' | ||
| import hmrJson from '../../hmr.json' | ||
| import { editFile, isBuild, isServe, page, untilUpdated } from '~utils' | ||
|
|
||
| const stringified = JSON.stringify(testJson) | ||
| const deepStringified = JSON.stringify(deepJson) | ||
| const hmrStringified = JSON.stringify(hmrJson) | ||
|
|
||
| test('default import', async () => { | ||
| expect(await page.textContent('.full')).toBe(stringified) | ||
| }) | ||
|
|
||
| test('named import', async () => { | ||
| expect(await page.textContent('.named')).toBe(testJson.hello) | ||
| }) | ||
|
|
||
| test('deep import', async () => { | ||
| expect(await page.textContent('.deep-full')).toBe(deepStringified) | ||
| }) | ||
|
|
||
| test('named deep import', async () => { | ||
| expect(await page.textContent('.deep-named')).toBe(deepJson.name) | ||
| }) | ||
|
|
||
| test('dynamic import', async () => { | ||
| expect(await page.textContent('.dynamic')).toBe(stringified) | ||
| }) | ||
|
|
||
| test('dynamic import, named', async () => { | ||
| expect(await page.textContent('.dynamic-named')).toBe(testJson.hello) | ||
| }) | ||
|
|
||
| test('fetch', async () => { | ||
| expect(await page.textContent('.fetch')).toBe(stringified) | ||
| }) | ||
|
|
||
| test('?url', async () => { | ||
| expect(await page.textContent('.url')).toMatch( | ||
| isBuild ? 'data:application/json' : '/test.json', | ||
| ) | ||
| }) | ||
|
|
||
| test('?raw', async () => { | ||
| expect(await page.textContent('.raw')).toBe( | ||
| readFileSync(require.resolve('../../test.json'), 'utf-8'), | ||
| ) | ||
| }) | ||
|
|
||
| test.runIf(isServe)('should full reload', async () => { | ||
| expect(await page.textContent('.hmr')).toBe(hmrStringified) | ||
|
|
||
| editFile('hmr.json', (code) => | ||
| code.replace('"this is hmr json"', '"this is hmr update json"'), | ||
| ) | ||
| await untilUpdated( | ||
| () => page.textContent('.hmr'), | ||
| '"this is hmr update json"', | ||
| ) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import fs from 'node:fs' | ||
| import path from 'node:path' | ||
| import { expect, test } from 'vitest' | ||
| import { isBuild, readFile, testDir } from '~utils' | ||
|
|
||
| test.runIf(isBuild)('tree shake json', () => { | ||
| const assetsDir = path.resolve(testDir, 'dist/assets') | ||
| const files = fs.readdirSync(assetsDir) | ||
|
|
||
| const jsFile = files.find((f) => f.endsWith('.js')) | ||
| const jsContent = readFile(path.resolve(assetsDir, jsFile)) | ||
|
|
||
| expect(jsContent).not.toContain('DEV_ONLY_JSON') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "dev": "DEV_ONLY_JSON" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "hmr": "this is hmr json" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| <h2>Normal Import</h2> | ||
| <pre class="full"></pre> | ||
| <pre class="named"></pre> | ||
|
|
||
| <h2>Deep Import</h2> | ||
| <pre class="deep-full"></pre> | ||
| <pre class="deep-named"></pre> | ||
|
|
||
| <h2>Dynamic Import</h2> | ||
| <pre class="dynamic"></pre> | ||
| <pre class="dynamic-named"></pre> | ||
|
|
||
| <h2>fetch</h2> | ||
| <pre class="fetch"></pre> | ||
|
|
||
| <h2>Importing as URL</h2> | ||
| <pre class="url"></pre> | ||
|
|
||
| <h2>Raw Import</h2> | ||
| <pre class="raw"></pre> | ||
|
|
||
| <h2>JSON Module</h2> | ||
| <pre class="module"></pre> | ||
|
|
||
| <h2>Has BOM Tag</h2> | ||
| <pre class="bom"></pre> | ||
|
|
||
| <h2>HMR</h2> | ||
| <pre class="hmr"></pre> | ||
|
|
||
| <script type="module"> | ||
| import json, { hello } from './test.json' | ||
| import deepJson, { name } from 'vue/package.json' | ||
| import dev from './dev.json' | ||
|
|
||
| if (import.meta.env.MODE === 'dev') { | ||
| text('.dev', dev) | ||
| } | ||
|
|
||
| text('.full', JSON.stringify(json)) | ||
| text('.named', hello) | ||
|
|
||
| text('.deep-full', JSON.stringify(deepJson)) | ||
| text('.deep-named', name) | ||
|
|
||
| import('/test.json').then((mod) => { | ||
| text('.dynamic', JSON.stringify(mod.default)) | ||
| text('.dynamic-named', mod.hello) | ||
| }) | ||
|
|
||
| fetch(import.meta.env.DEV ? '/test.json' : '/public.json') | ||
| .then((r) => r.json()) | ||
| .then((data) => { | ||
| text('.fetch', JSON.stringify(data)) | ||
| }) | ||
|
|
||
| import url from './test.json?url' | ||
| text('.url', url) | ||
|
|
||
| import raw from './test.json?raw' | ||
| text('.raw', raw) | ||
|
|
||
| import moduleJSON from '@vitejs/test-json-module' | ||
| text('.module', JSON.stringify(moduleJSON)) | ||
|
|
||
| import hasBomJson from './json-bom/has-bom.json' | ||
| text('.bom', JSON.stringify(hasBomJson)) | ||
|
|
||
| import hmrJSON from './hmr.json' | ||
| text('.hmr', JSON.stringify(hmrJSON)) | ||
|
|
||
| function text(sel, text) { | ||
| document.querySelector(sel).textContent = text | ||
| } | ||
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "description": "This file is marked with BOM.", | ||
| "message": "If the parsing is successful, the BOM tag has been removed." | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "hello": "hi" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| { | ||
| "name": "@vitejs/test-json-module", | ||
| "version": "0.0.0" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "name": "@vitejs/test-json", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite", | ||
| "build": "vite build", | ||
| "debug": "node --inspect-brk ../../packages/vite/bin/vite", | ||
| "preview": "vite preview" | ||
| }, | ||
| "devDependencies": { | ||
| "@vitejs/test-json-module": "file:./json-module", | ||
| "express": "^5.0.1", | ||
| "vue": "^3.5.13" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "hello": "this is json" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "hello": "this is json" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { defineConfig } from 'vite' | ||
|
|
||
| export default defineConfig({ json: { stringify: true } }) | ||
hi-ogawa marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| import fs from 'node:fs' | ||
| import path from 'node:path' | ||
| import { expect, test } from 'vitest' | ||
| import { isBuild, readFile, testDir } from '~utils' | ||
|
|
||
| test.runIf(isBuild)('tree shake json', () => { | ||
| const assetsDir = path.resolve(testDir, 'dist/assets') | ||
| const files = fs.readdirSync(assetsDir) | ||
|
|
||
| const jsFile = files.find((f) => f.endsWith('.js')) | ||
| const jsContent = readFile(path.resolve(assetsDir, jsFile)) | ||
|
|
||
| expect(jsContent).not.toContain('DEV_ONLY_JSON') | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "dev": "DEV_ONLY_JSON" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.