Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
refactor webpack plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 21, 2022
1 parent d0a3bab commit b832953
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
25 changes: 14 additions & 11 deletions packages/webpack/src/plugins/dynamic-base.ts
Expand Up @@ -6,22 +6,25 @@ interface DynamicBasePluginOptions {
sourcemap?: boolean
}

export const DynamicBasePlugin = createUnplugin(function (options: DynamicBasePluginOptions = {}) {
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) {
const s = new MagicString(code)

if (options.globalPublicPath && id.includes('paths.mjs') && code.includes('const appConfig = ')) {
s.append(`${options.globalPublicPath} = buildAssetsURL();\n`)
if (!id.includes('paths.mjs') || !code.includes('const appConfig = ')) {
return
}

if (s.hasChanged()) {
return {
code: s.toString(),
map: options.sourcemap && s.generateMap({ source: id, includeContent: true })
}
const s = new MagicString(code)
s.append(`${options.globalPublicPath} = buildAssetsURL();\n`)
return {
code: s.toString(),
map: options.sourcemap && s.generateMap({ source: id, includeContent: true })
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions packages/webpack/src/webpack.ts
Expand Up @@ -35,8 +35,7 @@ export async function bundle (nuxt: Nuxt) {
// Configure compilers
const compilers = webpackConfigs.map((config) => {
config.plugins.push(DynamicBasePlugin.webpack({
sourcemap: nuxt.options.sourcemap,
globalPublicPath: '__webpack_public_path__'
sourcemap: nuxt.options.sourcemap
}))
config.plugins.push(composableKeysPlugin.webpack({
sourcemap: nuxt.options.sourcemap,
Expand Down

0 comments on commit b832953

Please sign in to comment.