From a8c7eb24a2b7d8dc765d59758e2c6b930eb90af1 Mon Sep 17 00:00:00 2001 From: fi3ework Date: Tue, 9 May 2023 17:33:34 +0800 Subject: [PATCH] test: migrate playgrounds to ESM (#13094) --- .eslintrc.cjs | 15 ++++++++ playground/alias/package.json | 1 + playground/alias/vite.config.js | 3 ++ playground/assets-sanitize/package.json | 1 + playground/assets/package.json | 1 + playground/backend-integration/package.json | 1 + .../backend-integration/postcss.config.js | 9 +++-- .../backend-integration/tailwind.config.js | 4 ++- playground/build-old/package.json | 1 + playground/cli/package.json | 1 + playground/config/__tests__/load.spec.ts | 2 +- playground/config/packages/entry/package.json | 1 + .../plugin-module-condition/package.json | 1 + playground/config/packages/siblings/foo.ts | 4 +-- .../config/packages/siblings/package.json | 1 + playground/css-codesplit-cjs/package.json | 1 + playground/css-codesplit/package.json | 1 + playground/css-dynamic-import/package.json | 1 + playground/css-sourcemap/package.json | 1 + playground/css/package.json | 1 + .../css/postcss-caching/blue-app/package.json | 1 + .../blue-app/postcss.config.js | 2 +- .../postcss-caching/green-app/package.json | 1 + .../green-app/postcss.config.js | 2 +- playground/css/postcss.config.js | 15 ++++---- playground/data-uri/package.json | 1 + playground/define/package.json | 1 + playground/dynamic-import/package.json | 1 + playground/dynamic-import/vite.config.js | 4 +-- playground/env-nested/package.json | 1 + playground/env/package.json | 1 + playground/extensions/package.json | 1 + playground/external/package.json | 1 + playground/fs-serve/package.json | 1 + playground/glob-import/package.json | 1 + playground/hmr/package.json | 1 + playground/hmr/tsconfig.json | 16 --------- playground/html/package.json | 1 + playground/import-assertion/package.json | 1 + playground/js-sourcemap/package.json | 1 + .../json/__tests__/{ => csr}/json.spec.ts | 8 ++--- playground/json/__tests__/ssr/json.spec.ts | 20 +++++++++++ playground/json/__tests__/ssr/serve.ts | 35 +++++++++++++++++++ playground/json/package.json | 3 +- playground/json/server.js | 25 ++++++------- .../legacy/__tests__/client-and-ssr/serve.ts | 2 +- playground/legacy/__tests__/ssr/serve.ts | 2 +- playground/legacy/package.json | 1 + playground/lib/__tests__/lib.spec.ts | 12 ++++--- playground/lib/index.dist.html | 4 +-- playground/lib/package.json | 1 + playground/minify/package.json | 1 + playground/multiple-entrypoints/package.json | 1 + playground/nested-deps/package.json | 1 + playground/object-hooks/package.json | 1 + .../optimize-deps-no-discovery/package.json | 1 + playground/optimize-deps/package.json | 1 + playground/optimize-deps/vite.config.js | 2 ++ .../__test__/optimize-missing-deps.spec.ts | 4 +-- .../optimize-missing-deps/__test__/serve.ts | 2 +- playground/optimize-missing-deps/package.json | 1 + playground/optimize-missing-deps/server.js | 15 ++++---- playground/package.json | 1 + playground/preload/package.json | 1 + playground/preserve-symlinks/package.json | 1 + playground/resolve-config/package.json | 1 + playground/resolve-linked/package.json | 1 + .../resolve/{config-dep.js => config-dep.cjs} | 0 playground/resolve/package.json | 1 + playground/resolve/vite.config.js | 2 +- playground/ssr-deps/package.json | 2 +- playground/ssr-resolve/package.json | 1 + playground/tailwind-sourcemap/package.json | 1 + .../tailwind-sourcemap/postcss.config.js | 8 ++++- .../tailwind-sourcemap/tailwind.config.js | 4 ++- playground/tailwind/tailwind.config.js | 2 ++ playground/test-utils.ts | 1 + playground/transform-plugin/package.json | 1 + .../tsconfig-json-load-error/package.json | 1 + playground/tsconfig-json/package.json | 1 + playground/vitestSetup.ts | 2 +- playground/wasm/package.json | 1 + playground/worker/package.json | 1 + playground/worker/vite.config-es.js | 4 +-- playground/worker/vite.config-iife.js | 4 +-- .../worker/vite.config-relative-base-iife.js | 4 +-- .../worker/vite.config-relative-base.js | 4 +-- playground/worker/vite.config-sourcemap.js | 4 +-- .../worker/worker-plugin-test-plugin.js | 2 +- 89 files changed, 217 insertions(+), 87 deletions(-) delete mode 100644 playground/hmr/tsconfig.json rename playground/json/__tests__/{ => csr}/json.spec.ts (89%) create mode 100644 playground/json/__tests__/ssr/json.spec.ts create mode 100644 playground/json/__tests__/ssr/serve.ts rename playground/resolve/{config-dep.js => config-dep.cjs} (100%) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 3128e91c9df364..948041496208bb 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -181,6 +181,21 @@ module.exports = defineConfig({ '@typescript-eslint/no-empty-function': 'off', }, }, + { + files: ['playground/**'], + excludedFiles: [ + 'playground/ssr-resolve/**', + 'playground/**/*{commonjs,cjs}*/**', + 'playground/**/*{commonjs,cjs}*', + 'playground/**/*dep*/**', + 'playground/resolve/browser-module-field2/index.web.js', + 'playground/resolve/browser-field/**', + 'playground/tailwind/**', // blocked by https://github.com/postcss/postcss-load-config/issues/239 + ], + rules: { + 'import/no-commonjs': 'error', + }, + }, { files: [ 'playground/tsconfig-json/**', diff --git a/playground/alias/package.json b/playground/alias/package.json index 60c9a67218b86a..78995fa19e045c 100644 --- a/playground/alias/package.json +++ b/playground/alias/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-alias", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/alias/vite.config.js b/playground/alias/vite.config.js index c2b2e7e2e923e0..1312b295b985dd 100644 --- a/playground/alias/vite.config.js +++ b/playground/alias/vite.config.js @@ -1,6 +1,9 @@ import path from 'node:path' +import module from 'node:module' import { defineConfig } from 'vite' +const require = module.createRequire(import.meta.url) + export default defineConfig({ resolve: { alias: [ diff --git a/playground/assets-sanitize/package.json b/playground/assets-sanitize/package.json index 5deedbfaf97f38..1a110936d06db9 100644 --- a/playground/assets-sanitize/package.json +++ b/playground/assets-sanitize/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-assets-sanitize", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/assets/package.json b/playground/assets/package.json index 47e127261142e7..eeec51861fe068 100644 --- a/playground/assets/package.json +++ b/playground/assets/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-assets", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "debug": "node --inspect-brk ../../packages/vite/bin/vite", "dev": "vite", diff --git a/playground/backend-integration/package.json b/playground/backend-integration/package.json index d2d7ac79804c41..12b1aa1de18298 100644 --- a/playground/backend-integration/package.json +++ b/playground/backend-integration/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-backend-integration", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/backend-integration/postcss.config.js b/playground/backend-integration/postcss.config.js index f2f538422a839e..431c1ca465d257 100644 --- a/playground/backend-integration/postcss.config.js +++ b/playground/backend-integration/postcss.config.js @@ -1,5 +1,10 @@ -// postcss.config.js -module.exports = { +import { fileURLToPath } from 'node:url' +import { dirname } from 'node:path' + +const __filename = fileURLToPath(import.meta.url) +const __dirname = dirname(__filename) + +export default { plugins: { tailwindcss: { config: __dirname + '/tailwind.config.js' }, }, diff --git a/playground/backend-integration/tailwind.config.js b/playground/backend-integration/tailwind.config.js index a91db5247f6553..bf91cec6521e40 100644 --- a/playground/backend-integration/tailwind.config.js +++ b/playground/backend-integration/tailwind.config.js @@ -1,4 +1,6 @@ -module.exports = { +/** @type {import('tailwindcss').Config} */ + +export default { content: [__dirname + '/frontend/**/*.{css,html,ts,js}'], theme: { extend: {}, diff --git a/playground/build-old/package.json b/playground/build-old/package.json index 9766699a41d435..695d5e6f28fbc7 100644 --- a/playground/build-old/package.json +++ b/playground/build-old/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-build-old", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/cli/package.json b/playground/cli/package.json index 39b7efc3b24304..09b97d52574733 100644 --- a/playground/cli/package.json +++ b/playground/cli/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-cli", "private": true, "version": "0.0.0", + "type": "commonjs", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/config/__tests__/load.spec.ts b/playground/config/__tests__/load.spec.ts index 6236fff91b9272..a95cf592130f0b 100644 --- a/playground/config/__tests__/load.spec.ts +++ b/playground/config/__tests__/load.spec.ts @@ -20,7 +20,7 @@ it('loadConfigFromFile', async () => { 4, ], ], - "moduleCondition": "require condition", + "moduleCondition": "import condition", } `) }) diff --git a/playground/config/packages/entry/package.json b/playground/config/packages/entry/package.json index 287aa6c698618b..bb0e07d0af7389 100644 --- a/playground/config/packages/entry/package.json +++ b/playground/config/packages/entry/package.json @@ -1,5 +1,6 @@ { "name": "@vite/test-config-entry", + "type": "module", "dependencies": { "@vite/test-config-plugin-module-condition": "link:../plugin-module-condition" } diff --git a/playground/config/packages/plugin-module-condition/package.json b/playground/config/packages/plugin-module-condition/package.json index 38df99751d9e3e..623a54914737cf 100644 --- a/playground/config/packages/plugin-module-condition/package.json +++ b/playground/config/packages/plugin-module-condition/package.json @@ -1,5 +1,6 @@ { "name": "@vite/test-config-plugin-module-condition", + "type": "module", "exports": { ".": { "types": "./index.d.ts", diff --git a/playground/config/packages/siblings/foo.ts b/playground/config/packages/siblings/foo.ts index 78a8912131faed..efc3b112053c58 100644 --- a/playground/config/packages/siblings/foo.ts +++ b/playground/config/packages/siblings/foo.ts @@ -1,3 +1,3 @@ -import { partition } from 'lodash' +import lodash from 'lodash' -export const array = partition([1, 2, 3, 4], (n) => n % 2) +export const array = lodash.partition([1, 2, 3, 4], (n) => n % 2) diff --git a/playground/config/packages/siblings/package.json b/playground/config/packages/siblings/package.json index 1d7775558a79ac..b4bcc544931887 100644 --- a/playground/config/packages/siblings/package.json +++ b/playground/config/packages/siblings/package.json @@ -1,5 +1,6 @@ { "name": "@vite/test-config-sibling", + "type": "module", "devDependencies": { "@types/lodash": "^4.14.194", "lodash": "^4.17.21" diff --git a/playground/css-codesplit-cjs/package.json b/playground/css-codesplit-cjs/package.json index 97f1473d267e46..e305007adfcb98 100644 --- a/playground/css-codesplit-cjs/package.json +++ b/playground/css-codesplit-cjs/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-css-codesplit-cjs", "private": true, "version": "0.0.0", + "type": "commonjs", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/css-codesplit/package.json b/playground/css-codesplit/package.json index ac3149030dceaf..f7edd868783ebf 100644 --- a/playground/css-codesplit/package.json +++ b/playground/css-codesplit/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-css-codesplit", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/css-dynamic-import/package.json b/playground/css-dynamic-import/package.json index 60b8fa380e2243..2b5339f8c72760 100644 --- a/playground/css-dynamic-import/package.json +++ b/playground/css-dynamic-import/package.json @@ -1,5 +1,6 @@ { "name": "@vitejs/test-css-dynamic-import", "private": true, + "type": "module", "version": "0.0.0" } diff --git a/playground/css-sourcemap/package.json b/playground/css-sourcemap/package.json index 0bb168faa6360f..d1ff56dafa781f 100644 --- a/playground/css-sourcemap/package.json +++ b/playground/css-sourcemap/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-css-sourcemap", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/css/package.json b/playground/css/package.json index 43e4f87e12db72..d95ef47367e343 100644 --- a/playground/css/package.json +++ b/playground/css/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-css", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/css/postcss-caching/blue-app/package.json b/playground/css/postcss-caching/blue-app/package.json index 82ba64fadd44b4..528263c4e60923 100644 --- a/playground/css/postcss-caching/blue-app/package.json +++ b/playground/css/postcss-caching/blue-app/package.json @@ -2,6 +2,7 @@ "name": "blue-app", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/css/postcss-caching/blue-app/postcss.config.js b/playground/css/postcss-caching/blue-app/postcss.config.js index 50764f3ff8f21a..679b801013ef1a 100644 --- a/playground/css/postcss-caching/blue-app/postcss.config.js +++ b/playground/css/postcss-caching/blue-app/postcss.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { plugins: [replacePinkWithBlue], } diff --git a/playground/css/postcss-caching/green-app/package.json b/playground/css/postcss-caching/green-app/package.json index 72bd0f45b8865f..110ea74558435d 100644 --- a/playground/css/postcss-caching/green-app/package.json +++ b/playground/css/postcss-caching/green-app/package.json @@ -2,6 +2,7 @@ "name": "green-app", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/css/postcss-caching/green-app/postcss.config.js b/playground/css/postcss-caching/green-app/postcss.config.js index 7d9d8818540e09..c0a74e3676976d 100644 --- a/playground/css/postcss-caching/green-app/postcss.config.js +++ b/playground/css/postcss-caching/green-app/postcss.config.js @@ -1,4 +1,4 @@ -module.exports = { +export default { plugins: [replacePinkWithGreen], } diff --git a/playground/css/postcss.config.js b/playground/css/postcss.config.js index 955878ac216f53..4d2c144b9fa66c 100644 --- a/playground/css/postcss.config.js +++ b/playground/css/postcss.config.js @@ -1,11 +1,12 @@ -module.exports = { - plugins: [require('postcss-nested'), testDirDep, testSourceInput], -} +import fs from 'node:fs' +import path from 'node:path' +import glob from 'fast-glob' +import { normalizePath } from 'vite' +import postcssNested from 'postcss-nested' -const fs = require('node:fs') -const path = require('node:path') -const glob = require('fast-glob') -const { normalizePath } = require('vite') +export default { + plugins: [postcssNested, testDirDep, testSourceInput], +} /** * A plugin for testing the `dir-dependency` message handling. diff --git a/playground/data-uri/package.json b/playground/data-uri/package.json index c19befed0bca09..4e8b2d699f2ac8 100644 --- a/playground/data-uri/package.json +++ b/playground/data-uri/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-data-uri", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/define/package.json b/playground/define/package.json index 9a144c76a4dfb0..a65b36c1c3df67 100644 --- a/playground/define/package.json +++ b/playground/define/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-define", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/dynamic-import/package.json b/playground/dynamic-import/package.json index 55137ef5fb9375..d3ab6846463268 100644 --- a/playground/dynamic-import/package.json +++ b/playground/dynamic-import/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-dynamic-import", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/dynamic-import/vite.config.js b/playground/dynamic-import/vite.config.js index 15aa703d74530a..dc8ecbd8bbdfe5 100644 --- a/playground/dynamic-import/vite.config.js +++ b/playground/dynamic-import/vite.config.js @@ -1,8 +1,8 @@ import fs from 'node:fs' import path from 'node:path' -import vite from 'vite' +import { defineConfig } from 'vite' -export default vite.defineConfig({ +export default defineConfig({ plugins: [ { name: 'copy', diff --git a/playground/env-nested/package.json b/playground/env-nested/package.json index 545eefb2f690d5..060888e998c98d 100644 --- a/playground/env-nested/package.json +++ b/playground/env-nested/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-env-nested", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/env/package.json b/playground/env/package.json index 969f4061114a9d..a50e47e1c17313 100644 --- a/playground/env/package.json +++ b/playground/env/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-env", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "VITE_INLINE=inline-serve vite", "build": "VITE_INLINE=inline-build vite build", diff --git a/playground/extensions/package.json b/playground/extensions/package.json index 30dde24815a6ce..b11cc4a213e352 100644 --- a/playground/extensions/package.json +++ b/playground/extensions/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-extensions", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/external/package.json b/playground/external/package.json index c27e446e8fbdee..6f5141bb15ae9f 100644 --- a/playground/external/package.json +++ b/playground/external/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-external", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/fs-serve/package.json b/playground/fs-serve/package.json index 15fdfe3294cd83..ceb4552d1c244f 100644 --- a/playground/fs-serve/package.json +++ b/playground/fs-serve/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-fs-serve", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite root", "build": "vite build root", diff --git a/playground/glob-import/package.json b/playground/glob-import/package.json index f3ac7ae9313db5..66d4667336a1a5 100644 --- a/playground/glob-import/package.json +++ b/playground/glob-import/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-import-context", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/hmr/package.json b/playground/hmr/package.json index 8bb1d44d18bbe0..35e0799c262738 100644 --- a/playground/hmr/package.json +++ b/playground/hmr/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-hmr", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/hmr/tsconfig.json b/playground/hmr/tsconfig.json deleted file mode 100644 index ca98b7ce20a3ca..00000000000000 --- a/playground/hmr/tsconfig.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "extends": "../tsconfig.json", - "include": ["."], - "exclude": ["**/dist/**", "**/__tests__/**"], - "compilerOptions": { - "target": "ES2020", - "module": "ESNext", - "outDir": "dist", - "allowJs": true, - "esModuleInterop": true, - "moduleResolution": "bundler", - "baseUrl": ".", - "jsx": "preserve", - "types": ["vite/client", "node"] - } -} diff --git a/playground/html/package.json b/playground/html/package.json index 9eadef9f1a71a8..6bdb23d06194a4 100644 --- a/playground/html/package.json +++ b/playground/html/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-html", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/import-assertion/package.json b/playground/import-assertion/package.json index 03248a46ef4dd3..04ecce34c1f5ec 100644 --- a/playground/import-assertion/package.json +++ b/playground/import-assertion/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-import-assertion", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/js-sourcemap/package.json b/playground/js-sourcemap/package.json index 4b82881a37e473..6381b13c9b09e9 100644 --- a/playground/js-sourcemap/package.json +++ b/playground/js-sourcemap/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-js-sourcemap", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", diff --git a/playground/json/__tests__/json.spec.ts b/playground/json/__tests__/csr/json.spec.ts similarity index 89% rename from playground/json/__tests__/json.spec.ts rename to playground/json/__tests__/csr/json.spec.ts index 5837a236eee314..cec1fb5f0eb0ba 100644 --- a/playground/json/__tests__/json.spec.ts +++ b/playground/json/__tests__/csr/json.spec.ts @@ -1,10 +1,10 @@ import { readFileSync } from 'node:fs' import { expect, test } from 'vitest' -import testJson from '../test.json' -import hmrJson from '../hmr.json' +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 deepJson = require('vue/package.json') const stringified = JSON.stringify(testJson) const deepStringified = JSON.stringify(deepJson) const hmrStringified = JSON.stringify(hmrJson) @@ -45,7 +45,7 @@ test('?url', async () => { test('?raw', async () => { expect(await page.textContent('.raw')).toBe( - readFileSync(require.resolve('../test.json'), 'utf-8'), + readFileSync(require.resolve('../../test.json'), 'utf-8'), ) }) diff --git a/playground/json/__tests__/ssr/json.spec.ts b/playground/json/__tests__/ssr/json.spec.ts new file mode 100644 index 00000000000000..5efbeac7da12d6 --- /dev/null +++ b/playground/json/__tests__/ssr/json.spec.ts @@ -0,0 +1,20 @@ +import { beforeEach, test } from 'vitest' +import { port } from './serve' +import { page, untilUpdated } from '~utils' + +const url = `http://localhost:${port}` + +beforeEach(async () => { + await page.goto(url) +}) + +test('load json module', async () => { + await untilUpdated( + () => page.textContent('.fetch-json-module pre'), + 'export default JSON.parse("{\\n \\"hello\\": \\"hi\\"\\n}\\n")', + ) +}) + +test('fs json', async () => { + await untilUpdated(() => page.textContent('.fetch-json-fs pre'), '61') +}) diff --git a/playground/json/__tests__/ssr/serve.ts b/playground/json/__tests__/ssr/serve.ts new file mode 100644 index 00000000000000..369d3b3999df28 --- /dev/null +++ b/playground/json/__tests__/ssr/serve.ts @@ -0,0 +1,35 @@ +// this is automatically detected by playground/vitestSetup.ts and will replace +// the default e2e test serve behavior + +import path from 'node:path' +import kill from 'kill-port' +import { ports, rootDir } from '~utils' + +export const port = ports.json + +export async function serve(): Promise<{ close(): Promise }> { + await kill(port) + + const { createServer } = await import(path.resolve(rootDir, 'server.js')) + const { app, vite } = await createServer(rootDir) + + return new Promise((resolve, reject) => { + try { + const server = app.listen(port, () => { + resolve({ + // for test teardown + async close() { + await new Promise((resolve) => { + server.close(resolve) + }) + if (vite) { + await vite.close() + } + }, + }) + }) + } catch (e) { + reject(e) + } + }) +} diff --git a/playground/json/package.json b/playground/json/package.json index 748e23fda44137..1484ada27924e5 100644 --- a/playground/json/package.json +++ b/playground/json/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-json", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build", @@ -12,8 +13,8 @@ "debug:ssr": "node --inspect-brk server" }, "devDependencies": { - "express": "^4.18.2", "@vitejs/test-json-module": "file:./json-module", + "express": "^4.18.2", "vue": "^3.2.47" } } diff --git a/playground/json/server.js b/playground/json/server.js index 5a7a989d9c39e3..705fd0efa0aabb 100644 --- a/playground/json/server.js +++ b/playground/json/server.js @@ -1,21 +1,22 @@ // @ts-check -const fs = require('node:fs') -const path = require('node:path') -const express = require('express') +import fs from 'node:fs' +import path from 'node:path' +import { fileURLToPath } from 'node:url' +import express from 'express' +const __dirname = path.dirname(fileURLToPath(import.meta.url)) const isTest = process.env.VITEST -async function createServer( - root = process.cwd(), - isProd = process.env.NODE_ENV === 'production', -) { +export async function createServer(root = process.cwd()) { const resolve = (p) => path.resolve(__dirname, p) const app = express() /** * @type {import('vite').ViteDevServer} */ - const vite = await require('vite').createServer({ + const vite = await ( + await import('vite') + ).createServer({ root, logLevel: isTest ? 'error' : 'info', server: { @@ -50,13 +51,16 @@ async function createServer( if (url === '/json-fs') { console.time('transform module') - const source = fs.readFileSync('./test.json', { encoding: 'utf-8' }) + const source = fs.readFileSync(path.resolve(__dirname, './test.json'), { + encoding: 'utf-8', + }) const json = await vite.ssrTransform( `export default ${source}`, null, './output.json', ) console.timeEnd('transform module') + // @ts-expect-error ignore in test res.status(200).end(String(json.code.length)) return } @@ -83,6 +87,3 @@ if (!isTest) { }), ) } - -// for test use -exports.createServer = createServer diff --git a/playground/legacy/__tests__/client-and-ssr/serve.ts b/playground/legacy/__tests__/client-and-ssr/serve.ts index 3dc22f7adc0f45..46a075549db53a 100644 --- a/playground/legacy/__tests__/client-and-ssr/serve.ts +++ b/playground/legacy/__tests__/client-and-ssr/serve.ts @@ -34,7 +34,7 @@ export async function serve(): Promise<{ close(): Promise }> { app.use('/', async (_req, res) => { const { render } = await import( - path.resolve(rootDir, './dist/server/entry-server-sequential.mjs') + path.resolve(rootDir, './dist/server/entry-server-sequential.js') ) const html = await render() res.status(200).set({ 'Content-Type': 'text/html' }).end(html) diff --git a/playground/legacy/__tests__/ssr/serve.ts b/playground/legacy/__tests__/ssr/serve.ts index 69ff2ec940b825..2b20dfc8455d69 100644 --- a/playground/legacy/__tests__/ssr/serve.ts +++ b/playground/legacy/__tests__/ssr/serve.ts @@ -22,7 +22,7 @@ export async function serve(): Promise<{ close(): Promise }> { app.use('/', async (_req, res) => { const { render } = await import( - path.resolve(rootDir, './dist/server/entry-server.mjs') + path.resolve(rootDir, './dist/server/entry-server.js') ) const html = await render() res.status(200).set({ 'Content-Type': 'text/html' }).end(html) diff --git a/playground/legacy/package.json b/playground/legacy/package.json index b47df1430d6cdc..897d9c94ef819d 100644 --- a/playground/legacy/package.json +++ b/playground/legacy/package.json @@ -2,6 +2,7 @@ "name": "@vitejs/test-legacy", "private": true, "version": "0.0.0", + "type": "module", "scripts": { "dev": "vite", "build": "vite build --debug legacy", diff --git a/playground/lib/__tests__/lib.spec.ts b/playground/lib/__tests__/lib.spec.ts index ce309a2285fe98..d4ced107fdea7a 100644 --- a/playground/lib/__tests__/lib.spec.ts +++ b/playground/lib/__tests__/lib.spec.ts @@ -15,8 +15,10 @@ describe.runIf(isBuild)('build', () => { test('umd', async () => { expect(await page.textContent('.umd')).toBe('It works') - const code = readFile('dist/my-lib-custom-filename.umd.js') - const noMinifyCode = readFile('dist/nominify/my-lib-custom-filename.umd.js') + const code = readFile('dist/my-lib-custom-filename.umd.cjs') + const noMinifyCode = readFile( + 'dist/nominify/my-lib-custom-filename.umd.cjs', + ) // esbuild helpers are injected inside of the UMD wrapper expect(code).toMatch(/^\(function\(/) expect(noMinifyCode).toMatch( @@ -46,7 +48,7 @@ describe.runIf(isBuild)('build', () => { expect(code).not.toMatch('__vitePreload') // Test that library chunks are hashed - expect(code).toMatch(/await import\("\.\/message-[a-z\d]{8}.mjs"\)/) + expect(code).toMatch(/await import\("\.\/message-[a-z\d]{8}.js"\)/) }) test('@import hoist', async () => { @@ -57,9 +59,9 @@ describe.runIf(isBuild)('build', () => { }) test('preserve process.env', () => { - const es = readFile('dist/my-lib-custom-filename.mjs') + const es = readFile('dist/my-lib-custom-filename.js') const iife = readFile('dist/my-lib-custom-filename.iife.js') - const umd = readFile('dist/my-lib-custom-filename.umd.js') + const umd = readFile('dist/my-lib-custom-filename.umd.cjs') expect(es).toMatch('process.env.NODE_ENV') expect(iife).toMatch('process.env.NODE_ENV') expect(umd).toMatch('process.env.NODE_ENV') diff --git a/playground/lib/index.dist.html b/playground/lib/index.dist.html index b719268ded6365..e7fb284811876a 100644 --- a/playground/lib/index.dist.html +++ b/playground/lib/index.dist.html @@ -14,7 +14,7 @@ @@ -25,7 +25,7 @@ message('.dynamic-import-message') - +