mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-11 08:33:53 +00:00
fix(auto-imports): disable global mode and improve detection (#748)
This commit is contained in:
parent
7064728c21
commit
edeafbf6e3
@ -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',
|
||||
|
@ -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) {
|
||||
|
@ -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 () => {
|
||||
|
Loading…
Reference in New Issue
Block a user