fix(nuxt): ensure we process files in buildDir for unimport (#28899)

This commit is contained in:
Daniel Roe 2024-09-09 18:23:36 +02:00
parent 08a0c90828
commit f94a2bf850
No known key found for this signature in database
GPG Key ID: CBC814C393D93268
3 changed files with 10 additions and 8 deletions

View File

@ -1,9 +1,10 @@
import { existsSync } from 'node:fs' import { existsSync } from 'node:fs'
import { addTemplate, addTypeTemplate, addVitePlugin, addWebpackPlugin, defineNuxtModule, isIgnored, logger, resolveAlias, tryResolveModule, updateTemplates, useNuxt } from '@nuxt/kit' import { addBuildPlugin, addTemplate, addTypeTemplate, defineNuxtModule, isIgnored, logger, resolveAlias, tryResolveModule, updateTemplates, useNuxt } from '@nuxt/kit'
import { isAbsolute, join, normalize, relative, resolve } from 'pathe' import { isAbsolute, join, normalize, relative, resolve } from 'pathe'
import type { Import, Unimport } from 'unimport' import type { Import, Unimport } from 'unimport'
import { createUnimport, scanDirExports, toExports } from 'unimport' import { createUnimport, scanDirExports, toExports } from 'unimport'
import type { ImportPresetWithDeprecation, ImportsOptions, ResolvedNuxtTemplate } from 'nuxt/schema' import type { ImportPresetWithDeprecation, ImportsOptions, ResolvedNuxtTemplate } from 'nuxt/schema'
import escapeRE from 'escape-string-regexp'
import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly' import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
import { isDirectory } from '../utils' import { isDirectory } from '../utils'
@ -15,7 +16,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
name: 'imports', name: 'imports',
configKey: 'imports', configKey: 'imports',
}, },
defaults: { defaults: nuxt => ({
autoImport: true, autoImport: true,
scan: true, scan: true,
presets: defaultPresets, presets: defaultPresets,
@ -23,11 +24,13 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
imports: [], imports: [],
dirs: [], dirs: [],
transform: { transform: {
include: [], include: [
new RegExp('^' + escapeRE(nuxt.options.buildDir)),
],
exclude: undefined, exclude: undefined,
}, },
virtualImports: ['#imports'], virtualImports: ['#imports'],
}, }),
async setup (options, nuxt) { async setup (options, nuxt) {
// TODO: fix sharing of defaults between invocations of modules // TODO: fix sharing of defaults between invocations of modules
const presets = JSON.parse(JSON.stringify(options.presets)) as ImportPresetWithDeprecation[] const presets = JSON.parse(JSON.stringify(options.presets)) as ImportPresetWithDeprecation[]
@ -92,8 +95,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
nuxt.options.alias['#imports'] = join(nuxt.options.buildDir, 'imports') nuxt.options.alias['#imports'] = join(nuxt.options.buildDir, 'imports')
// Transform to inject imports in production mode // Transform to inject imports in production mode
addVitePlugin(() => TransformPlugin.vite({ ctx, options, sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client })) addBuildPlugin(TransformPlugin({ ctx, options, sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client }))
addWebpackPlugin(() => TransformPlugin.webpack({ ctx, options, sourcemap: !!nuxt.options.sourcemap.server || !!nuxt.options.sourcemap.client }))
const priorities = nuxt.options._layers.map((layer, i) => [layer.config.srcDir, -i] as const).sort(([a], [b]) => b.length - a.length) const priorities = nuxt.options._layers.map((layer, i) => [layer.config.srcDir, -i] as const).sort(([a], [b]) => b.length - a.length)

View File

@ -8,7 +8,7 @@ import { isJS, isVue } from '../core/utils'
const NODE_MODULES_RE = /[\\/]node_modules[\\/]/ const NODE_MODULES_RE = /[\\/]node_modules[\\/]/
const IMPORTS_RE = /(['"])#imports\1/ const IMPORTS_RE = /(['"])#imports\1/
export const TransformPlugin = createUnplugin(({ ctx, options, sourcemap }: { ctx: Unimport, options: Partial<ImportsOptions>, sourcemap?: boolean }) => { export const TransformPlugin = ({ ctx, options, sourcemap }: { ctx: Unimport, options: Partial<ImportsOptions>, sourcemap?: boolean }) => createUnplugin(() => {
return { return {
name: 'nuxt:imports-transform', name: 'nuxt:imports-transform',
enforce: 'post', enforce: 'post',

View File

@ -22,7 +22,7 @@ describe('imports:transform', () => {
imports, imports,
}) })
const transformPlugin = TransformPlugin.raw({ ctx, options: { transform: { exclude: [/node_modules/] } } }, { framework: 'rollup' }) as Plugin const transformPlugin = TransformPlugin({ ctx, options: { transform: { exclude: [/node_modules/] } } }).raw({}, { framework: 'rollup' }) as Plugin
const transform = async (source: string) => { const transform = async (source: string) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
const result = await (transformPlugin.transform! as Function).call({ error: null, warn: null } as any, source, '') const result = await (transformPlugin.transform! as Function).call({ error: null, warn: null } as any, source, '')