mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-25 15:15:19 +00:00
refactor: rename global-imports
to auto-imports
(#706)
This commit is contained in:
parent
27666063ea
commit
bade471dcf
2
examples/with-components/nuxt.d.ts
vendored
2
examples/with-components/nuxt.d.ts
vendored
@ -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 {}
|
||||
|
@ -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)
|
||||
}
|
@ -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'))
|
||||
|
2
packages/bridge/types.d.ts
vendored
2
packages/bridge/types.d.ts
vendored
@ -7,7 +7,7 @@ export interface BridgeConfig {
|
||||
app: boolean | {}
|
||||
capi: boolean | {}
|
||||
scriptSetup: boolean
|
||||
globalImports: boolean
|
||||
autoImports: boolean
|
||||
transpile: boolean
|
||||
constraints: boolean
|
||||
postcss8: boolean
|
||||
|
@ -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') })
|
||||
})
|
||||
}
|
||||
})
|
@ -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)
|
@ -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[]
|
||||
}
|
@ -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)
|
||||
|
@ -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, '')
|
||||
|
2
playground/nuxt.d.ts
vendored
2
playground/nuxt.d.ts
vendored
@ -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 {}
|
||||
|
Loading…
Reference in New Issue
Block a user