From 1edb0a23eae5aef84fe97e4f6ce66e4d71ba8e2c Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 11:24:51 +0900 Subject: [PATCH 01/16] fix: fix sourcemap when esbuild define transform includes "" source --- packages/vite/src/node/plugins/define.ts | 27 ++++++++++++++++++++++++ playground/define-wip/index.html | 1 + playground/define-wip/package.json | 13 ++++++++++++ playground/define-wip/src/repro.ts | 6 ++++++ playground/define-wip/vite.config.js | 8 +++++++ 5 files changed, 55 insertions(+) create mode 100644 playground/define-wip/index.html create mode 100644 playground/define-wip/package.json create mode 100644 playground/define-wip/src/repro.ts create mode 100644 playground/define-wip/vite.config.js diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index 3eaf2def0efde3..d165b481d74df8 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -1,4 +1,9 @@ import { transform } from 'esbuild' +import { + TraceMap, + decodedMap, + encodedMappings, +} from '@jridgewell/trace-mapping' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' import { escapeRegex, getHash } from '../utils' @@ -157,7 +162,29 @@ export async function replaceDefine( sourcemap: config.command === 'build' ? !!config.build.sourcemap : true, }) + // remove esbuild's source entries + // since they would confuse source map remapping which expects single source + if (result.map.includes('= 2) { + const sourceIndex = originalMap.sources.indexOf(id) + const decoded = decodedMap(originalMap) + decoded.mappings = decoded.mappings.map((segments) => + segments.filter((segment) => { + // modify and filter + const index = segment[1] + segment[1] = 0 + return index === sourceIndex + }), + ) + result.map = JSON.stringify({ + mappings: encodedMappings(new TraceMap(decoded)), + }) + } + } + for (const marker in replacementMarkers) { + // TODO: this could also break sourcemap? result.code = result.code.replaceAll(marker, replacementMarkers[marker]) } diff --git a/playground/define-wip/index.html b/playground/define-wip/index.html new file mode 100644 index 00000000000000..a139b6a76b4959 --- /dev/null +++ b/playground/define-wip/index.html @@ -0,0 +1 @@ + diff --git a/playground/define-wip/package.json b/playground/define-wip/package.json new file mode 100644 index 00000000000000..12425998e299a2 --- /dev/null +++ b/playground/define-wip/package.json @@ -0,0 +1,13 @@ +{ + "name": "@vitejs/test-define-wip", + "private": true, + "version": "0.0.0", + "type": "module", + "scripts": { + "repro": "pnpm build && pnpm preview", + "dev": "vite", + "build": "vite build", + "debug": "node --inspect-brk ../../packages/vite/bin/vite", + "preview": "vite preview" + } +} diff --git a/playground/define-wip/src/repro.ts b/playground/define-wip/src/repro.ts new file mode 100644 index 00000000000000..e2d8f134d3e5e4 --- /dev/null +++ b/playground/define-wip/src/repro.ts @@ -0,0 +1,6 @@ +function main() { + import.meta.env.VITE_NOT_EXIST + console.log('hello') // browser devtools should show "repro.ts:3" +} + +main() diff --git a/playground/define-wip/vite.config.js b/playground/define-wip/vite.config.js new file mode 100644 index 00000000000000..f55343d534bebe --- /dev/null +++ b/playground/define-wip/vite.config.js @@ -0,0 +1,8 @@ +import { defineConfig } from 'vite' + +export default defineConfig({ + build: { + minify: false, + sourcemap: true, + }, +}) From 309ab6b4c2e3c32bee22d29e7f710240b60fb6f4 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 11:53:09 +0900 Subject: [PATCH 02/16] chore: tweak test --- playground/define-wip/src/repro.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/playground/define-wip/src/repro.ts b/playground/define-wip/src/repro.ts index e2d8f134d3e5e4..c165e74410c0c3 100644 --- a/playground/define-wip/src/repro.ts +++ b/playground/define-wip/src/repro.ts @@ -1,6 +1,6 @@ function main() { - import.meta.env.VITE_NOT_EXIST - console.log('hello') // browser devtools should show "repro.ts:3" + // this will be replaced with `define_import_meta_env_default` object + console.log('[import.meta.env]', import.meta.env) // devtool should show "repro.ts:3" } main() From 0fcb9a5b8bfe64a3750d8ea9a0fb3dd0b2aacd70 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 11:54:07 +0900 Subject: [PATCH 03/16] chore: lockfile --- pnpm-lock.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a7150128fce65f..29cb66ac56c295 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -647,6 +647,8 @@ importers: specifier: file:./commonjs-dep version: file:playground/define/commonjs-dep + playground/define-wip: {} + playground/define/commonjs-dep: {} playground/dynamic-import: From 2a036604d349b2a87f2c4de33c4a932d0e2997a5 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 12:13:01 +0900 Subject: [PATCH 04/16] test: tweak --- packages/vite/src/node/plugins/define.ts | 2 +- playground/{define-wip => define-sourcemap}/index.html | 0 playground/{define-wip => define-sourcemap}/package.json | 2 +- playground/define-sourcemap/src/repro.ts | 9 +++++++++ .../{define-wip => define-sourcemap}/vite.config.js | 3 +++ playground/define-wip/src/repro.ts | 6 ------ pnpm-lock.yaml | 2 +- 7 files changed, 15 insertions(+), 9 deletions(-) rename playground/{define-wip => define-sourcemap}/index.html (100%) rename playground/{define-wip => define-sourcemap}/package.json (86%) create mode 100644 playground/define-sourcemap/src/repro.ts rename playground/{define-wip => define-sourcemap}/vite.config.js (67%) delete mode 100644 playground/define-wip/src/repro.ts diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index d165b481d74df8..e294a92607ec28 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -163,7 +163,7 @@ export async function replaceDefine( }) // remove esbuild's source entries - // since they would confuse source map remapping which expects single source + // since they would confuse source map remapping/collapsing which expects a single source if (result.map.includes('= 2) { diff --git a/playground/define-wip/index.html b/playground/define-sourcemap/index.html similarity index 100% rename from playground/define-wip/index.html rename to playground/define-sourcemap/index.html diff --git a/playground/define-wip/package.json b/playground/define-sourcemap/package.json similarity index 86% rename from playground/define-wip/package.json rename to playground/define-sourcemap/package.json index 12425998e299a2..707c5f2afb655c 100644 --- a/playground/define-wip/package.json +++ b/playground/define-sourcemap/package.json @@ -1,5 +1,5 @@ { - "name": "@vitejs/test-define-wip", + "name": "@vitejs/test-define-sourcemap", "private": true, "version": "0.0.0", "type": "module", diff --git a/playground/define-sourcemap/src/repro.ts b/playground/define-sourcemap/src/repro.ts new file mode 100644 index 00000000000000..5bdd5675920343 --- /dev/null +++ b/playground/define-sourcemap/src/repro.ts @@ -0,0 +1,9 @@ +declare let __defineObject: object + +function main() { + // this will be replaced with `define_defineObject_default` object + // devtool should show "repro.ts:6" + console.log('[__defineObject]', __defineObject) +} + +main() diff --git a/playground/define-wip/vite.config.js b/playground/define-sourcemap/vite.config.js similarity index 67% rename from playground/define-wip/vite.config.js rename to playground/define-sourcemap/vite.config.js index f55343d534bebe..451abb4e5059d3 100644 --- a/playground/define-wip/vite.config.js +++ b/playground/define-sourcemap/vite.config.js @@ -5,4 +5,7 @@ export default defineConfig({ minify: false, sourcemap: true, }, + define: { + __defineObject: '{ "hello": "test" }', + }, }) diff --git a/playground/define-wip/src/repro.ts b/playground/define-wip/src/repro.ts deleted file mode 100644 index c165e74410c0c3..00000000000000 --- a/playground/define-wip/src/repro.ts +++ /dev/null @@ -1,6 +0,0 @@ -function main() { - // this will be replaced with `define_import_meta_env_default` object - console.log('[import.meta.env]', import.meta.env) // devtool should show "repro.ts:3" -} - -main() diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 29cb66ac56c295..e1fee5033c1589 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -647,7 +647,7 @@ importers: specifier: file:./commonjs-dep version: file:playground/define/commonjs-dep - playground/define-wip: {} + playground/define-sourcemap: {} playground/define/commonjs-dep: {} From 97d24c5a002e1194f2ea6c8b4c2885357d8979c7 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 13:59:58 +0900 Subject: [PATCH 05/16] test: add to playground/js-sourcemap --- .../__tests__/js-sourcemap.spec.ts | 28 +++++++++++++++++++ playground/js-sourcemap/index.html | 1 + playground/js-sourcemap/vite.config.js | 6 ++++ playground/js-sourcemap/with-define-object.ts | 12 ++++++++ 4 files changed, 47 insertions(+) create mode 100644 playground/js-sourcemap/with-define-object.ts diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 7c22274b2a3a66..add6b4c4efe008 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -156,4 +156,32 @@ describe.runIf(isBuild)('build tests', () => { /^\/\/# sourceMappingURL=after-preload-dynamic.*\.js\.map$/, ) }) + + test('sourcemap is correct when using object as "define" value', async () => { + const map = findAssetFile(/with-define-object.*\.js\.map/) + expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(` + { + "mappings": "qBAGA,SAASA,GAAK,CACTC,GACL,CAEA,SAASA,GAAK,CACJ,QAAA,MAAM,qBAAsBC,CAAkB,CACxD,CAEAF,EAAG", + "sources": [ + "../../with-define-object.ts", + ], + "sourcesContent": [ + "declare let __testDefineObject: unknown + + // test complicated stack since broken sourcemap might still looks correct with a simple case + function f1() { + f2() + } + + function f2() { + console.trace('with-define-object', __testDefineObject) + } + + f1() + ", + ], + "version": 3, + } + `) + }) }) diff --git a/playground/js-sourcemap/index.html b/playground/js-sourcemap/index.html index 80ee729d99ce90..96a51deb15d4f9 100644 --- a/playground/js-sourcemap/index.html +++ b/playground/js-sourcemap/index.html @@ -8,3 +8,4 @@

JS Sourcemap

+ diff --git a/playground/js-sourcemap/vite.config.js b/playground/js-sourcemap/vite.config.js index 41484f2c99d0f3..fbda5449b7a4b0 100644 --- a/playground/js-sourcemap/vite.config.js +++ b/playground/js-sourcemap/vite.config.js @@ -15,8 +15,14 @@ export default defineConfig({ if (name.includes('after-preload-dynamic')) { return 'after-preload-dynamic' } + if (name.includes('with-define-object')) { + return 'with-define-object' + } }, }, }, }, + define: { + __testDefineObject: '{ "hello": "test" }', + }, }) diff --git a/playground/js-sourcemap/with-define-object.ts b/playground/js-sourcemap/with-define-object.ts new file mode 100644 index 00000000000000..2c27eec51ff58b --- /dev/null +++ b/playground/js-sourcemap/with-define-object.ts @@ -0,0 +1,12 @@ +declare let __testDefineObject: unknown + +// test complicated stack since broken sourcemap might still looks correct with a simple case +function f1() { + f2() +} + +function f2() { + console.trace('with-define-object', __testDefineObject) +} + +f1() From 0a7184986af2ab90ae36cd6fee211dfb5331aa86 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 14:16:25 +0900 Subject: [PATCH 06/16] chore: comment --- playground/js-sourcemap/with-define-object.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/playground/js-sourcemap/with-define-object.ts b/playground/js-sourcemap/with-define-object.ts index 2c27eec51ff58b..63380fce09ada7 100644 --- a/playground/js-sourcemap/with-define-object.ts +++ b/playground/js-sourcemap/with-define-object.ts @@ -1,6 +1,7 @@ declare let __testDefineObject: unknown -// test complicated stack since broken sourcemap might still looks correct with a simple case +// test complicated stack since broken sourcemap +// might still look correct with a simple case function f1() { f2() } From ba8b364dfc64213adcd9b193e310b710c1a238b5 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 14:22:23 +0900 Subject: [PATCH 07/16] chore: snapshot --- playground/js-sourcemap/__tests__/js-sourcemap.spec.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index add6b4c4efe008..87d43586a04828 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -161,14 +161,15 @@ describe.runIf(isBuild)('build tests', () => { const map = findAssetFile(/with-define-object.*\.js\.map/) expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(` { - "mappings": "qBAGA,SAASA,GAAK,CACTC,GACL,CAEA,SAASA,GAAK,CACJ,QAAA,MAAM,qBAAsBC,CAAkB,CACxD,CAEAF,EAAG", + "mappings": "qBAIA,SAASA,GAAK,CACTC,GACL,CAEA,SAASA,GAAK,CACJ,QAAA,MAAM,qBAAsBC,CAAkB,CACxD,CAEAF,EAAG", "sources": [ "../../with-define-object.ts", ], "sourcesContent": [ "declare let __testDefineObject: unknown - // test complicated stack since broken sourcemap might still looks correct with a simple case + // test complicated stack since broken sourcemap + // might still look correct with a simple case function f1() { f2() } From 187b2aecf26b86412b0c8005cd8bda467fb7ce37 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 14:34:38 +0900 Subject: [PATCH 08/16] fix: for ssr dev --- packages/vite/src/node/plugins/define.ts | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index e294a92607ec28..b1d7721f624484 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -1,9 +1,5 @@ import { transform } from 'esbuild' -import { - TraceMap, - decodedMap, - encodedMappings, -} from '@jridgewell/trace-mapping' +import { TraceMap, decodedMap, encodedMap } from '@jridgewell/trace-mapping' import type { ResolvedConfig } from '../config' import type { Plugin } from '../plugin' import { escapeRegex, getHash } from '../utils' @@ -169,6 +165,7 @@ export async function replaceDefine( if (originalMap.sources.length >= 2) { const sourceIndex = originalMap.sources.indexOf(id) const decoded = decodedMap(originalMap) + decoded.sources = [id] decoded.mappings = decoded.mappings.map((segments) => segments.filter((segment) => { // modify and filter @@ -177,9 +174,7 @@ export async function replaceDefine( return index === sourceIndex }), ) - result.map = JSON.stringify({ - mappings: encodedMappings(new TraceMap(decoded)), - }) + result.map = JSON.stringify(encodedMap(new TraceMap(decoded))) } } From 37275a821e9b383ccfbabe861ff865d35ba7d0e0 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 14:54:16 +0900 Subject: [PATCH 09/16] test: add ssr dev test --- playground/js-sourcemap/package.json | 3 +- playground/js-sourcemap/test-ssr-dev.js | 38 +++++++++++++++++++ .../js-sourcemap/with-define-object-ssr.ts | 8 ++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 playground/js-sourcemap/test-ssr-dev.js create mode 100644 playground/js-sourcemap/with-define-object-ssr.ts diff --git a/playground/js-sourcemap/package.json b/playground/js-sourcemap/package.json index 816f80c600f51c..24fd6a4698c06e 100644 --- a/playground/js-sourcemap/package.json +++ b/playground/js-sourcemap/package.json @@ -7,7 +7,8 @@ "dev": "vite", "build": "vite build", "debug": "node --inspect-brk ../../packages/vite/bin/vite", - "preview": "vite preview" + "preview": "vite preview", + "test-ssr-dev": "node test-ssr-dev.js" }, "dependencies": { "@vitejs/test-importee-pkg": "file:importee-pkg", diff --git a/playground/js-sourcemap/test-ssr-dev.js b/playground/js-sourcemap/test-ssr-dev.js new file mode 100644 index 00000000000000..c414f058517283 --- /dev/null +++ b/playground/js-sourcemap/test-ssr-dev.js @@ -0,0 +1,38 @@ +import assert from 'node:assert' +import { fileURLToPath } from 'node:url' +import { createServer } from 'vite' + +async function runTest() { + const server = await createServer({ + root: fileURLToPath(new URL('.', import.meta.url)), + configFile: false, + optimizeDeps: { + noDiscovery: true, + }, + server: { + middlewareMode: true, + hmr: false, + }, + define: { + __testDefineObject: '{ "hello": "test" }', + }, + }) + const mod = await server.ssrLoadModule('/with-define-object-ssr.ts') + const error = await getError(() => mod.error()) + server.ssrFixStacktrace(error) + assert.match(error.stack, /at errorInner (.*with-define-object-ssr.ts:7:9)/) + await server.close() +} + +async function getError(f) { + let error + try { + await f() + } catch (e) { + error = e + } + assert.ok(error) + return error +} + +runTest() diff --git a/playground/js-sourcemap/with-define-object-ssr.ts b/playground/js-sourcemap/with-define-object-ssr.ts new file mode 100644 index 00000000000000..c4436771bcb43c --- /dev/null +++ b/playground/js-sourcemap/with-define-object-ssr.ts @@ -0,0 +1,8 @@ +export function error() { + errorInner() +} + +function errorInner() { + // @ts-ignore + throw new Error('with-define-object: ' + JSON.stringify(__testDefineObject)) +} From 24e8079907096ebf477b23b4c5c2843b47c43790 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 14:55:19 +0900 Subject: [PATCH 10/16] chore: remove --- playground/define-sourcemap/index.html | 1 - playground/define-sourcemap/package.json | 13 ------------- playground/define-sourcemap/src/repro.ts | 9 --------- playground/define-sourcemap/vite.config.js | 11 ----------- 4 files changed, 34 deletions(-) delete mode 100644 playground/define-sourcemap/index.html delete mode 100644 playground/define-sourcemap/package.json delete mode 100644 playground/define-sourcemap/src/repro.ts delete mode 100644 playground/define-sourcemap/vite.config.js diff --git a/playground/define-sourcemap/index.html b/playground/define-sourcemap/index.html deleted file mode 100644 index a139b6a76b4959..00000000000000 --- a/playground/define-sourcemap/index.html +++ /dev/null @@ -1 +0,0 @@ - diff --git a/playground/define-sourcemap/package.json b/playground/define-sourcemap/package.json deleted file mode 100644 index 707c5f2afb655c..00000000000000 --- a/playground/define-sourcemap/package.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "name": "@vitejs/test-define-sourcemap", - "private": true, - "version": "0.0.0", - "type": "module", - "scripts": { - "repro": "pnpm build && pnpm preview", - "dev": "vite", - "build": "vite build", - "debug": "node --inspect-brk ../../packages/vite/bin/vite", - "preview": "vite preview" - } -} diff --git a/playground/define-sourcemap/src/repro.ts b/playground/define-sourcemap/src/repro.ts deleted file mode 100644 index 5bdd5675920343..00000000000000 --- a/playground/define-sourcemap/src/repro.ts +++ /dev/null @@ -1,9 +0,0 @@ -declare let __defineObject: object - -function main() { - // this will be replaced with `define_defineObject_default` object - // devtool should show "repro.ts:6" - console.log('[__defineObject]', __defineObject) -} - -main() diff --git a/playground/define-sourcemap/vite.config.js b/playground/define-sourcemap/vite.config.js deleted file mode 100644 index 451abb4e5059d3..00000000000000 --- a/playground/define-sourcemap/vite.config.js +++ /dev/null @@ -1,11 +0,0 @@ -import { defineConfig } from 'vite' - -export default defineConfig({ - build: { - minify: false, - sourcemap: true, - }, - define: { - __defineObject: '{ "hello": "test" }', - }, -}) From 068cd933216a75c263d088a154571f6488d98044 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 15:00:52 +0900 Subject: [PATCH 11/16] test: run test --- .../js-sourcemap/__tests__/js-sourcemap.spec.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 87d43586a04828..d6dbaa61bf36af 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -1,4 +1,6 @@ -import { URL } from 'node:url' +import { URL, fileURLToPath } from 'node:url' +import { promisify } from 'node:util' +import { execFile } from 'node:child_process' import { describe, expect, test } from 'vitest' import { mapFileCommentRegex } from 'convert-source-map' import { commentSourceMap } from '../foo-with-sourcemap-plugin' @@ -185,4 +187,11 @@ describe.runIf(isBuild)('build tests', () => { } `) }) + + test('correct sourcemap during ssr dev when using object as "define" value', async () => { + const execFileAsync = promisify(execFile) + await execFileAsync('node', ['test-ssr-dev.js'], { + cwd: fileURLToPath(new URL('..', import.meta.url)), + }) + }) }) From 9f2674452fc8e9273bfa699afb16153af482a05d Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 15:02:16 +0900 Subject: [PATCH 12/16] chore: remove unused --- playground/js-sourcemap/package.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/playground/js-sourcemap/package.json b/playground/js-sourcemap/package.json index 24fd6a4698c06e..816f80c600f51c 100644 --- a/playground/js-sourcemap/package.json +++ b/playground/js-sourcemap/package.json @@ -7,8 +7,7 @@ "dev": "vite", "build": "vite build", "debug": "node --inspect-brk ../../packages/vite/bin/vite", - "preview": "vite preview", - "test-ssr-dev": "node test-ssr-dev.js" + "preview": "vite preview" }, "dependencies": { "@vitejs/test-importee-pkg": "file:importee-pkg", From 1f823ea08b44e2831c159cf36db08fcb2baedbee Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 15:02:50 +0900 Subject: [PATCH 13/16] chore: lockfile --- pnpm-lock.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e1fee5033c1589..a7150128fce65f 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -647,8 +647,6 @@ importers: specifier: file:./commonjs-dep version: file:playground/define/commonjs-dep - playground/define-sourcemap: {} - playground/define/commonjs-dep: {} playground/dynamic-import: From e6e9b9a1103de877c2c4615f2b4c695714e6b341 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 15:04:54 +0900 Subject: [PATCH 14/16] chore: lint --- .../js-sourcemap/__tests__/js-sourcemap.spec.ts | 15 +++++++-------- playground/js-sourcemap/with-define-object-ssr.ts | 2 +- playground/js-sourcemap/with-define-object.ts | 11 +++++------ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index d6dbaa61bf36af..79efa6e8ab667a 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -163,24 +163,23 @@ describe.runIf(isBuild)('build tests', () => { const map = findAssetFile(/with-define-object.*\.js\.map/) expect(formatSourcemapForSnapshot(JSON.parse(map))).toMatchInlineSnapshot(` { - "mappings": "qBAIA,SAASA,GAAK,CACTC,GACL,CAEA,SAASA,GAAK,CACJ,QAAA,MAAM,qBAAsBC,CAAkB,CACxD,CAEAF,EAAG", + "mappings": "qBAEA,SAASA,GAAO,CACJC,GACZ,CAEA,SAASA,GAAY,CAEX,QAAA,MAAM,qBAAsBC,CAAkB,CACxD,CAEAF,EAAK", "sources": [ "../../with-define-object.ts", ], "sourcesContent": [ - "declare let __testDefineObject: unknown - - // test complicated stack since broken sourcemap + "// test complicated stack since broken sourcemap // might still look correct with a simple case - function f1() { - f2() + function main() { + mainInner() } - function f2() { + function mainInner() { + // @ts-expect-error "define" console.trace('with-define-object', __testDefineObject) } - f1() + main() ", ], "version": 3, diff --git a/playground/js-sourcemap/with-define-object-ssr.ts b/playground/js-sourcemap/with-define-object-ssr.ts index c4436771bcb43c..9ff85230025e2d 100644 --- a/playground/js-sourcemap/with-define-object-ssr.ts +++ b/playground/js-sourcemap/with-define-object-ssr.ts @@ -3,6 +3,6 @@ export function error() { } function errorInner() { - // @ts-ignore + // @ts-expect-error "define" throw new Error('with-define-object: ' + JSON.stringify(__testDefineObject)) } diff --git a/playground/js-sourcemap/with-define-object.ts b/playground/js-sourcemap/with-define-object.ts index 63380fce09ada7..5a9f8e2ddd43d9 100644 --- a/playground/js-sourcemap/with-define-object.ts +++ b/playground/js-sourcemap/with-define-object.ts @@ -1,13 +1,12 @@ -declare let __testDefineObject: unknown - // test complicated stack since broken sourcemap // might still look correct with a simple case -function f1() { - f2() +function main() { + mainInner() } -function f2() { +function mainInner() { + // @ts-expect-error "define" console.trace('with-define-object', __testDefineObject) } -f1() +main() From 8ed7c653a1c44b397c511d8f8ed862fd25b0a4d6 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Mon, 5 Feb 2024 17:02:31 +0900 Subject: [PATCH 15/16] chore: revert comment --- packages/vite/src/node/plugins/define.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index b1d7721f624484..ebcc385e130f6b 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -179,7 +179,6 @@ export async function replaceDefine( } for (const marker in replacementMarkers) { - // TODO: this could also break sourcemap? result.code = result.code.replaceAll(marker, replacementMarkers[marker]) } From 90554717c6057970ebe6df860cfb3c6ac930999c Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Wed, 3 Apr 2024 17:37:57 +0900 Subject: [PATCH 16/16] chore: type any? --- packages/vite/src/node/plugins/define.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/define.ts b/packages/vite/src/node/plugins/define.ts index ebcc385e130f6b..786a1038505c00 100644 --- a/packages/vite/src/node/plugins/define.ts +++ b/packages/vite/src/node/plugins/define.ts @@ -174,7 +174,7 @@ export async function replaceDefine( return index === sourceIndex }), ) - result.map = JSON.stringify(encodedMap(new TraceMap(decoded))) + result.map = JSON.stringify(encodedMap(new TraceMap(decoded as any))) } }