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 { 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 type { Import, Unimport } from 'unimport'
import { createUnimport, scanDirExports, toExports } from 'unimport'
import type { ImportPresetWithDeprecation, ImportsOptions, ResolvedNuxtTemplate } from 'nuxt/schema'
import escapeRE from 'escape-string-regexp'
import { lookupNodeModuleSubpath, parseNodeModulePath } from 'mlly'
import { isDirectory } from '../utils'
@ -15,7 +16,7 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
name: 'imports',
configKey: 'imports',
},
defaults: {
defaults: nuxt => ({
autoImport: true,
scan: true,
presets: defaultPresets,
@ -23,11 +24,13 @@ export default defineNuxtModule<Partial<ImportsOptions>>({
imports: [],
dirs: [],
transform: {
include: [],
include: [
new RegExp('^' + escapeRE(nuxt.options.buildDir)),
],
exclude: undefined,
},
virtualImports: ['#imports'],
},
}),
async setup (options, nuxt) {
// TODO: fix sharing of defaults between invocations of modules
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')
// Transform to inject imports in production mode
addVitePlugin(() => TransformPlugin.vite({ 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 }))
addBuildPlugin(TransformPlugin({ 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)

View File

@ -8,7 +8,7 @@ import { isJS, isVue } from '../core/utils'
const NODE_MODULES_RE = /[\\/]node_modules[\\/]/
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 {
name: 'nuxt:imports-transform',
enforce: 'post',

View File

@ -22,7 +22,7 @@ describe('imports:transform', () => {
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) => {
// 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, '')