From 9d59723b6ecbc6f446809ca40b63959e3dc3e92f Mon Sep 17 00:00:00 2001 From: likui <2218301630@qq.com> Date: Thu, 6 Aug 2020 11:14:51 +0800 Subject: [PATCH] fix: fix sourcemap fix #4 --- src/serverPlugin.ts | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/serverPlugin.ts b/src/serverPlugin.ts index 5e28c5e..36d6fac 100644 --- a/src/serverPlugin.ts +++ b/src/serverPlugin.ts @@ -1,6 +1,10 @@ import { ServerPlugin, Context } from 'vite' -import { parse, compileTemplate } from '@vue/component-compiler-utils' -import { SFCDescriptor, SFCBlock } from 'vue-template-compiler' +import { + parse, + compileTemplate, + SFCDescriptor, + SFCBlock, +} from '@vue/component-compiler-utils' import * as fs from 'fs-extra' import hash_sum from 'hash-sum' import { transform } from './esbuildService' @@ -17,6 +21,7 @@ import { import { srcImportMap } from 'vite/dist/node/server/serverPluginVue' import { TemplateCompileOptions } from '@vue/component-compiler-utils/lib/compileTemplate' import { clientPublicPath } from 'vite/dist/node/server/serverPluginClient' +import { mergeSourceMap } from 'vite/dist/node/server/serverPluginSourceMap' const vueTemplateCompiler = require('vue-template-compiler') @@ -90,6 +95,7 @@ export const vuePlugin: ServerPlugin = ({ descriptor, resolver ) + ctx.map = descriptor.script!.map return } @@ -140,11 +146,14 @@ async function parseSFC( code = readFile(resolver.requestToFile(srcPath)) } if (scriptBlock.lang === 'ts') { - code = ( - await transform(code, publicPath, { - loader: 'ts', - }) - ).code + const res = await transform(code, publicPath, { + loader: 'ts', + }) + code = res.code + scriptBlock.map = mergeSourceMap( + scriptBlock.map, + JSON.parse(res.map!) + ) as any } // rewrite export default.