refactor: rename global-imports to auto-imports (#706)

This commit is contained in:
Anthony Fu 2021-10-11 16:07:27 +08:00 committed by GitHub
parent 27666063ea
commit bade471dcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 27 additions and 27 deletions

View File

@ -5,5 +5,5 @@
/// <reference types="@nuxt/kit" />
/// <reference types="@nuxt/nitro" />
/// <reference path=".nuxt/components.d.ts" />
/// <reference path=".nuxt/global-imports.d.ts" />
/// <reference path=".nuxt/auto-imports.d.ts" />
export {}

View File

@ -1,5 +1,5 @@
import { installModule, useNuxt } from '@nuxt/kit'
import globalImports from '../../nuxt3/src/global-imports/module'
import autoImports from '../../nuxt3/src/auto-imports/module'
// TODO: implement these: https://github.com/nuxt/framework/issues/549
const disabled = [
@ -71,10 +71,10 @@ for (const pkg in identifiers) {
}
}
export async function setupGlobalImports () {
export async function setupAutoImports () {
const nuxt = useNuxt()
nuxt.options.globalImports = nuxt.options.globalImports || {}
nuxt.options.globalImports.disabled = nuxt.options.globalImports.disabled || disabled
nuxt.options.globalImports.identifiers = Object.assign({}, defaultIdentifiers, nuxt.options.globalImports.identifiers)
await installModule(nuxt, globalImports)
nuxt.options.autoImports = nuxt.options.autoImports || {}
nuxt.options.autoImports.disabled = nuxt.options.autoImports.disabled || disabled
nuxt.options.autoImports.identifiers = Object.assign({}, defaultIdentifiers, nuxt.options.autoImports.identifiers)
await installModule(nuxt, autoImports)
}

View File

@ -5,7 +5,7 @@ import { setupNitroBridge } from './nitro'
import { setupAppBridge } from './app'
import { setupCAPIBridge } from './capi'
import { setupBetterResolve } from './resolve'
import { setupGlobalImports } from './global-imports'
import { setupAutoImports } from './auto-imports'
import { setupTypescript } from './typescript'
import { setupMeta } from './meta'
import { setupTranspile } from './transpile'
@ -21,7 +21,7 @@ export default defineNuxtModule({
capi: {},
transpile: true,
scriptSetup: true,
globalImports: true,
autoImports: true,
constraints: true,
meta: null,
// TODO: Remove from 2.16
@ -47,8 +47,8 @@ export default defineNuxtModule({
if (opts.scriptSetup) {
await setupScriptSetup()
}
if (opts.globalImports) {
await setupGlobalImports()
if (opts.autoImports) {
await setupAutoImports()
}
if (opts.vite) {
await installModule(nuxt, _require.resolve('nuxt-vite'))

View File

@ -7,7 +7,7 @@ export interface BridgeConfig {
app: boolean | {}
capi: boolean | {}
scriptSetup: boolean
globalImports: boolean
autoImports: boolean
transpile: boolean
constraints: boolean
postcss8: boolean

View File

@ -1,12 +1,12 @@
import { addVitePlugin, addWebpackPlugin, defineNuxtModule, addTemplate, resolveAlias, addPluginTemplate } from '@nuxt/kit'
import { isAbsolute, relative, resolve } from 'pathe'
import type { Identifiers, GlobalImportsOptions } from './types'
import type { Identifiers, AutoImportsOptions } from './types'
import { TransformPlugin } from './transform'
import { defaultIdentifiers } from './identifiers'
export default defineNuxtModule<GlobalImportsOptions>({
name: 'global-imports',
configKey: 'globalImports',
export default defineNuxtModule<AutoImportsOptions>({
name: 'auto-imports',
configKey: 'autoImports',
defaults: { identifiers: defaultIdentifiers },
setup ({ disabled = [], identifiers }, nuxt) {
for (const key of disabled) {
@ -15,7 +15,7 @@ export default defineNuxtModule<GlobalImportsOptions>({
if (nuxt.options.dev) {
// Add all imports to globalThis in development mode
addPluginTemplate({
filename: 'global-imports.mjs',
filename: 'auto-imports.mjs',
src: '',
getContents: () => {
const imports = toImports(Object.entries(identifiers))
@ -44,15 +44,15 @@ export default defineNuxtModule<GlobalImportsOptions>({
}
addTemplate({
filename: 'global-imports.d.ts',
filename: 'auto-imports.d.ts',
write: true,
getContents: () => `// Generated by global imports
getContents: () => `// Generated by auto imports
declare global {
${Object.entries(identifiers).map(([api, moduleName]) => ` const ${api}: typeof import('${r(moduleName)}')['${api}']`).join('\n')}
}\nexport {}`
})
nuxt.hook('prepare:types', ({ references }) => {
references.push({ path: resolve(nuxt.options.buildDir, 'global-imports.d.ts') })
references.push({ path: resolve(nuxt.options.buildDir, 'auto-imports.d.ts') })
})
}
})

View File

@ -24,7 +24,7 @@ export const TransformPlugin = createUnplugin((map: IdentifierMap) => {
const matchRE = new RegExp(`\\b(${Object.keys(map).join('|')})\\b`, 'g')
return {
name: 'nuxt-global-imports-transform',
name: 'nuxt-auto-imports-transform',
enforce: 'post',
transformInclude (id) {
const { pathname, search } = parseURL(id)

View File

@ -1,7 +1,7 @@
export type IdentifierMap = Record<string, string>
export type Identifiers = [string, string][]
export interface GlobalImportsOptions {
export interface AutoImportsOptions {
identifiers?: IdentifierMap
disabled?: string[]
}

View File

@ -4,7 +4,7 @@ import { loadNuxtConfig, LoadNuxtOptions, Nuxt, NuxtOptions, NuxtConfig, nuxtCtx
import pagesModule from '../pages/module'
import metaModule from '../meta/module'
import componentsModule from '../components/module'
import globalImportsModule from '../global-imports/module'
import autoImportsModule from '../auto-imports/module'
import { distDir, pkgDir } from '../dirs'
import { version } from '../../package.json'
import { initNitro } from './nitro'
@ -69,7 +69,7 @@ export async function loadNuxt (opts: LoadNuxtOptions): Promise<Nuxt> {
// Temp
options.appDir = options.alias['#app'] = resolve(distDir, 'app')
options._majorVersion = 3
options.buildModules.push(pagesModule, metaModule, componentsModule, globalImportsModule)
options.buildModules.push(pagesModule, metaModule, componentsModule, autoImportsModule)
options.modulesDir.push(resolve(pkgDir, 'node_modules'))
const nuxt = createNuxt(options)

View File

@ -1,7 +1,7 @@
import { expect } from 'chai'
import { TransformPlugin } from '../src/global-imports/transform'
import { TransformPlugin } from '../src/auto-imports/transform'
describe('module:global-imports:build', () => {
describe('module:auto-imports:build', () => {
const { transform: _transform } = TransformPlugin.raw({ ref: 'vue', computed: 'bar' }, {} as any)
const transform = (code: string) => _transform.call({} as any, code, '')

View File

@ -3,5 +3,5 @@
/// <reference types="nuxt3" />
/// <reference path=".nuxt/components.d.ts" />
/// <reference path=".nuxt/global-imports.d.ts" />
/// <reference path=".nuxt/auto-imports.d.ts" />
export {}