diff --git a/src/index.ts b/src/index.ts index 37c56f9..4b43441 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,15 +1,17 @@ import fs from 'node:fs' import path from 'node:path' import type { Plugin, ResolvedConfig } from 'vite' +import type { SourceMapInput } from 'rollup' import type { Loader } from 'esbuild' import { parse as parseAst } from 'acorn' +import MagicString from 'magic-string' import { DEFAULT_EXTENSIONS, KNOWN_SFC_EXTENSIONS, KNOWN_ASSET_TYPES, KNOWN_CSS_TYPES, } from 'vite-plugin-utils/constant' -import { MagicString, cleanUrl } from 'vite-plugin-utils/function' +import { cleanUrl } from 'vite-plugin-utils/function' import { analyzer } from './analyze' import { generateImport } from './generate-import' import { generateExport } from './generate-export' @@ -77,7 +79,7 @@ export default function commonjs(options: Options = {}): Plugin { return } - const contents = await transformCommonjs({ + const result = await transformCommonjs({ options, code, id, @@ -85,9 +87,9 @@ export default function commonjs(options: Options = {}): Plugin { dynaimcRequire, }) - if (contents != null) { + if (result != null) { return { - contents, + contents: result.code, loader: id.slice(id.lastIndexOf('.') + 1) as Loader, } } @@ -119,7 +121,7 @@ async function transformCommonjs({ id: string, extensions: string[], dynaimcRequire: DynaimcRequire, -}) { +}): Promise<{ code: string; map: SourceMapInput; } | null | undefined> { if (!(extensions.includes(path.extname(id)) || extensions.includes(path.extname(cleanUrl(id))))) return if (!isCommonjs(code)) return @@ -218,6 +220,10 @@ async function transformCommonjs({ } } - const str = ms.toString() - return str !== code ? str : null + if (ms.hasChanged()) { + return { + code: ms.toString(), + map: ms.generateMap({ hires: true, source: id }), + } + } }