From 79a8c852e84506a844057b0a2fcf9448fb8123e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=A0=20/=20green?= Date: Fri, 13 May 2022 17:37:38 +0900 Subject: [PATCH] chore(plugin-vue): replace source-map with gen/trace-mapping (#8155) --- packages/plugin-vue/package.json | 2 ++ packages/plugin-vue/src/main.ts | 28 +++++++++++++++++++++------- pnpm-lock.yaml | 13 +++++++++++++ 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/packages/plugin-vue/package.json b/packages/plugin-vue/package.json index eef528d8fc46f0..e6c712b6545663 100644 --- a/packages/plugin-vue/package.json +++ b/packages/plugin-vue/package.json @@ -39,6 +39,8 @@ "vue": "^3.2.25" }, "devDependencies": { + "@jridgewell/gen-mapping": "^0.3.1", + "@jridgewell/trace-mapping": "^0.3.10", "debug": "^4.3.4", "rollup": "^2.72.1", "slash": "^4.0.0", diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts index 6df6901a2bab40..8215ff7ba346a7 100644 --- a/packages/plugin-vue/src/main.ts +++ b/packages/plugin-vue/src/main.ts @@ -3,7 +3,10 @@ import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc' import type { PluginContext, SourceMap, TransformPluginContext } from 'rollup' import { normalizePath } from '@rollup/pluginutils' import type { RawSourceMap } from 'source-map' -import { SourceMapConsumer, SourceMapGenerator } from 'source-map' +import type { EncodedSourceMap as TraceEncodedSourceMap } from '@jridgewell/trace-mapping' +import { TraceMap, eachMapping } from '@jridgewell/trace-mapping' +import type { EncodedSourceMap as GenEncodedSourceMap } from '@jridgewell/gen-mapping' +import { addMapping, fromMap, toEncodedMap } from '@jridgewell/gen-mapping' import { transformWithEsbuild } from 'vite' import { createDescriptor, @@ -158,13 +161,19 @@ export async function transformMain( // of templateMap, we need to concatenate the two source maps. let resolvedMap = options.sourceMap ? map : undefined if (resolvedMap && templateMap) { - const generator = SourceMapGenerator.fromSourceMap( - new SourceMapConsumer(map) + const gen = fromMap( + // version property of result.map is declared as string + // but actually it is `3` + map as Omit as TraceEncodedSourceMap + ) + const tracer = new TraceMap( + // same above + templateMap as Omit as TraceEncodedSourceMap ) const offset = (scriptCode.match(/\r?\n/g)?.length ?? 0) + 1 - const templateMapConsumer = new SourceMapConsumer(templateMap) - templateMapConsumer.eachMapping((m) => { - generator.addMapping({ + eachMapping(tracer, (m) => { + if (m.source == null) return + addMapping(gen, { source: m.source, original: { line: m.originalLine, column: m.originalColumn }, generated: { @@ -173,7 +182,12 @@ export async function transformMain( } }) }) - resolvedMap = (generator as any).toJSON() as RawSourceMap + + // same above + resolvedMap = toEncodedMap(gen) as Omit< + GenEncodedSourceMap, + 'version' + > as RawSourceMap // if this is a template only update, we will be reusing a cached version // of the main module compile result, which has outdated sourcesContent. resolvedMap.sourcesContent = templateMap.sourcesContent diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 00d6847a132bf4..4c9e2f58cd9789 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -172,6 +172,8 @@ importers: packages/plugin-vue: specifiers: + '@jridgewell/gen-mapping': ^0.3.1 + '@jridgewell/trace-mapping': ^0.3.10 '@rollup/pluginutils': ^4.2.1 debug: ^4.3.4 rollup: ^2.72.1 @@ -182,6 +184,8 @@ importers: dependencies: '@rollup/pluginutils': 4.2.1 devDependencies: + '@jridgewell/gen-mapping': 0.3.1 + '@jridgewell/trace-mapping': 0.3.11 debug: 4.3.4 rollup: 2.72.1 slash: 4.0.0 @@ -1641,6 +1645,15 @@ packages: '@jridgewell/set-array': 1.1.1 '@jridgewell/sourcemap-codec': 1.4.13 + /@jridgewell/gen-mapping/0.3.1: + resolution: {integrity: sha512-GcHwniMlA2z+WFPWuY8lp3fsza0I8xPFMWL5+n8LYyP6PSvPrXf4+n8stDHZY2DM0zy9sVkRDy1jDI4XGzYVqg==} + engines: {node: '>=6.0.0'} + dependencies: + '@jridgewell/set-array': 1.1.1 + '@jridgewell/sourcemap-codec': 1.4.13 + '@jridgewell/trace-mapping': 0.3.11 + dev: true + /@jridgewell/resolve-uri/3.0.7: resolution: {integrity: sha512-8cXDaBBHOr2pQ7j77Y6Vp5VDT2sIqWyWQ56TjEq4ih/a4iST3dItRe8Q9fp0rrIl9DoKhWQtUQz/YpOxLkXbNA==} engines: {node: '>=6.0.0'}