feat(auto-import): allow explicit transform exclusion patterns (#2183)

Co-authored-by: pooya parsa <pyapar@gmail.com>
This commit is contained in:
Yasser Lahbibi 2021-12-21 15:28:45 +01:00 committed by GitHub
parent 2d3f61d478
commit ca9761df9e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 8 deletions

View File

@ -3,14 +3,20 @@ import type { AutoImport } from '@nuxt/schema'
export interface AutoImportContext {
autoImports: AutoImport[]
matchRE: RegExp
transform: {
exclude: RegExp[]
}
map: Map<string, AutoImport>
}
export function createAutoImportContext (): AutoImportContext {
export function createAutoImportContext (opts): AutoImportContext {
return {
autoImports: [],
map: new Map(),
matchRE: /__never__/
matchRE: /__never__/,
transform: {
exclude: opts.transform.exclude || [/node_modules/]
}
}
}

View File

@ -13,7 +13,10 @@ export default defineNuxtModule<AutoImportsOptions>({
defaults: {
sources: Nuxt3AutoImports,
global: false,
dirs: []
dirs: [],
transform: {
exclude: undefined
}
},
async setup (options, nuxt) {
// Allow modules extending sources
@ -23,7 +26,7 @@ export default defineNuxtModule<AutoImportsOptions>({
options.sources = options.sources.filter(source => source.disabled !== true)
// Create a context to share state between module internals
const ctx = createAutoImportContext()
const ctx = createAutoImportContext(options)
// Resolve autoimports from sources
for (const source of options.sources) {

View File

@ -39,7 +39,8 @@ export const TransformPlugin = createUnplugin((ctx: AutoImportContext) => {
const { pathname, search } = parseURL(id)
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
}

View File

@ -11,10 +11,17 @@ import { Nuxt3AutoImports } from '../src/auto-imports/imports'
describe('auto-imports:transform', () => {
const autoImports: AutoImport[] = [
{ 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)
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)')
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']

View File

@ -42,14 +42,25 @@ export interface AutoImportsOptions {
* Auto import sources
*/
sources?: AutoImportSource[]
/**
* [experimental] Use globalThis injection instead of transform for development
*/
global?: boolean
/**
* Additional directories to scan composables from
*
* 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[]
}
}