From e0a84c3a759336aa8b384e226eaa3ed78db0e697 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E9=B8=A1=E6=B2=A1=E5=90=8D?= Date: Sun, 30 Jun 2024 20:35:34 +0800 Subject: [PATCH 1/3] chore: comments --- src/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index b9f9c0a..adb231f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -86,8 +86,9 @@ export default function native(options: PrebundleOptions): Plugin { } }, async config(config) { + // Not necessary, since bundle.js is wrapped in an immediately executed closure. modifyCommonjs(config, options.modules) - // Run build are not necessary. + // Not necessary, since Pre-Bundling is disabled by default in `vite build` command. modifyOptimizeDeps(config, options.modules) }, } From 5bd47c7f5a32174b1f3e53b0a31bfb32364f8cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E9=B8=A1=E6=B2=A1=E5=90=8D?= Date: Sun, 30 Jun 2024 20:36:04 +0800 Subject: [PATCH 2/3] feat: improve sourcemap --- src/utils.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/utils.ts b/src/utils.ts index 5df84e8..a1c7760 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -28,12 +28,28 @@ export function cjs2esm(id: string, code: string) { const snippet = libEsm({ exports: Object.getOwnPropertyNames(cjs.require(id)), }) + const commentsStart = '/* [webpack-prebundle]: start */' + const commentsEnd = '/* [webpack-prebundle]: end */' + const sourcemapURL = '//# sourceMappingURL=' + const esmStart = [commentsStart, 'const module = { exports: {} };', commentsEnd].join('') + const esmEnd = [ + commentsStart, + 'const _M_ = module.exports;', + snippet.exports, + commentsEnd, + ].join('\n') + + code = esmStart + code + const lines = code.split('\n') + let lastLine = lines[lines.length - 1] + + if (lastLine.startsWith(sourcemapURL)) { + lines[lines.length - 1] = `${esmEnd}\n${lastLine}` + code = lines.join('\n') + } else { + code = `${code}\n${esmEnd}` + } // TODO: generate sourcemap for esm wrap-snippet - return ` -const module = { exports: {} }; -${code} -const _M_ = module.exports; -${snippet.exports} -` + return code } From 429e31fd0eec79ea01732e990d55d92d168acd91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8E=E9=B8=A1=E6=B2=A1=E5=90=8D?= Date: Mon, 1 Jul 2024 10:14:17 +0800 Subject: [PATCH 3/3] fix: add `exports` variable --- src/utils.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index a1c7760..ca014cf 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -31,7 +31,7 @@ export function cjs2esm(id: string, code: string) { const commentsStart = '/* [webpack-prebundle]: start */' const commentsEnd = '/* [webpack-prebundle]: end */' const sourcemapURL = '//# sourceMappingURL=' - const esmStart = [commentsStart, 'const module = { exports: {} };', commentsEnd].join('') + const esmStart = [commentsStart, 'const module = { exports: {} }; const exports = module.exports;', commentsEnd].join('') const esmEnd = [ commentsStart, 'const _M_ = module.exports;',