From 17dd24a7088679f8f2cb96dd41d12b377693a008 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Thu, 16 Sep 2021 08:56:25 +0800 Subject: [PATCH] fix: cleanup sources in transform map close https://github.com/antfu/unplugin-vue-components/issues/138 --- src/webpack/loaders/transform.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/webpack/loaders/transform.ts b/src/webpack/loaders/transform.ts index 57e9203..9116e43 100644 --- a/src/webpack/loaders/transform.ts +++ b/src/webpack/loaders/transform.ts @@ -19,7 +19,23 @@ export default async function transform (this: LoaderContext, source: strin if (res == null) { callback(null, source, map) } else if (typeof res !== 'string') { - callback(null, res.code, map == null ? map : (res.map || map)) + // only pass sourcemap when sourcemap is provided upstream + const newMap = map == null ? map : (res.map || map) + // clean up nullish sources produced by magic-string + if (newMap && Array.isArray(newMap.sources)) { + const excluded: number[] = [] + newMap.sources = newMap.sources.filter((i:any, idx:number) => { + if (i == null) { + excluded.push(idx) + return false + } + return true + }) + newMap.sources = newMap.sources.filter((_: any, idx: number) => { + return !excluded.includes(idx) + }) + } + callback(null, res.code, newMap) } else { callback(null, res, map) }