mirror of
https://github.com/nuxt/nuxt.git
synced 2025-01-31 07:40:33 +00:00
fix: sourcemap support for nuxt:vite-relative-asset
(#3428)
This commit is contained in:
parent
0c99002351
commit
a0f8a44e10
@ -15,6 +15,7 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nuxt/schema": "3.0.0",
|
"@nuxt/schema": "3.0.0",
|
||||||
"@types/cssnano": "^5",
|
"@types/cssnano": "^5",
|
||||||
|
"magic-string-extra": "^0.1.0",
|
||||||
"unbuild": "latest",
|
"unbuild": "latest",
|
||||||
"vue": "3.2.31"
|
"vue": "3.2.31"
|
||||||
},
|
},
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { createUnplugin } from 'unplugin'
|
import { createUnplugin } from 'unplugin'
|
||||||
import escapeRE from 'escape-string-regexp'
|
import escapeRE from 'escape-string-regexp'
|
||||||
import type { Plugin } from 'vite'
|
import type { Plugin } from 'vite'
|
||||||
|
import MagicString from 'magic-string-extra'
|
||||||
|
|
||||||
interface DynamicBasePluginOptions {
|
interface DynamicBasePluginOptions {
|
||||||
env: 'dev' | 'server' | 'client'
|
env: 'dev' | 'server' | 'client'
|
||||||
@ -42,51 +43,57 @@ export const DynamicBasePlugin = createUnplugin(function (options: DynamicBasePl
|
|||||||
return null
|
return null
|
||||||
},
|
},
|
||||||
enforce: 'post',
|
enforce: 'post',
|
||||||
transform (original, id) {
|
transform (code, id) {
|
||||||
let code = original
|
const s = new MagicString(code)
|
||||||
|
let injectUtils = false
|
||||||
|
|
||||||
if (options.globalPublicPath && id.includes('entry.ts')) {
|
if (options.globalPublicPath && id.includes('entry.ts')) {
|
||||||
code = 'import { joinURL } from "ufo";' +
|
injectUtils = true
|
||||||
`${options.globalPublicPath} = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir);` + code
|
s.prepend(`${options.globalPublicPath} = joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir);`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const assetId = code.match(VITE_ASSET_RE)
|
const assetId = code.match(VITE_ASSET_RE)
|
||||||
if (assetId) {
|
if (assetId) {
|
||||||
code = 'import { joinURL } from "ufo";' +
|
injectUtils = true
|
||||||
`export default joinURL(NUXT_BASE, NUXT_CONFIG.app.buildAssetsDir, "${assetId[1]}".replace("/__NUXT_BASE__", ""));`
|
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 =')) {
|
if (injectUtils || (code.includes('NUXT_BASE') && !code.includes('const NUXT_BASE ='))) {
|
||||||
code = 'const NUXT_BASE = NUXT_CONFIG.app.cdnURL || NUXT_CONFIG.app.baseURL;' + code
|
s.prepend('const NUXT_BASE = NUXT_CONFIG.app.cdnURL || NUXT_CONFIG.app.baseURL;')
|
||||||
|
|
||||||
if (options.env === 'dev') {
|
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') {
|
} else if (options.env === 'server') {
|
||||||
code = 'import NUXT_CONFIG from "#config";' + code
|
s.prepend('import NUXT_CONFIG from "#config";')
|
||||||
} else {
|
} else {
|
||||||
code = 'const NUXT_CONFIG = __NUXT__.config;' + code
|
s.prepend('const NUXT_CONFIG = __NUXT__.config;')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id === 'vite/preload-helper') {
|
if (id === 'vite/preload-helper') {
|
||||||
|
injectUtils = true
|
||||||
// Define vite base path as buildAssetsUrl (i.e. including _nuxt/)
|
// Define vite base path as buildAssetsUrl (i.e. including _nuxt/)
|
||||||
code = code.replace(
|
code.replace(
|
||||||
/const base = ['"]\/__NUXT_BASE__\/['"]/,
|
/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
|
// 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
|
// Dynamically compute string URLs featuring baseURL
|
||||||
for (const delimiter of ['`', '"', "'"]) {
|
for (const delimiter of ['`', '"', "'"]) {
|
||||||
const delimiterRE = new RegExp(`${delimiter}([^${delimiter}]*)\\/__NUXT_BASE__\\/([^${delimiter}]*)${delimiter}`, 'g')
|
const delimiterRE = new RegExp(`${delimiter}([^${delimiter}]*)\\/__NUXT_BASE__\\/([^${delimiter}]*)${delimiter}`, 'g')
|
||||||
/* eslint-disable-next-line no-template-curly-in-string */
|
/* 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 }
|
if (injectUtils) {
|
||||||
return code
|
s.prepend('import { joinURL } from "ufo";\n')
|
||||||
|
}
|
||||||
|
|
||||||
|
return s.toRollupResult(true, { source: id, includeContent: true })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
10
yarn.lock
10
yarn.lock
@ -3372,6 +3372,7 @@ __metadata:
|
|||||||
get-port-please: ^2.4.2
|
get-port-please: ^2.4.2
|
||||||
knitwork: ^0.1.0
|
knitwork: ^0.1.0
|
||||||
magic-string: ^0.25.7
|
magic-string: ^0.25.7
|
||||||
|
magic-string-extra: ^0.1.0
|
||||||
mlly: ^0.4.3
|
mlly: ^0.4.3
|
||||||
p-debounce: ^4.0.0
|
p-debounce: ^4.0.0
|
||||||
pathe: ^0.2.0
|
pathe: ^0.2.0
|
||||||
@ -13761,6 +13762,15 @@ __metadata:
|
|||||||
languageName: node
|
languageName: node
|
||||||
linkType: hard
|
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":
|
"magic-string-extra@npm:^0.1.2":
|
||||||
version: 0.1.2
|
version: 0.1.2
|
||||||
resolution: "magic-string-extra@npm:0.1.2"
|
resolution: "magic-string-extra@npm:0.1.2"
|
||||||
|
Loading…
Reference in New Issue
Block a user