fix(auto-imports): disable global mode and improve detection (#748)

This commit is contained in:
Anthony Fu 2021-10-12 20:24:43 +08:00 committed by GitHub
parent 7064728c21
commit edeafbf6e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View File

@ -12,7 +12,10 @@ export default defineNuxtModule<AutoImportsOptions>({
for (const key of disabled) {
delete identifiers[key]
}
if (nuxt.options.dev) {
// temporary disable #746
// eslint-disable-next-line no-constant-condition
if (nuxt.options.dev && false) {
// Add all imports to globalThis in development mode
addPluginTemplate({
filename: 'auto-imports.mjs',

View File

@ -4,13 +4,15 @@ import { IdentifierMap } from './types'
const excludeRE = [
// imported from other module
/\bimport\s*([\w_$]*?),?\s*\{([\s\S]*?)\}\s*from\b/g,
/\bimport\s*([\w_$]*?),?\s*(?:\{([\s\S]*?)\})?\s*from\b/g,
// defined as function
/\bfunction\s*([\s\S]+?)\s*\(/g,
/\bfunction\s*([\w_$]+?)\s*\(/g,
// defined as local variable
/\b(?:const|let|var)\s*(\{([\s\S]*?)\}|[\w\d_$]+?\b)/g
/\b(?:const|let|var)\s+?(\[[\s\S]*?\]|\{[\s\S]*?\}|[\s\S]+?)\s*?[=;\n]/g
]
const importAsRE = /^.*\sas\s+/
const seperatorRE = /[,[\]{}\n]/g
const multilineCommentsRE = /\/\*(.|[\r\n])*?\*\//gm
const singlelineCommentsRE = /^\s*\/\/.*$/gm
@ -54,8 +56,13 @@ export const TransformPlugin = createUnplugin((map: IdentifierMap) => {
// remove those already defined
for (const regex of excludeRE) {
Array.from(withoutComment.matchAll(regex))
.flatMap(i => [...(i[1]?.split(',') || []), ...(i[2]?.split(',') || [])])
.forEach(i => matched.delete(i.trim()))
.flatMap(i => [
...(i[1]?.split(seperatorRE) || []),
...(i[2]?.split(seperatorRE) || [])
])
.map(i => i.replace(importAsRE, '').trim())
.filter(Boolean)
.forEach(i => matched.delete(i))
}
if (!matched.size) {

View File

@ -6,13 +6,17 @@ describe('module:auto-imports:build', () => {
const transform = (code: string) => _transform.call({} as any, code, '')
it('should correct inject', async () => {
const result = await transform('const a = ref(0)')
expect(result).to.equal('import { ref } from \'vue\';const a = ref(0)')
expect(await transform('const a = ref(0)')).to.equal('import { ref } from \'vue\';const a = ref(0)')
expect(await transform('import { computed as ref } from "foo"; const a = ref(0)')).to.includes('import { computed } from \'bar\';')
})
it('should ignore imported', async () => {
const result = await transform('import { ref } from "foo";const a = ref(0)')
expect(result).to.equal(null)
it('should ignore existing imported', async () => {
expect(await transform('import { ref } from "foo"; const a = ref(0)')).to.equal(null)
expect(await transform('import ref from "foo"; const a = ref(0)')).to.equal(null)
expect(await transform('import { z as ref } from "foo"; const a = ref(0)')).to.equal(null)
expect(await transform('let ref = () => {}; const a = ref(0)')).to.equal(null)
expect(await transform('let { ref } = Vue; const a = ref(0)')).to.equal(null)
expect(await transform('let [\ncomputed,\nref\n] = Vue; const a = ref(0); const b = ref(0)')).to.equal(null)
})
it('should ignore comments', async () => {