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/kit" />
/// <reference types="@nuxt/nitro" /> /// <reference types="@nuxt/nitro" />
/// <reference path=".nuxt/components.d.ts" /> /// <reference path=".nuxt/components.d.ts" />
/// <reference path=".nuxt/global-imports.d.ts" /> /// <reference path=".nuxt/auto-imports.d.ts" />
export {} export {}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,7 +1,7 @@
import { expect } from 'chai' 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: _transform } = TransformPlugin.raw({ ref: 'vue', computed: 'bar' }, {} as any)
const transform = (code: string) => _transform.call({} as any, code, '') const transform = (code: string) => _transform.call({} as any, code, '')

View File

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