2022-07-21 10:44:33 +00:00
|
|
|
import { createUnplugin } from 'unplugin'
|
|
|
|
import MagicString from 'magic-string'
|
|
|
|
|
|
|
|
interface DynamicBasePluginOptions {
|
|
|
|
globalPublicPath?: string
|
|
|
|
sourcemap?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaults: DynamicBasePluginOptions = {
|
|
|
|
globalPublicPath: '__webpack_public_path__',
|
|
|
|
sourcemap: true
|
|
|
|
}
|
|
|
|
|
|
|
|
export const DynamicBasePlugin = createUnplugin((options: DynamicBasePluginOptions = {}) => {
|
|
|
|
options = { ...defaults, ...options }
|
|
|
|
return {
|
|
|
|
name: 'nuxt:dynamic-base-path',
|
|
|
|
enforce: 'post',
|
|
|
|
transform (code, id) {
|
|
|
|
if (!id.includes('paths.mjs') || !code.includes('const appConfig = ')) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const s = new MagicString(code)
|
2022-10-16 09:03:02 +00:00
|
|
|
s.append(`\n${options.globalPublicPath} = buildAssetsURL();\n`)
|
2022-07-21 10:44:33 +00:00
|
|
|
return {
|
|
|
|
code: s.toString(),
|
2022-08-26 15:47:29 +00:00
|
|
|
map: options.sourcemap
|
2023-04-14 17:21:08 +00:00
|
|
|
? s.generateMap({ hires: true })
|
2022-08-26 15:47:29 +00:00
|
|
|
: undefined
|
2022-07-21 10:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|