From 7a1f1df0881e166127db5c4469cafbe3f72c727a Mon Sep 17 00:00:00 2001 From: Julien Huang Date: Mon, 25 Nov 2024 11:06:53 +0100 Subject: [PATCH] refactor: move `composable-keys` plugin into nuxt core (#30029) --- packages/nuxt/src/core/nuxt.ts | 8 ++++++++ .../src => nuxt/src/core}/plugins/composable-keys.ts | 4 ++-- packages/nuxt/src/core/utils/plugins.ts | 10 ++++++++++ packages/{vite => nuxt}/test/composable-keys.test.ts | 2 +- packages/vite/src/utils/index.ts | 10 ---------- packages/vite/src/vite.ts | 6 ------ packages/webpack/src/webpack.ts | 6 ------ 7 files changed, 21 insertions(+), 25 deletions(-) rename packages/{vite/src => nuxt/src/core}/plugins/composable-keys.ts (98%) rename packages/{vite => nuxt}/test/composable-keys.test.ts (93%) diff --git a/packages/nuxt/src/core/nuxt.ts b/packages/nuxt/src/core/nuxt.ts index 835d71c9bd..e2402adf68 100644 --- a/packages/nuxt/src/core/nuxt.ts +++ b/packages/nuxt/src/core/nuxt.ts @@ -40,6 +40,7 @@ import { initNitro } from './nitro' import schemaModule from './schema' import { RemovePluginMetadataPlugin } from './plugins/plugin-metadata' import { AsyncContextInjectionPlugin } from './plugins/async-context' +import { ComposableKeysPlugin } from './plugins/composable-keys' import { resolveDeepImportsPlugin } from './plugins/resolve-deep-imports' import { prehydrateTransformPlugin } from './plugins/prehydrate' import { VirtualFSPlugin } from './plugins/virtual' @@ -247,6 +248,13 @@ async function initNuxt (nuxt: Nuxt) { // Add plugin normalization plugin addBuildPlugin(RemovePluginMetadataPlugin(nuxt)) + // Add keys for useFetch, useAsyncData, etc. + addBuildPlugin(ComposableKeysPlugin({ + sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client, + rootDir: nuxt.options.rootDir, + composables: nuxt.options.optimization.keyedComposables, + })) + // shared folder import protection const sharedDir = withTrailingSlash(resolve(nuxt.options.rootDir, nuxt.options.dir.shared)) const relativeSharedDir = withTrailingSlash(relative(nuxt.options.rootDir, resolve(nuxt.options.rootDir, nuxt.options.dir.shared))) diff --git a/packages/vite/src/plugins/composable-keys.ts b/packages/nuxt/src/core/plugins/composable-keys.ts similarity index 98% rename from packages/vite/src/plugins/composable-keys.ts rename to packages/nuxt/src/core/plugins/composable-keys.ts index 14233469aa..01e11159fc 100644 --- a/packages/vite/src/plugins/composable-keys.ts +++ b/packages/nuxt/src/core/plugins/composable-keys.ts @@ -9,7 +9,7 @@ import type { CallExpression, Pattern } from 'estree' import { parseQuery, parseURL } from 'ufo' import escapeRE from 'escape-string-regexp' import { findStaticImports, parseStaticImport } from 'mlly' -import { matchWithStringOrRegex } from '../utils' +import { matchWithStringOrRegex } from '../utils/plugins' interface ComposableKeysOptions { sourcemap: boolean @@ -22,7 +22,7 @@ const NUXT_LIB_RE = /node_modules\/(?:nuxt|nuxt3|nuxt-nightly)\// const SUPPORTED_EXT_RE = /\.(?:m?[jt]sx?|vue)/ const SCRIPT_RE = /(?<=]*>)[\s\S]*?(?=<\/script>)/i -export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptions) => { +export const ComposableKeysPlugin = (options: ComposableKeysOptions) => createUnplugin(() => { const composableMeta: Record = {} const composableLengths = new Set() const keyedFunctions = new Set() diff --git a/packages/nuxt/src/core/utils/plugins.ts b/packages/nuxt/src/core/utils/plugins.ts index 1e80ddb07d..d07ea3254e 100644 --- a/packages/nuxt/src/core/utils/plugins.ts +++ b/packages/nuxt/src/core/utils/plugins.ts @@ -54,3 +54,13 @@ export function getLoader (id: string): 'vue' | 'ts' | 'tsx' | null { } return ext.endsWith('x') ? 'tsx' : 'ts' } + +export function matchWithStringOrRegex (value: string, matcher: string | RegExp) { + if (typeof matcher === 'string') { + return value === matcher + } else if (matcher instanceof RegExp) { + return matcher.test(value) + } + + return false +} diff --git a/packages/vite/test/composable-keys.test.ts b/packages/nuxt/test/composable-keys.test.ts similarity index 93% rename from packages/vite/test/composable-keys.test.ts rename to packages/nuxt/test/composable-keys.test.ts index 75299618e1..e5733d3cc0 100644 --- a/packages/vite/test/composable-keys.test.ts +++ b/packages/nuxt/test/composable-keys.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from 'vitest' -import { detectImportNames } from '../src/plugins/composable-keys' +import { detectImportNames } from '../src/core/plugins/composable-keys' describe('detectImportNames', () => { const keyedComposables = { diff --git a/packages/vite/src/utils/index.ts b/packages/vite/src/utils/index.ts index 9221d70278..eceac878d8 100644 --- a/packages/vite/src/utils/index.ts +++ b/packages/vite/src/utils/index.ts @@ -7,16 +7,6 @@ export function isCSS (file: string) { return IS_CSS_RE.test(file) } -export function matchWithStringOrRegex (value: string, matcher: string | RegExp) { - if (typeof matcher === 'string') { - return value === matcher - } else if (matcher instanceof RegExp) { - return matcher.test(value) - } - - return false -} - /** @since 3.9.0 */ export function toArray (value: T | T[]): T[] { return Array.isArray(value) ? value : [value] diff --git a/packages/vite/src/vite.ts b/packages/vite/src/vite.ts index 5500f4bed0..9cf99b7072 100644 --- a/packages/vite/src/vite.ts +++ b/packages/vite/src/vite.ts @@ -14,7 +14,6 @@ import { buildClient } from './client' import { buildServer } from './server' import { warmupViteServer } from './utils/warmup' import { resolveCSSOptions } from './css' -import { composableKeysPlugin } from './plugins/composable-keys' import { logLevelMap } from './utils/logger' import { ssrStylesPlugin } from './plugins/ssr-styles' import { VitePublicDirsPlugin } from './plugins/public-dirs' @@ -99,11 +98,6 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => { sourcemap: !!nuxt.options.sourcemap.server, baseURL: nuxt.options.app.baseURL, }), - composableKeysPlugin.vite({ - sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client, - rootDir: nuxt.options.rootDir, - composables: nuxt.options.optimization.keyedComposables, - }), replace({ preventAssignment: true, ...globalThisReplacements }), ], server: { diff --git a/packages/webpack/src/webpack.ts b/packages/webpack/src/webpack.ts index 69b4ffb80a..ccc8717031 100644 --- a/packages/webpack/src/webpack.ts +++ b/packages/webpack/src/webpack.ts @@ -11,7 +11,6 @@ import { joinURL } from 'ufo' import { logger, useNitro, useNuxt } from '@nuxt/kit' import type { InputPluginOption } from 'rollup' -import { composableKeysPlugin } from '../../vite/src/plugins/composable-keys' import { DynamicBasePlugin } from './plugins/dynamic-base' import { ChunkErrorPlugin } from './plugins/chunk' import { createMFS } from './utils/mfs' @@ -67,11 +66,6 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => { if (config.name === 'client' && nuxt.options.experimental.emitRouteChunkError && nuxt.options.builder !== '@nuxt/rspack-builder') { config.plugins!.push(new ChunkErrorPlugin()) } - config.plugins!.push(composableKeysPlugin.webpack({ - sourcemap: !!nuxt.options.sourcemap[config.name as 'client' | 'server'], - rootDir: nuxt.options.rootDir, - composables: nuxt.options.optimization.keyedComposables, - })) } await nuxt.callHook(`${builder}:configResolved`, webpackConfigs)