refactor: move composable-keys plugin into nuxt core (#30029)

This commit is contained in:
Julien Huang 2024-11-25 11:06:53 +01:00 committed by GitHub
parent f68589c2ca
commit 7a1f1df088
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 21 additions and 25 deletions

View File

@ -40,6 +40,7 @@ import { initNitro } from './nitro'
import schemaModule from './schema' import schemaModule from './schema'
import { RemovePluginMetadataPlugin } from './plugins/plugin-metadata' import { RemovePluginMetadataPlugin } from './plugins/plugin-metadata'
import { AsyncContextInjectionPlugin } from './plugins/async-context' import { AsyncContextInjectionPlugin } from './plugins/async-context'
import { ComposableKeysPlugin } from './plugins/composable-keys'
import { resolveDeepImportsPlugin } from './plugins/resolve-deep-imports' import { resolveDeepImportsPlugin } from './plugins/resolve-deep-imports'
import { prehydrateTransformPlugin } from './plugins/prehydrate' import { prehydrateTransformPlugin } from './plugins/prehydrate'
import { VirtualFSPlugin } from './plugins/virtual' import { VirtualFSPlugin } from './plugins/virtual'
@ -247,6 +248,13 @@ async function initNuxt (nuxt: Nuxt) {
// Add plugin normalization plugin // Add plugin normalization plugin
addBuildPlugin(RemovePluginMetadataPlugin(nuxt)) 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 // shared folder import protection
const sharedDir = withTrailingSlash(resolve(nuxt.options.rootDir, nuxt.options.dir.shared)) 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))) const relativeSharedDir = withTrailingSlash(relative(nuxt.options.rootDir, resolve(nuxt.options.rootDir, nuxt.options.dir.shared)))

View File

@ -9,7 +9,7 @@ import type { CallExpression, Pattern } from 'estree'
import { parseQuery, parseURL } from 'ufo' import { parseQuery, parseURL } from 'ufo'
import escapeRE from 'escape-string-regexp' import escapeRE from 'escape-string-regexp'
import { findStaticImports, parseStaticImport } from 'mlly' import { findStaticImports, parseStaticImport } from 'mlly'
import { matchWithStringOrRegex } from '../utils' import { matchWithStringOrRegex } from '../utils/plugins'
interface ComposableKeysOptions { interface ComposableKeysOptions {
sourcemap: boolean 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 SUPPORTED_EXT_RE = /\.(?:m?[jt]sx?|vue)/
const SCRIPT_RE = /(?<=<script[^>]*>)[\s\S]*?(?=<\/script>)/i const SCRIPT_RE = /(?<=<script[^>]*>)[\s\S]*?(?=<\/script>)/i
export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptions) => { export const ComposableKeysPlugin = (options: ComposableKeysOptions) => createUnplugin(() => {
const composableMeta: Record<string, any> = {} const composableMeta: Record<string, any> = {}
const composableLengths = new Set<number>() const composableLengths = new Set<number>()
const keyedFunctions = new Set<string>() const keyedFunctions = new Set<string>()

View File

@ -54,3 +54,13 @@ export function getLoader (id: string): 'vue' | 'ts' | 'tsx' | null {
} }
return ext.endsWith('x') ? 'tsx' : 'ts' 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
}

View File

@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest' import { describe, expect, it } from 'vitest'
import { detectImportNames } from '../src/plugins/composable-keys' import { detectImportNames } from '../src/core/plugins/composable-keys'
describe('detectImportNames', () => { describe('detectImportNames', () => {
const keyedComposables = { const keyedComposables = {

View File

@ -7,16 +7,6 @@ export function isCSS (file: string) {
return IS_CSS_RE.test(file) 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 */ /** @since 3.9.0 */
export function toArray<T> (value: T | T[]): T[] { export function toArray<T> (value: T | T[]): T[] {
return Array.isArray(value) ? value : [value] return Array.isArray(value) ? value : [value]

View File

@ -14,7 +14,6 @@ import { buildClient } from './client'
import { buildServer } from './server' import { buildServer } from './server'
import { warmupViteServer } from './utils/warmup' import { warmupViteServer } from './utils/warmup'
import { resolveCSSOptions } from './css' import { resolveCSSOptions } from './css'
import { composableKeysPlugin } from './plugins/composable-keys'
import { logLevelMap } from './utils/logger' import { logLevelMap } from './utils/logger'
import { ssrStylesPlugin } from './plugins/ssr-styles' import { ssrStylesPlugin } from './plugins/ssr-styles'
import { VitePublicDirsPlugin } from './plugins/public-dirs' import { VitePublicDirsPlugin } from './plugins/public-dirs'
@ -99,11 +98,6 @@ export const bundle: NuxtBuilder['bundle'] = async (nuxt) => {
sourcemap: !!nuxt.options.sourcemap.server, sourcemap: !!nuxt.options.sourcemap.server,
baseURL: nuxt.options.app.baseURL, 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 }), replace({ preventAssignment: true, ...globalThisReplacements }),
], ],
server: { server: {

View File

@ -11,7 +11,6 @@ import { joinURL } from 'ufo'
import { logger, useNitro, useNuxt } from '@nuxt/kit' import { logger, useNitro, useNuxt } from '@nuxt/kit'
import type { InputPluginOption } from 'rollup' import type { InputPluginOption } from 'rollup'
import { composableKeysPlugin } from '../../vite/src/plugins/composable-keys'
import { DynamicBasePlugin } from './plugins/dynamic-base' import { DynamicBasePlugin } from './plugins/dynamic-base'
import { ChunkErrorPlugin } from './plugins/chunk' import { ChunkErrorPlugin } from './plugins/chunk'
import { createMFS } from './utils/mfs' 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') { if (config.name === 'client' && nuxt.options.experimental.emitRouteChunkError && nuxt.options.builder !== '@nuxt/rspack-builder') {
config.plugins!.push(new ChunkErrorPlugin()) 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) await nuxt.callHook(`${builder}:configResolved`, webpackConfigs)