From 5089735519b6925a2e0c71a1d3c75cc3dfde3325 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 12 Oct 2023 09:18:13 +0800 Subject: [PATCH 01/13] fix(node): sourcemap compatible rollup plugin --- packages/vite/src/node/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 843d06ed5c6159..770efcd272fbb9 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1147,7 +1147,7 @@ export function transformStableResult( map: config.command === 'build' && config.build.sourcemap ? s.generateMap({ hires: 'boundary', source: id }) - : null, + : { mappings: '' }, } } From 77da6f5dda0b40d35f9d9172eb6b29c3e1b2acf6 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 12 Oct 2023 15:13:28 +0800 Subject: [PATCH 02/13] =?UTF-8?q?fix(sourcemap):=20sourcemap=20is=20incorr?= =?UTF-8?q?ect=20when=20sourcemap=20doesn=E2=80=99t=20include=20sources?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/vite/src/node/server/pluginContainer.ts | 11 ++++++++++- packages/vite/src/node/utils.ts | 2 +- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 76985d93b0cf5d..83fae2d2a35fd4 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -578,7 +578,16 @@ export async function createPluginContainer( break } if (!combinedMap) { - combinedMap = m as SourceMap + const sm = m as SourceMap + if (sm.sources.length === 1 && !sm.sources[0]) { + combinedMap = { + ...sm, + sources: [this.filename], + sourcesContent: [this.originalCode], + } + } else { + combinedMap = sm + } } else { combinedMap = combineSourcemaps(cleanUrl(this.filename), [ m as RawSourceMap, diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 770efcd272fbb9..843d06ed5c6159 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -1147,7 +1147,7 @@ export function transformStableResult( map: config.command === 'build' && config.build.sourcemap ? s.generateMap({ hires: 'boundary', source: id }) - : { mappings: '' }, + : null, } } From a321ca2c50241afdad8dfa284c1edb22b90a76de Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 12 Oct 2023 07:48:02 +0000 Subject: [PATCH 03/13] test: update snap --- playground/js-sourcemap/__tests__/js-sourcemap.spec.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index eac7b2a524a9e7..3bda8106a92050 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -44,10 +44,15 @@ if (!isBuild) { { "mappings": "AAAA,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG", "sources": [ - "", + "foo-with-sourcemap.js", ], "sourcesContent": [ - null, + "export const foo = 'foo' + + // default boundary sourcemap with magic-string + + + ", ], "version": 3, } From 10e1d372da7af78411fefb0779cae249a9ab4c71 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 12 Oct 2023 08:15:26 +0000 Subject: [PATCH 04/13] test: add plugin-sourcemap --- .../__tests__/plugin-sourcemap.spec.ts | 30 +++++++++++++++++++ playground/plugin-sourcemap/foo.js | 1 + playground/plugin-sourcemap/index.html | 5 ++++ playground/plugin-sourcemap/package.json | 15 ++++++++++ playground/plugin-sourcemap/vite.config.js | 23 ++++++++++++++ pnpm-lock.yaml | 7 +++++ 6 files changed, 81 insertions(+) create mode 100644 playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts create mode 100644 playground/plugin-sourcemap/foo.js create mode 100644 playground/plugin-sourcemap/index.html create mode 100644 playground/plugin-sourcemap/package.json create mode 100644 playground/plugin-sourcemap/vite.config.js diff --git a/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts b/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts new file mode 100644 index 00000000000000..5639fdaaf1c762 --- /dev/null +++ b/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts @@ -0,0 +1,30 @@ +import { URL } from 'node:url' +import { expect, test } from 'vitest' +import { + extractSourcemap, + formatSourcemapForSnapshot, + isBuild, + page, +} from '~utils' + +if (!isBuild) { + test('correct sourcemap', async () => { + const res = await page.request.get(new URL('./foo.js', page.url()).href) + const js = await res.text() + expect(js).toContain('// add comment') + const map = extractSourcemap(js) + expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` + { + "mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;", + "sources": [ + "foo.js", + ], + "sourcesContent": [ + "export const foo = 'foo' + ", + ], + "version": 3, + } + `) + }) +} diff --git a/playground/plugin-sourcemap/foo.js b/playground/plugin-sourcemap/foo.js new file mode 100644 index 00000000000000..cb356468240d50 --- /dev/null +++ b/playground/plugin-sourcemap/foo.js @@ -0,0 +1 @@ +export const foo = 'foo' diff --git a/playground/plugin-sourcemap/index.html b/playground/plugin-sourcemap/index.html new file mode 100644 index 00000000000000..034e77d10968c0 --- /dev/null +++ b/playground/plugin-sourcemap/index.html @@ -0,0 +1,5 @@ +
+

Plugin Sourcemap

+
+ + diff --git a/playground/plugin-sourcemap/package.json b/playground/plugin-sourcemap/package.json new file mode 100644 index 00000000000000..4acbfd56a7cba2 --- /dev/null +++ b/playground/plugin-sourcemap/package.json @@ -0,0 +1,15 @@ +{ + "name": "@vitejs/test-plugin-sourcemap", + "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": { + "magic-string": "^0.30.3" + } +} diff --git a/playground/plugin-sourcemap/vite.config.js b/playground/plugin-sourcemap/vite.config.js new file mode 100644 index 00000000000000..611fa53ffcbcf2 --- /dev/null +++ b/playground/plugin-sourcemap/vite.config.js @@ -0,0 +1,23 @@ +import { defineConfig } from 'vite' +import MagicString from 'magic-string' + +export default defineConfig({ + plugins: [ + { + name: 'sourcemap', + transform(code, id) { + if (id.includes('foo.js')) { + const ms = new MagicString(code) + ms.append('// add comment') + return { + code: ms.toString(), + map: ms.generateMap({ hires: true }), + } + } + }, + }, + ], + build: { + sourcemap: true, + }, +}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 20255fcd8773d1..dd8bb996dc6d94 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -998,6 +998,12 @@ importers: playground/optimize-missing-deps/multi-entry-dep: {} + playground/plugin-sourcemap: + devDependencies: + magic-string: + specifier: ^0.30.3 + version: 0.30.3 + playground/preload: devDependencies: '@vitejs/test-dep-a': @@ -1925,6 +1931,7 @@ packages: /@babel/highlight@7.22.13: resolution: {integrity: sha512-C/BaXcnnvBCmHTpz/VGZ8jgtE2aYlW4hxDhseJAWZb7gqGM/qtCK6iZUb0TyKFf7BOUsBH7Q7fkRsDRhg1XklQ==} engines: {node: '>=6.9.0'} + requiresBuild: true dependencies: '@babel/helper-validator-identifier': 7.22.15 chalk: 2.4.2 From 3bbb423fe222c90565ae76ad765c1e8640c96acc Mon Sep 17 00:00:00 2001 From: Dunqing Date: Thu, 12 Oct 2023 16:28:40 +0800 Subject: [PATCH 05/13] fix: test --- .../__tests__/plugin-sourcemap.spec.ts | 40 +++++++++---------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts b/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts index 5639fdaaf1c762..f462760920222c 100644 --- a/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts +++ b/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts @@ -7,24 +7,22 @@ import { page, } from '~utils' -if (!isBuild) { - test('correct sourcemap', async () => { - const res = await page.request.get(new URL('./foo.js', page.url()).href) - const js = await res.text() - expect(js).toContain('// add comment') - const map = extractSourcemap(js) - expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` - { - "mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;", - "sources": [ - "foo.js", - ], - "sourcesContent": [ - "export const foo = 'foo' - ", - ], - "version": 3, - } - `) - }) -} +test.runIf(!isBuild)('correct sourcemap', async () => { + const res = await page.request.get(new URL('./foo.js', page.url()).href) + const js = await res.text() + expect(js).toContain('// add comment') + const map = extractSourcemap(js) + expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` + { + "mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;", + "sources": [ + "foo.js", + ], + "sourcesContent": [ + "export const foo = 'foo' + ", + ], + "version": 3, + } + `) +}) From bf1032165ef8bbe95d7923f5131398e16f3a7b81 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Sun, 15 Oct 2023 09:49:56 +0800 Subject: [PATCH 06/13] test: update test case --- playground/js-sourcemap/__tests__/js-sourcemap.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 9d22161db304b7..b4e63e66290a74 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -46,7 +46,7 @@ if (!isBuild) { { "mappings": "AAAA,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG", "sources": [ - "foo-with-sourcemap.js", + "", ], "version": 3, } From 71696647ecb1b08d607e34bb7fd75b6b1e3589d8 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 21 Nov 2023 16:42:32 +0800 Subject: [PATCH 07/13] Update playground/plugin-sourcemap/vite.config.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- playground/plugin-sourcemap/vite.config.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/playground/plugin-sourcemap/vite.config.js b/playground/plugin-sourcemap/vite.config.js index 611fa53ffcbcf2..239fc72ab68a6e 100644 --- a/playground/plugin-sourcemap/vite.config.js +++ b/playground/plugin-sourcemap/vite.config.js @@ -11,6 +11,8 @@ export default defineConfig({ ms.append('// add comment') return { code: ms.toString(), + // NOTE: MagicString without `filename` option generates + // a sourcemap with `sources: ['']` or `sources: [null]` map: ms.generateMap({ hires: true }), } } From 04c436e9d9eacde0660a53b2d3c9274ae0daf178 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 21 Nov 2023 16:42:53 +0800 Subject: [PATCH 08/13] Update packages/vite/src/node/server/pluginContainer.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 翠 / green --- packages/vite/src/node/server/pluginContainer.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/vite/src/node/server/pluginContainer.ts b/packages/vite/src/node/server/pluginContainer.ts index 83fae2d2a35fd4..2ae0aa61e35160 100644 --- a/packages/vite/src/node/server/pluginContainer.ts +++ b/packages/vite/src/node/server/pluginContainer.ts @@ -579,6 +579,10 @@ export async function createPluginContainer( } if (!combinedMap) { const sm = m as SourceMap + // sourcemap should not include `sources: [null]` (because `sources` should be string) nor + // `sources: ['']` (because `''` means the path of sourcemap) + // but MagicString generates this when `filename` option is not set. + // Rollup supports these and therefore we support this as well if (sm.sources.length === 1 && !sm.sources[0]) { combinedMap = { ...sm, From a8563908956e08adcdc46b7ca77c675019388b5b Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 21 Nov 2023 17:10:31 +0800 Subject: [PATCH 09/13] test: move test to js-sourcemap --- .../__tests__/js-sourcemap.spec.ts | 21 ++++++++++++++ playground/js-sourcemap/index.html | 2 +- playground/js-sourcemap/package.json | 3 +- .../foo.js => js-sourcemap/plugin-foo.js} | 0 playground/js-sourcemap/vite.config.js | 6 +++- .../js-sourcemap/zoo-with-sourcemap-plugin.ts | 17 +++++++++++ playground/js-sourcemap/zoo.js | 1 + .../__tests__/plugin-sourcemap.spec.ts | 28 ------------------- playground/plugin-sourcemap/index.html | 5 ---- playground/plugin-sourcemap/package.json | 15 ---------- playground/plugin-sourcemap/vite.config.js | 25 ----------------- pnpm-lock.yaml | 9 ++---- 12 files changed, 50 insertions(+), 82 deletions(-) rename playground/{plugin-sourcemap/foo.js => js-sourcemap/plugin-foo.js} (100%) create mode 100644 playground/js-sourcemap/zoo-with-sourcemap-plugin.ts create mode 100644 playground/js-sourcemap/zoo.js delete mode 100644 playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts delete mode 100644 playground/plugin-sourcemap/index.html delete mode 100644 playground/plugin-sourcemap/package.json delete mode 100644 playground/plugin-sourcemap/vite.config.js diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index b4e63e66290a74..4b438a1d204242 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -31,6 +31,27 @@ if (!isBuild) { `) }) + test('plugin return sourcemap without sources', async () => { + const res = await page.request.get(new URL('./zoo.js', page.url()).href) + const js = await res.text() + expect(js).toContain('// add comment') + const map = extractSourcemap(js) + console.log('map', map) + expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` + { + "mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;", + "sources": [ + "zoo.js", + ], + "sourcesContent": [ + "export const zoo = 'zoo' + ", + ], + "version": 3, + } + `) + }) + test('js with inline sourcemap injected by a plugin', async () => { const res = await page.request.get( new URL('./foo-with-sourcemap.js', page.url()).href, diff --git a/playground/js-sourcemap/index.html b/playground/js-sourcemap/index.html index f669bf4fc102aa..e89390e6dc1219 100644 --- a/playground/js-sourcemap/index.html +++ b/playground/js-sourcemap/index.html @@ -4,6 +4,6 @@

JS Sourcemap

- + diff --git a/playground/js-sourcemap/package.json b/playground/js-sourcemap/package.json index b002697756a24c..816f80c600f51c 100644 --- a/playground/js-sourcemap/package.json +++ b/playground/js-sourcemap/package.json @@ -10,6 +10,7 @@ "preview": "vite preview" }, "dependencies": { - "@vitejs/test-importee-pkg": "file:importee-pkg" + "@vitejs/test-importee-pkg": "file:importee-pkg", + "magic-string": "^0.30.5" } } diff --git a/playground/plugin-sourcemap/foo.js b/playground/js-sourcemap/plugin-foo.js similarity index 100% rename from playground/plugin-sourcemap/foo.js rename to playground/js-sourcemap/plugin-foo.js diff --git a/playground/js-sourcemap/vite.config.js b/playground/js-sourcemap/vite.config.js index efebbc5ca00dee..41484f2c99d0f3 100644 --- a/playground/js-sourcemap/vite.config.js +++ b/playground/js-sourcemap/vite.config.js @@ -1,8 +1,12 @@ import { defineConfig } from 'vite' import transformFooWithInlineSourceMap from './foo-with-sourcemap-plugin' +import { transformZooWithSourcemapPlugin } from './zoo-with-sourcemap-plugin' export default defineConfig({ - plugins: [transformFooWithInlineSourceMap()], + plugins: [ + transformFooWithInlineSourceMap(), + transformZooWithSourcemapPlugin(), + ], build: { sourcemap: true, rollupOptions: { diff --git a/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts b/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts new file mode 100644 index 00000000000000..a744a89b5d478b --- /dev/null +++ b/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts @@ -0,0 +1,17 @@ +import MagicString from 'magic-string' +import { Plugin } from 'vite' +export const transformZooWithSourcemapPlugin: () => Plugin = () => ({ + name: 'sourcemap', + transform(code, id) { + if (id.includes('zoo.js')) { + const ms = new MagicString(code) + ms.append('// add comment') + return { + code: ms.toString(), + // NOTE: MagicString without `filename` option generates + // a sourcemap with `sources: ['']` or `sources: [null]` + map: ms.generateMap({ hires: true }), + } + } + }, +}) diff --git a/playground/js-sourcemap/zoo.js b/playground/js-sourcemap/zoo.js new file mode 100644 index 00000000000000..286343f930d3c3 --- /dev/null +++ b/playground/js-sourcemap/zoo.js @@ -0,0 +1 @@ +export const zoo = 'zoo' diff --git a/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts b/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts deleted file mode 100644 index f462760920222c..00000000000000 --- a/playground/plugin-sourcemap/__tests__/plugin-sourcemap.spec.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { URL } from 'node:url' -import { expect, test } from 'vitest' -import { - extractSourcemap, - formatSourcemapForSnapshot, - isBuild, - page, -} from '~utils' - -test.runIf(!isBuild)('correct sourcemap', async () => { - const res = await page.request.get(new URL('./foo.js', page.url()).href) - const js = await res.text() - expect(js).toContain('// add comment') - const map = extractSourcemap(js) - expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` - { - "mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;", - "sources": [ - "foo.js", - ], - "sourcesContent": [ - "export const foo = 'foo' - ", - ], - "version": 3, - } - `) -}) diff --git a/playground/plugin-sourcemap/index.html b/playground/plugin-sourcemap/index.html deleted file mode 100644 index 034e77d10968c0..00000000000000 --- a/playground/plugin-sourcemap/index.html +++ /dev/null @@ -1,5 +0,0 @@ -
-

Plugin Sourcemap

-
- - diff --git a/playground/plugin-sourcemap/package.json b/playground/plugin-sourcemap/package.json deleted file mode 100644 index 4acbfd56a7cba2..00000000000000 --- a/playground/plugin-sourcemap/package.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "name": "@vitejs/test-plugin-sourcemap", - "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": { - "magic-string": "^0.30.3" - } -} diff --git a/playground/plugin-sourcemap/vite.config.js b/playground/plugin-sourcemap/vite.config.js deleted file mode 100644 index 239fc72ab68a6e..00000000000000 --- a/playground/plugin-sourcemap/vite.config.js +++ /dev/null @@ -1,25 +0,0 @@ -import { defineConfig } from 'vite' -import MagicString from 'magic-string' - -export default defineConfig({ - plugins: [ - { - name: 'sourcemap', - transform(code, id) { - if (id.includes('foo.js')) { - const ms = new MagicString(code) - ms.append('// add comment') - return { - code: ms.toString(), - // NOTE: MagicString without `filename` option generates - // a sourcemap with `sources: ['']` or `sources: [null]` - map: ms.generateMap({ hires: true }), - } - } - }, - }, - ], - build: { - sourcemap: true, - }, -}) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6b1b61337fe3e6..6c0b64a28ba8d8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -697,6 +697,9 @@ importers: '@vitejs/test-importee-pkg': specifier: file:importee-pkg version: file:playground/js-sourcemap/importee-pkg + magic-string: + specifier: ^0.30.5 + version: 0.30.5 playground/js-sourcemap/importee-pkg: {} @@ -1014,12 +1017,6 @@ importers: playground/optimize-missing-deps/multi-entry-dep: {} - playground/plugin-sourcemap: - devDependencies: - magic-string: - specifier: ^0.30.3 - version: 0.30.3 - playground/preload: devDependencies: '@vitejs/test-dep-a': From fca8146661de882a29227e7087dbc88868ae35d4 Mon Sep 17 00:00:00 2001 From: Dunqing Date: Tue, 21 Nov 2023 17:21:16 +0800 Subject: [PATCH 10/13] fix: missing file --- playground/js-sourcemap/index.html | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/playground/js-sourcemap/index.html b/playground/js-sourcemap/index.html index e89390e6dc1219..80ee729d99ce90 100644 --- a/playground/js-sourcemap/index.html +++ b/playground/js-sourcemap/index.html @@ -4,6 +4,7 @@

JS Sourcemap

- + + From 80e2e4a935c0768191f7f7ba5b263898710b3ad0 Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 21 Nov 2023 21:00:17 +0900 Subject: [PATCH 11/13] chore: fix lint --- playground/js-sourcemap/zoo-with-sourcemap-plugin.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts b/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts index a744a89b5d478b..715fe349a8d420 100644 --- a/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts +++ b/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts @@ -1,5 +1,6 @@ import MagicString from 'magic-string' -import { Plugin } from 'vite' +import type { Plugin } from 'vite' + export const transformZooWithSourcemapPlugin: () => Plugin = () => ({ name: 'sourcemap', transform(code, id) { From bf0b4007b2641c2f41d22b1a516b6e186128de0d Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 21 Nov 2023 21:02:11 +0900 Subject: [PATCH 12/13] chore: format comment --- playground/js-sourcemap/zoo-with-sourcemap-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts b/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts index 715fe349a8d420..6c493278d166c8 100644 --- a/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts +++ b/playground/js-sourcemap/zoo-with-sourcemap-plugin.ts @@ -10,7 +10,7 @@ export const transformZooWithSourcemapPlugin: () => Plugin = () => ({ return { code: ms.toString(), // NOTE: MagicString without `filename` option generates - // a sourcemap with `sources: ['']` or `sources: [null]` + // a sourcemap with `sources: ['']` or `sources: [null]` map: ms.generateMap({ hires: true }), } } From e8a09a5421190e38724dfc0173814a331863b5fb Mon Sep 17 00:00:00 2001 From: sapphi-red <49056869+sapphi-red@users.noreply.github.com> Date: Tue, 21 Nov 2023 21:03:15 +0900 Subject: [PATCH 13/13] chore: remove console.log --- playground/js-sourcemap/__tests__/js-sourcemap.spec.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts index 4b438a1d204242..208213d3d84f9e 100644 --- a/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts +++ b/playground/js-sourcemap/__tests__/js-sourcemap.spec.ts @@ -31,12 +31,12 @@ if (!isBuild) { `) }) - test('plugin return sourcemap without sources', async () => { + test('plugin return sourcemap with `sources: [""]`', async () => { const res = await page.request.get(new URL('./zoo.js', page.url()).href) const js = await res.text() expect(js).toContain('// add comment') + const map = extractSourcemap(js) - console.log('map', map) expect(formatSourcemapForSnapshot(map)).toMatchInlineSnapshot(` { "mappings": "AAAA,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;",