diff --git a/packages/webpack/src/plugins/vue/modern.js b/packages/webpack/src/plugins/vue/modern.js index 7878927d86..52f450fd37 100644 --- a/packages/webpack/src/plugins/vue/modern.js +++ b/packages/webpack/src/plugins/vue/modern.js @@ -7,8 +7,8 @@ import EventEmitter from 'events' import HtmlWebpackPlugin from 'html-webpack-plugin' import { safariNoModuleFix } from '@nuxt/utils' -const assetsMap = {} -const watcher = new EventEmitter() +const legacyTemplateTags = {} +const legacyTemplateWatcher = new EventEmitter() export default class ModernModePlugin { constructor ({ targetDir, isModernBuild, noUnsafeInline }) { @@ -25,24 +25,15 @@ export default class ModernModePlugin { } } - get assets () { - return assetsMap - } - - set assets ({ name, content }) { - assetsMap[name] = content - watcher.emit(name) - } - - getAssets (name) { + getLegacyTemplateTags (name) { return new Promise((resolve) => { - const asset = this.assets[name] - if (asset) { - return resolve(asset) + const tags = legacyTemplateTags[name] + if (tags) { + return resolve(tags) } - return watcher.once(name, () => { - const asset = this.assets[name] - return asset && resolve(asset) + return legacyTemplateWatcher.once(name, () => { + const tags = legacyTemplateTags[name] + return tags && resolve(tags) }) }) } @@ -50,14 +41,13 @@ export default class ModernModePlugin { applyLegacy (compiler) { const ID = 'nuxt-legacy-bundle' compiler.hooks.compilation.tap(ID, (compilation) => { - HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tapAsync(ID, (data, cb) => { - // get stats, write to disk - this.assets = { - name: data.plugin.options.filename, - content: data.bodyTags - } - - cb() + HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tap(ID, (data) => { + HtmlWebpackPlugin.getHooks(compilation).afterEmit.tap(ID, ({ outputName }) => { + // get stats, write to disk + legacyTemplateTags[data.plugin.options.filename] = data.bodyTags + legacyTemplateWatcher.emit(outputName) + }) + return data }) }) } @@ -65,7 +55,7 @@ export default class ModernModePlugin { applyModern (compiler) { const ID = 'nuxt-modern-bundle' compiler.hooks.compilation.tap(ID, (compilation) => { - HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tapAsync(ID, async (data, cb) => { + HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups.tapPromise(ID, async (data) => { // use