mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 23:22:02 +00:00
feat(auto-import): allow explicit transform exclusion patterns (#2183)
Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
parent
2d3f61d478
commit
ca9761df9e
@ -3,14 +3,20 @@ import type { AutoImport } from '@nuxt/schema'
|
|||||||
export interface AutoImportContext {
|
export interface AutoImportContext {
|
||||||
autoImports: AutoImport[]
|
autoImports: AutoImport[]
|
||||||
matchRE: RegExp
|
matchRE: RegExp
|
||||||
|
transform: {
|
||||||
|
exclude: RegExp[]
|
||||||
|
}
|
||||||
map: Map<string, AutoImport>
|
map: Map<string, AutoImport>
|
||||||
}
|
}
|
||||||
|
|
||||||
export function createAutoImportContext (): AutoImportContext {
|
export function createAutoImportContext (opts): AutoImportContext {
|
||||||
return {
|
return {
|
||||||
autoImports: [],
|
autoImports: [],
|
||||||
map: new Map(),
|
map: new Map(),
|
||||||
matchRE: /__never__/
|
matchRE: /__never__/,
|
||||||
|
transform: {
|
||||||
|
exclude: opts.transform.exclude || [/node_modules/]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,10 @@ export default defineNuxtModule<AutoImportsOptions>({
|
|||||||
defaults: {
|
defaults: {
|
||||||
sources: Nuxt3AutoImports,
|
sources: Nuxt3AutoImports,
|
||||||
global: false,
|
global: false,
|
||||||
dirs: []
|
dirs: [],
|
||||||
|
transform: {
|
||||||
|
exclude: undefined
|
||||||
|
}
|
||||||
},
|
},
|
||||||
async setup (options, nuxt) {
|
async setup (options, nuxt) {
|
||||||
// Allow modules extending sources
|
// Allow modules extending sources
|
||||||
@ -23,7 +26,7 @@ export default defineNuxtModule<AutoImportsOptions>({
|
|||||||
options.sources = options.sources.filter(source => source.disabled !== true)
|
options.sources = options.sources.filter(source => source.disabled !== true)
|
||||||
|
|
||||||
// Create a context to share state between module internals
|
// Create a context to share state between module internals
|
||||||
const ctx = createAutoImportContext()
|
const ctx = createAutoImportContext(options)
|
||||||
|
|
||||||
// Resolve autoimports from sources
|
// Resolve autoimports from sources
|
||||||
for (const source of options.sources) {
|
for (const source of options.sources) {
|
||||||
|
@ -39,7 +39,8 @@ export const TransformPlugin = createUnplugin((ctx: AutoImportContext) => {
|
|||||||
const { pathname, search } = parseURL(id)
|
const { pathname, search } = parseURL(id)
|
||||||
const { type } = parseQuery(search)
|
const { type } = parseQuery(search)
|
||||||
|
|
||||||
if (id.includes('node_modules')) {
|
// Exclude node_modules by default
|
||||||
|
if (ctx.transform.exclude.some(pattern => id.match(pattern))) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,10 +11,17 @@ import { Nuxt3AutoImports } from '../src/auto-imports/imports'
|
|||||||
describe('auto-imports:transform', () => {
|
describe('auto-imports:transform', () => {
|
||||||
const autoImports: AutoImport[] = [
|
const autoImports: AutoImport[] = [
|
||||||
{ name: 'ref', as: 'ref', from: 'vue' },
|
{ name: 'ref', as: 'ref', from: 'vue' },
|
||||||
{ name: 'computed', as: 'computed', from: 'bar' }
|
{ name: 'computed', as: 'computed', from: 'bar' },
|
||||||
|
{ name: 'foo', as: 'foo', from: 'excluded' }
|
||||||
]
|
]
|
||||||
|
|
||||||
const ctx = { autoImports, map: new Map() } as AutoImportContext
|
const ctx = {
|
||||||
|
autoImports,
|
||||||
|
map: new Map(),
|
||||||
|
transform: {
|
||||||
|
exclude: [/excluded/]
|
||||||
|
}
|
||||||
|
} as AutoImportContext
|
||||||
updateAutoImportContext(ctx)
|
updateAutoImportContext(ctx)
|
||||||
|
|
||||||
const transformPlugin = TransformPlugin.raw(ctx, { framework: 'rollup' })
|
const transformPlugin = TransformPlugin.raw(ctx, { framework: 'rollup' })
|
||||||
@ -38,6 +45,10 @@ describe('auto-imports:transform', () => {
|
|||||||
const result = await transform('// import { computed } from "foo"\n;const a = computed(0)')
|
const result = await transform('// import { computed } from "foo"\n;const a = computed(0)')
|
||||||
expect(result).to.equal('import { computed } from \'bar\';// import { computed } from "foo"\n;const a = computed(0)')
|
expect(result).to.equal('import { computed } from \'bar\';// import { computed } from "foo"\n;const a = computed(0)')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should exclude files from transform', async () => {
|
||||||
|
expect(await transform('const a = foo()')).to.not.include('import { foo } from "excluded"')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
const excludedNuxtHelpers = ['useHydration']
|
const excludedNuxtHelpers = ['useHydration']
|
||||||
|
@ -42,14 +42,25 @@ export interface AutoImportsOptions {
|
|||||||
* Auto import sources
|
* Auto import sources
|
||||||
*/
|
*/
|
||||||
sources?: AutoImportSource[]
|
sources?: AutoImportSource[]
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [experimental] Use globalThis injection instead of transform for development
|
* [experimental] Use globalThis injection instead of transform for development
|
||||||
*/
|
*/
|
||||||
global?: boolean
|
global?: boolean
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Additional directories to scan composables from
|
* Additional directories to scan composables from
|
||||||
*
|
*
|
||||||
* By default <rootDir>/composables is added
|
* By default <rootDir>/composables is added
|
||||||
*/
|
*/
|
||||||
dirs?: string[]
|
dirs?: string[]
|
||||||
|
|
||||||
|
transform: {
|
||||||
|
/**
|
||||||
|
* Exclusion patterns for transforming files
|
||||||
|
*
|
||||||
|
* By default [/node_modules/] will be used
|
||||||
|
*/
|
||||||
|
exclude?: RegExp[]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user