From a0f8a44e10958035a8b8ba76becf5145d0c9dc98 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Tue, 1 Mar 2022 03:24:52 +0800 Subject: [PATCH] fix: sourcemap support for `nuxt:vite-relative-asset` (#3428) --- packages/vite/package.json | 1 + packages/vite/src/plugins/dynamic-base.ts | 43 +++++++++++++---------- yarn.lock | 10 ++++++ 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/packages/vite/package.json b/packages/vite/package.json index 4e5d948332..cd89c2e0aa 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -15,6 +15,7 @@ "devDependencies": { "@nuxt/schema": "3.0.0", "@types/cssnano": "^5", + "magic-string-extra": "^0.1.0", "unbuild": "latest", "vue": "3.2.31" }, diff --git a/packages/vite/src/plugins/dynamic-base.ts b/packages/vite/src/plugins/dynamic-base.ts index e32dfb429a..67a48b9fd5 100644 --- a/packages/vite/src/plugins/dynamic-base.ts +++ b/packages/vite/src/plugins/dynamic-base.ts @@ -1,6 +1,7 @@ import { createUnplugin } from 'unplugin' import escapeRE from 'escape-string-regexp' import type { Plugin } from 'vite' +import MagicString from 'magic-string-extra' interface DynamicBasePluginOptions { env: 'dev' | 'server' | 'client' @@ -42,51 +43,57 @@ export const DynamicBasePlugin = createUnplugin(function (options: DynamicBasePl return null }, enforce: 'post', - transform (original, id) { - let code = original + transform (code, id) { + const s = new MagicString(code) + let injectUtils = false + if (options.globalPublicPath && id.includes('entry.ts')) { - code = 'import { joinURL } from "ufo";' + - `${options.globalPublicPath} = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir);` + code + injectUtils = true + s.prepend(`${options.globalPublicPath} = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir);`) } const assetId = code.match(VITE_ASSET_RE) if (assetId) { - code = 'import { joinURL } from "ufo";' + - `export default joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir, "${assetId[1]}".replace("/__NUXT_BASE__", ""));` + injectUtils = true + s.overwrite(0, code.length, `export default joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir, "${assetId[1]}".replace("/__NUXT_BASE__", ""));`) } - if (code.includes('NUXT_BASE') && !code.includes('const NUXT_BASE =')) { - code = 'const NUXT_BASE = NUXT_CONFIG.app.cdnURL || NUXT_CONFIG.app.baseURL;' + code + if (injectUtils || (code.includes('NUXT_BASE') && !code.includes('const NUXT_BASE ='))) { + s.prepend('const NUXT_BASE = NUXT_CONFIG.app.cdnURL || NUXT_CONFIG.app.baseURL;') if (options.env === 'dev') { - code = `const NUXT_CONFIG = { app: ${JSON.stringify(options.devAppConfig)} };` + code + s.prepend(`const NUXT_CONFIG = { app: ${JSON.stringify(options.devAppConfig)} };`) } else if (options.env === 'server') { - code = 'import NUXT_CONFIG from "#config";' + code + s.prepend('import NUXT_CONFIG from "#config";') } else { - code = 'const NUXT_CONFIG = __NUXT__.config;' + code + s.prepend('const NUXT_CONFIG = __NUXT__.config;') } } if (id === 'vite/preload-helper') { + injectUtils = true // Define vite base path as buildAssetsUrl (i.e. including _nuxt/) - code = code.replace( + code.replace( /const base = ['"]\/__NUXT_BASE__\/['"]/, - 'import { joinURL } from "ufo";' + - 'const base = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir);') + 'const base = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir);' + ) } // Sanitize imports - code = code.replace(/from *['"]\/__NUXT_BASE__(\/[^'"]*)['"]/g, 'from "$1"') + s.replace(/from *['"]\/__NUXT_BASE__(\/[^'"]*)['"]/g, 'from "$1"') // Dynamically compute string URLs featuring baseURL for (const delimiter of ['`', '"', "'"]) { const delimiterRE = new RegExp(`${delimiter}([^${delimiter}]*)\\/__NUXT_BASE__\\/([^${delimiter}]*)${delimiter}`, 'g') /* eslint-disable-next-line no-template-curly-in-string */ - code = code.replace(delimiterRE, '`$1${NUXT_BASE}$2`') + s.replace(delimiterRE, '`$1${NUXT_BASE}$2`') } - if (code === original) { return } - return code + if (injectUtils) { + s.prepend('import { joinURL } from "ufo";\n') + } + + return s.toRollupResult(true, { source: id, includeContent: true }) } } }) diff --git a/yarn.lock b/yarn.lock index e67c663c0f..a4ab86c39a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3372,6 +3372,7 @@ __metadata: get-port-please: ^2.4.2 knitwork: ^0.1.0 magic-string: ^0.25.7 + magic-string-extra: ^0.1.0 mlly: ^0.4.3 p-debounce: ^4.0.0 pathe: ^0.2.0 @@ -13761,6 +13762,15 @@ __metadata: languageName: node linkType: hard +"magic-string-extra@npm:^0.1.0": + version: 0.1.0 + resolution: "magic-string-extra@npm:0.1.0" + dependencies: + magic-string: ^0.25.7 + checksum: 875f7f976f77d9bf697af46541420ffe5c9d5dca93f6d181aac7d875466bc3f8abf72b05f914a55ccf0abed75523362fd1882c3d407a920b280f72ed3b45e499 + languageName: node + linkType: hard + "magic-string-extra@npm:^0.1.2": version: 0.1.2 resolution: "magic-string-extra@npm:0.1.2"