From 52e9e93ded535319ed2fac22aeaa33677151f055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Padier?= Date: Sun, 12 Jan 2025 20:03:41 +0100 Subject: [PATCH 1/4] Add tests for tree shaking json on build --- .../__tests__/csr/json-csr.spec.ts | 62 +++++++++++++++ .../json-stringify/__tests__/minify.spec.ts | 14 ++++ playground/json-stringify/dev.json | 3 + playground/json-stringify/hmr.json | 3 + playground/json-stringify/index.html | 75 +++++++++++++++++++ .../json-stringify/json-bom/has-bom.json | 4 + .../json-stringify/json-module/index.json | 3 + .../json-stringify/json-module/package.json | 4 + playground/json-stringify/package.json | 17 +++++ playground/json-stringify/public/public.json | 3 + playground/json-stringify/test.json | 3 + playground/json-stringify/vite.config.js | 3 + playground/json/__tests__/minify.spec.ts | 14 ++++ playground/json/dev.json | 3 + playground/json/index.html | 5 ++ 15 files changed, 216 insertions(+) create mode 100644 playground/json-stringify/__tests__/csr/json-csr.spec.ts create mode 100644 playground/json-stringify/__tests__/minify.spec.ts create mode 100644 playground/json-stringify/dev.json create mode 100644 playground/json-stringify/hmr.json create mode 100644 playground/json-stringify/index.html create mode 100644 playground/json-stringify/json-bom/has-bom.json create mode 100644 playground/json-stringify/json-module/index.json create mode 100644 playground/json-stringify/json-module/package.json create mode 100644 playground/json-stringify/package.json create mode 100644 playground/json-stringify/public/public.json create mode 100644 playground/json-stringify/test.json create mode 100644 playground/json-stringify/vite.config.js create mode 100644 playground/json/__tests__/minify.spec.ts create mode 100644 playground/json/dev.json diff --git a/playground/json-stringify/__tests__/csr/json-csr.spec.ts b/playground/json-stringify/__tests__/csr/json-csr.spec.ts new file mode 100644 index 00000000000000..cec1fb5f0eb0ba --- /dev/null +++ b/playground/json-stringify/__tests__/csr/json-csr.spec.ts @@ -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"', + ) +}) diff --git a/playground/json-stringify/__tests__/minify.spec.ts b/playground/json-stringify/__tests__/minify.spec.ts new file mode 100644 index 00000000000000..ea7294f4e16cdb --- /dev/null +++ b/playground/json-stringify/__tests__/minify.spec.ts @@ -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') +}) diff --git a/playground/json-stringify/dev.json b/playground/json-stringify/dev.json new file mode 100644 index 00000000000000..e22a75693673b1 --- /dev/null +++ b/playground/json-stringify/dev.json @@ -0,0 +1,3 @@ +{ + "dev": "DEV_ONLY_JSON" +} diff --git a/playground/json-stringify/hmr.json b/playground/json-stringify/hmr.json new file mode 100644 index 00000000000000..2dc497c5321ac4 --- /dev/null +++ b/playground/json-stringify/hmr.json @@ -0,0 +1,3 @@ +{ + "hmr": "this is hmr json" +} diff --git a/playground/json-stringify/index.html b/playground/json-stringify/index.html new file mode 100644 index 00000000000000..0b3f9d74373eda --- /dev/null +++ b/playground/json-stringify/index.html @@ -0,0 +1,75 @@ +

Normal Import

+

+

+
+

Deep Import

+

+

+
+

Dynamic Import

+

+

+
+

fetch

+

+
+

Importing as URL

+

+
+

Raw Import

+

+
+

JSON Module

+

+
+

Has BOM Tag

+

+
+

HMR

+

+
+
diff --git a/playground/json-stringify/json-bom/has-bom.json b/playground/json-stringify/json-bom/has-bom.json
new file mode 100644
index 00000000000000..8145b3da51e442
--- /dev/null
+++ b/playground/json-stringify/json-bom/has-bom.json
@@ -0,0 +1,4 @@
+{
+  "description": "This file is marked with BOM.",
+  "message": "If the parsing is successful, the BOM tag has been removed."
+}
diff --git a/playground/json-stringify/json-module/index.json b/playground/json-stringify/json-module/index.json
new file mode 100644
index 00000000000000..00c52e3dfc6fa3
--- /dev/null
+++ b/playground/json-stringify/json-module/index.json
@@ -0,0 +1,3 @@
+{
+  "hello": "hi"
+}
diff --git a/playground/json-stringify/json-module/package.json b/playground/json-stringify/json-module/package.json
new file mode 100644
index 00000000000000..a02c431845033f
--- /dev/null
+++ b/playground/json-stringify/json-module/package.json
@@ -0,0 +1,4 @@
+{
+  "name": "@vitejs/test-json-module",
+  "version": "0.0.0"
+}
diff --git a/playground/json-stringify/package.json b/playground/json-stringify/package.json
new file mode 100644
index 00000000000000..f60fdbea9b263d
--- /dev/null
+++ b/playground/json-stringify/package.json
@@ -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"
+  }
+}
diff --git a/playground/json-stringify/public/public.json b/playground/json-stringify/public/public.json
new file mode 100644
index 00000000000000..bacbe05617617e
--- /dev/null
+++ b/playground/json-stringify/public/public.json
@@ -0,0 +1,3 @@
+{
+  "hello": "this is json"
+}
diff --git a/playground/json-stringify/test.json b/playground/json-stringify/test.json
new file mode 100644
index 00000000000000..bacbe05617617e
--- /dev/null
+++ b/playground/json-stringify/test.json
@@ -0,0 +1,3 @@
+{
+  "hello": "this is json"
+}
diff --git a/playground/json-stringify/vite.config.js b/playground/json-stringify/vite.config.js
new file mode 100644
index 00000000000000..5bdf7d7a988e02
--- /dev/null
+++ b/playground/json-stringify/vite.config.js
@@ -0,0 +1,3 @@
+import { defineConfig } from 'vite'
+
+export default defineConfig({ json: { stringify: true } })
diff --git a/playground/json/__tests__/minify.spec.ts b/playground/json/__tests__/minify.spec.ts
new file mode 100644
index 00000000000000..ea7294f4e16cdb
--- /dev/null
+++ b/playground/json/__tests__/minify.spec.ts
@@ -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')
+})
diff --git a/playground/json/dev.json b/playground/json/dev.json
new file mode 100644
index 00000000000000..e22a75693673b1
--- /dev/null
+++ b/playground/json/dev.json
@@ -0,0 +1,3 @@
+{
+  "dev": "DEV_ONLY_JSON"
+}
diff --git a/playground/json/index.html b/playground/json/index.html
index ee076836f10b53..0b3f9d74373eda 100644
--- a/playground/json/index.html
+++ b/playground/json/index.html
@@ -31,6 +31,11 @@ 

HMR

diff --git a/playground/json-stringify/json-bom/has-bom.json b/playground/json-stringify/json-bom/has-bom.json deleted file mode 100644 index 8145b3da51e442..00000000000000 --- a/playground/json-stringify/json-bom/has-bom.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "description": "This file is marked with BOM.", - "message": "If the parsing is successful, the BOM tag has been removed." -} diff --git a/playground/json-stringify/json-module/index.json b/playground/json-stringify/json-module/index.json deleted file mode 100644 index 00c52e3dfc6fa3..00000000000000 --- a/playground/json-stringify/json-module/index.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "hello": "hi" -} diff --git a/playground/json-stringify/json-module/package.json b/playground/json-stringify/json-module/package.json deleted file mode 100644 index a02c431845033f..00000000000000 --- a/playground/json-stringify/json-module/package.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "name": "@vitejs/test-json-module", - "version": "0.0.0" -} diff --git a/playground/json-stringify/package.json b/playground/json-stringify/package.json deleted file mode 100644 index f60fdbea9b263d..00000000000000 --- a/playground/json-stringify/package.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "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" - } -} diff --git a/playground/json-stringify/public/public.json b/playground/json-stringify/public/public.json deleted file mode 100644 index bacbe05617617e..00000000000000 --- a/playground/json-stringify/public/public.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "hello": "this is json" -} diff --git a/playground/json-stringify/test.json b/playground/json-stringify/test.json deleted file mode 100644 index bacbe05617617e..00000000000000 --- a/playground/json-stringify/test.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "hello": "this is json" -} diff --git a/playground/json-stringify/vite.config.js b/playground/json-stringify/vite.config.js deleted file mode 100644 index 5bdf7d7a988e02..00000000000000 --- a/playground/json-stringify/vite.config.js +++ /dev/null @@ -1,3 +0,0 @@ -import { defineConfig } from 'vite' - -export default defineConfig({ json: { stringify: true } }) diff --git a/playground/json/__tests__/minify.spec.ts b/playground/json/__tests__/minify.spec.ts deleted file mode 100644 index ea7294f4e16cdb..00000000000000 --- a/playground/json/__tests__/minify.spec.ts +++ /dev/null @@ -1,14 +0,0 @@ -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') -}) diff --git a/playground/json/dev.json b/playground/json/dev.json deleted file mode 100644 index e22a75693673b1..00000000000000 --- a/playground/json/dev.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "dev": "DEV_ONLY_JSON" -} diff --git a/playground/json/index.html b/playground/json/index.html index 0b3f9d74373eda..ee076836f10b53 100644 --- a/playground/json/index.html +++ b/playground/json/index.html @@ -31,11 +31,6 @@

HMR