From 1d68b51c2c1d48896b1e366c778ba6d4fbd874bc Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 25 Jan 2023 04:52:00 -0800 Subject: [PATCH] fix(nuxt): async transform for inline middleware (#18460) --- packages/nuxt/src/core/nuxt.ts | 6 ++++-- packages/nuxt/src/core/plugins/unctx.ts | 8 +++++++- test/fixtures/basic/pages/navigate-to-redirect.vue | 5 +++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 477f5b10cd..0e754ed886 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -80,8 +80,10 @@ async function initNuxt (nuxt: Nuxt) { addWebpackPlugin(ImportProtectionPlugin.webpack(config)) // Add unctx transform - addVitePlugin(UnctxTransformPlugin(nuxt).vite({ sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client })) - addWebpackPlugin(UnctxTransformPlugin(nuxt).webpack({ sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client })) + nuxt.hook('modules:done', () => { + addVitePlugin(UnctxTransformPlugin(nuxt).vite({ sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client })) + addWebpackPlugin(UnctxTransformPlugin(nuxt).webpack({ sourcemap: nuxt.options.sourcemap.server || nuxt.options.sourcemap.client })) + }) if (!nuxt.options.dev) { const removeFromServer = ['onBeforeMount', 'onMounted', 'onBeforeUpdate', 'onRenderTracked', 'onRenderTriggered', 'onActivated', 'onDeactivated', 'onBeforeUnmount'] diff --git a/packages/nuxt/src/core/plugins/unctx.ts b/packages/nuxt/src/core/plugins/unctx.ts index 2cc6961d23..420838e6b0 100644 --- a/packages/nuxt/src/core/plugins/unctx.ts +++ b/packages/nuxt/src/core/plugins/unctx.ts @@ -3,6 +3,8 @@ import { normalize } from 'pathe' import { createTransformer } from 'unctx/transform' import { createUnplugin } from 'unplugin' +const TRANSFORM_MARKER = '/* _processed_nuxt_unctx_transform */\n' + export const UnctxTransformPlugin = (nuxt: Nuxt) => { const transformer = createTransformer({ asyncFunctions: ['defineNuxtPlugin', 'defineNuxtRouteMiddleware'] @@ -15,14 +17,18 @@ export const UnctxTransformPlugin = (nuxt: Nuxt) => { name: 'unctx:transform', enforce: 'post', transformInclude (id) { + if (id.includes('macro=true')) { return true } + id = normalize(id).replace(/\?.*$/, '') return app?.plugins.some(i => i.src === id) || app?.middleware.some(m => m.path === id) }, transform (code, id) { + // TODO: needed for webpack - update transform in unctx/unplugin? + if (code.startsWith(TRANSFORM_MARKER)) { return } const result = transformer.transform(code) if (result) { return { - code: result.code, + code: TRANSFORM_MARKER + result.code, map: options.sourcemap ? result.magicString.generateMap({ source: id, includeContent: true }) : undefined diff --git a/test/fixtures/basic/pages/navigate-to-redirect.vue b/test/fixtures/basic/pages/navigate-to-redirect.vue index b0c7bea050..b705612434 100644 --- a/test/fixtures/basic/pages/navigate-to-redirect.vue +++ b/test/fixtures/basic/pages/navigate-to-redirect.vue @@ -4,8 +4,9 @@