Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
refactor!: remove deprecated api (#9029)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Nov 16, 2022
1 parent ab7ec78 commit 5ac9d85
Show file tree
Hide file tree
Showing 35 changed files with 65 additions and 294 deletions.
58 changes: 23 additions & 35 deletions docs/content/1.getting-started/5.seo-meta.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default defineNuxtConfig({
app: {
head: {
charset: 'utf-16',
viewport: 'width=500, initial-scale=1',
viewport: 'width=500, initial-scale=1',
title: 'My App',
meta: [
// <meta name="description" content="My amazing site">
Expand Down Expand Up @@ -72,7 +72,7 @@ useHead({

## Components

Nuxt provides `<Title>`, `<Base>`, `<Script>`, `<NoScript>`, `<Style>`, `<Meta>`, `<Link>`, `<Body>`, `<Html>` and `<Head>` components so that you can interact directly with your metadata within your component's template.
Nuxt provides `<Title>`, `<Base>`, `<NoScript>`, `<Style>`, `<Meta>`, `<Link>`, `<Body>`, `<Html>` and `<Head>` components so that you can interact directly with your metadata within your component's template.

Because these component names match native HTML elements, it is very important that they are capitalized in the template.

Expand Down Expand Up @@ -134,7 +134,7 @@ It's recommended to use computed getters (`() => {}`) over computed (`computed((
```vue [useHead]
<script setup lang="ts">
const desc = ref('My amazing site.')
useHead({
meta: [
{ name: 'description', content: desc }
Expand Down Expand Up @@ -186,30 +186,18 @@ You can use the `body: true` option on the `link` and `script` meta tags to appe

For example:

::code-group

```vue [useHead]
<script setup lang="ts">
useHead({
script: [
{
src: 'https://third-party-script.com',
body: true
}
]
})
</script>
```

```vue [Components]
<template>
<div>
<Script src="https://third-party-script.com" body="true" />
</div>
</template>
```

::
```vue
<script setup lang="ts">
useHead({
script: [
{
src: 'https://third-party-script.com',
body: true
}
]
})
</script>
```

## Examples

Expand Down Expand Up @@ -254,7 +242,7 @@ In the example below, `titleTemplate` is set either as a string with the `%s` pl
// as a string,
// where `%s` is replaced with the title
titleTemplate: '%s - Site Title',
// ... or as a function
// ... or as a function
titleTemplate: (productCategory) => {
return productCategory
? `${productCategory} - Site Title`
Expand All @@ -273,17 +261,17 @@ The example below inserts Google Fonts using the `link` property of the `useHead
::code-group

```vue [useHead]
<script setup lang="ts">
<script setup lang="ts">
useHead({
link: [
{
rel: 'preconnect',
{
rel: 'preconnect',
href: 'https://fonts.googleapis.com'
},
{
rel: 'stylesheet',
href: 'https://fonts.googleapis.com/css2?family=Roboto&display=swap',
crossorigin: ''
{
rel: 'stylesheet',
href: 'https://fonts.googleapis.com/css2?family=Roboto&display=swap',
crossorigin: ''
}
]
})
Expand Down
4 changes: 2 additions & 2 deletions docs/content/3.api/4.advanced/2.kit.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ description: Nuxt Kit provides composable utilities to help interacting with Nux
[source code](https://github.com/nuxt/framework/blob/main/packages/kit/src/imports.ts)

- `addImports(imports)`
- `addImportsDir(autoImportDirs)`
- `addImportsSources(autoImportSources)`
- `addImportsDir(importDirs)`
- `addImportsSources(importSources)`

### Components

Expand Down
1 change: 0 additions & 1 deletion examples/composables/use-head/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<Title>Luck number: {{ dynamic }}</Title>
<Meta name="description" :content="`My page's ${dynamic} description`" />
<Link rel="preload" href="/test.txt" as="script" />
<Script>console.log("hello script");</Script>
</Head>
</Html>

Expand Down
14 changes: 0 additions & 14 deletions packages/kit/src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ export interface ExtendConfigOptions {
}

export interface ExtendWebpackConfigOptions extends ExtendConfigOptions {
/**
* Install plugin on modern build
*
* @default true
* @deprecated Nuxt 2 only
*/
modern?: boolean
}

export interface ExtendViteConfigOptions extends ExtendConfigOptions {}
Expand Down Expand Up @@ -73,13 +66,6 @@ export function extendWebpackConfig (
fn(config)
}
}
// Nuxt 2 backwards compatibility
if (options.modern !== false) {
const config = configs.find(i => i.name === 'modern')
if (config) {
fn(config)
}
}
})
}

Expand Down
8 changes: 0 additions & 8 deletions packages/kit/src/components.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { pascalCase, kebabCase } from 'scule'
import type { ComponentsDir, Component } from '@nuxt/schema'
import { genDynamicImport } from 'knitwork'
import { useNuxt } from './context'
import { assertNuxtCompatibility } from './compatibility'

Expand Down Expand Up @@ -40,14 +39,7 @@ export async function addComponent (opts: AddComponentOptions) {
prefetch: false,
preload: false,
mode: 'all',

// Nuxt 2 support
shortPath: opts.filePath,
async: false,
level: 0,
asyncImport: `${genDynamicImport(opts.filePath)}.then(r => r['${opts.export || 'default'}'])`,
import: `require(${JSON.stringify(opts.filePath)})['${opts.export || 'default'}']`,

...opts
}

Expand Down
26 changes: 6 additions & 20 deletions packages/kit/src/imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,26 @@ import { assertNuxtCompatibility } from './compatibility'
export function addImports (imports: Import | Import[]) {
assertNuxtCompatibility({ bridge: true })

// TODO: Use imports:* when widely adopted
useNuxt().hook('autoImports:extend', (_imports) => {
useNuxt().hook('imports:extend', (_imports) => {
_imports.push(...(Array.isArray(imports) ? imports : [imports]))
}, { allowDeprecated: true })
})
}

/**
* @deprecated Please use `addImports` instead with nuxt>=3.0.0-rc.9
*/
export const addAutoImport = addImports

export function addImportsDir (dirs: string | string[]) {
assertNuxtCompatibility({ bridge: true })

// TODO: Use imports:* when widely adopted
useNuxt().hook('autoImports:dirs', (_dirs: string[]) => {
useNuxt().hook('imports:dirs', (_dirs: string[]) => {
for (const dir of (Array.isArray(dirs) ? dirs : [dirs])) {
_dirs.push(dir)
}
}, { allowDeprecated: true })
})
}

/**
* @deprecated Please use `addImportsDir` instead with nuxt>=3.0.0-rc.9
*/
export const addAutoImportDir = addImportsDir

export function addImportsSources (presets: ImportPresetWithDeprecation | ImportPresetWithDeprecation[]) {
assertNuxtCompatibility({ bridge: true })

// TODO: Use imports:* when widely adopted
useNuxt().hook('autoImports:sources', (_presets: ImportPresetWithDeprecation[]) => {
useNuxt().hook('imports:sources', (_presets: ImportPresetWithDeprecation[]) => {
for (const preset of (Array.isArray(presets) ? presets : [presets])) {
_presets.push(preset)
}
}, { allowDeprecated: true })
})
}
17 changes: 12 additions & 5 deletions packages/kit/src/internal/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import jiti from 'jiti'
// TODO: use create-require for jest environment
const _require = jiti(process.cwd(), { interopDefault: true, esmResolve: true })

/** @deprecated Do not use CJS utils */
export interface ResolveModuleOptions {
paths?: string | string[]
}

/** @deprecated Do not use CJS utils */
export interface RequireModuleOptions extends ResolveModuleOptions {
// TODO: use create-require for jest environment
// native?: boolean
Expand All @@ -20,11 +22,13 @@ export interface RequireModuleOptions extends ResolveModuleOptions {
interopDefault?: boolean
}

/** @deprecated Do not use CJS utils */
export function isNodeModules (id: string) {
// TODO: Follow symlinks
return /[/\\]node_modules[/\\]/.test(id)
}

/** @deprecated Do not use CJS utils */
export function clearRequireCache (id: string) {
if (isNodeModules(id)) {
return
Expand All @@ -48,6 +52,7 @@ export function clearRequireCache (id: string) {
delete _require.cache[id]
}

/** @deprecated Do not use CJS utils */
export function scanRequireTree (id: string, files = new Set<string>()) {
if (isNodeModules(id) || files.has(id)) {
return files
Expand All @@ -69,7 +74,7 @@ export function scanRequireTree (id: string, files = new Set<string>()) {
return files
}

/** Access the require cache by module id. */
/** @deprecated Do not use CJS utils */
export function getRequireCacheItem (id: string) {
try {
return _require.cache[id]
Expand All @@ -82,7 +87,7 @@ export function requireModulePkg (id: string, opts: RequireModuleOptions = {}) {
return requireModule(join(id, 'package.json'), opts)
}

/** Resolve the path of a module. */
/** @deprecated Do not use CJS utils */
export function resolveModule (id: string, opts: ResolveModuleOptions = {}) {
return normalize(_require.resolve(id, {
paths: ([] as string[]).concat(
Expand All @@ -96,7 +101,7 @@ export function resolveModule (id: string, opts: ResolveModuleOptions = {}) {
}))
}

/** Try to resolve the path of a module, but don't emit an error if it can't be found. */
/** @deprecated Do not use CJS utils */
export function tryResolveModule (path: string, opts: ResolveModuleOptions = {}): string | null {
try {
return resolveModule(path, opts)
Expand All @@ -108,7 +113,7 @@ export function tryResolveModule (path: string, opts: ResolveModuleOptions = {})
return null
}

/** Require a module and return it. */
/** @deprecated Do not use CJS utils */
export function requireModule (id: string, opts: RequireModuleOptions = {}) {
// Resolve id
const resolvedPath = resolveModule(id, opts)
Expand All @@ -124,6 +129,7 @@ export function requireModule (id: string, opts: RequireModuleOptions = {}) {
return requiredModule
}

/** @deprecated Do not use CJS utils */
export function importModule (id: string, opts: RequireModuleOptions = {}) {
const resolvedPath = resolveModule(id, opts)
if (opts.interopDefault !== false) {
Expand All @@ -132,13 +138,14 @@ export function importModule (id: string, opts: RequireModuleOptions = {}) {
return import(pathToFileURL(resolvedPath).href)
}

/** @deprecated Do not use CJS utils */
export function tryImportModule (id: string, opts: RequireModuleOptions = {}) {
try {
return importModule(id, opts).catch(() => undefined)
} catch { }
}

/** Try to require a module, but don't emit an error if the module can't be required. */
/** @deprecated Do not use CJS utils */
export function tryRequireModule (id: string, opts: RequireModuleOptions = {}) {
try {
return requireModule(id, opts)
Expand Down
1 change: 1 addition & 0 deletions packages/kit/src/internal/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { genSafeVariableName, genDynamicImport, genImport } from 'knitwork'

import type { NuxtTemplate } from '@nuxt/schema'

/** @deprecated */
export async function compileTemplate (template: NuxtTemplate, ctx: any) {
const data = { ...ctx, options: template.options }
if (template.src) {
Expand Down
14 changes: 2 additions & 12 deletions packages/kit/src/module/define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,10 @@ import { templateUtils, compileTemplate } from '../internal/template'
* any hooks that are provided, and calling an optional setup function for full control.
*/
export function defineNuxtModule<OptionsT extends ModuleOptions> (definition: ModuleDefinition<OptionsT>): NuxtModule<OptionsT> {
// Legacy format. TODO: Remove in RC
if (typeof definition === 'function') {
// @ts-ignore
definition = definition(useNuxt())
logger.warn('Module definition as function is deprecated and will be removed in the future versions', definition)
}

// Normalize definition and meta
if (!definition.meta) { definition.meta = {} }
if (!definition.meta.configKey) {
// @ts-ignore TODO: Remove non-meta fallbacks in RC
definition.meta.name = definition.meta.name || definition.name
// @ts-ignore
definition.meta.configKey = definition.configKey || definition.meta.name
if (definition.meta.configKey === undefined) {
definition.meta.configKey = definition.meta.name
}

// Resolves module options from inline options, [configKey] in nuxt.config, defaults and schema
Expand Down
8 changes: 0 additions & 8 deletions packages/kit/src/module/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ export async function installModule (moduleToInstall: string | NuxtModule, _inli
async function normalizeModule (nuxtModule: string | NuxtModule, inlineOptions?: any) {
const nuxt = useNuxt()

// Detect if `installModule` used with older signuture (nuxt, nuxtModule)
// TODO: Remove in RC
// @ts-ignore
if (nuxtModule?._version || nuxtModule?.version || nuxtModule?.constructor?.version || '') {
[nuxtModule, inlineOptions] = [inlineOptions, {}]
console.warn(new Error('`installModule` is being called with old signature!'))
}

// Import if input is string
if (typeof nuxtModule === 'string') {
const _src = resolveModule(resolveAlias(nuxtModule), { paths: nuxt.options.modulesDir })
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/src/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function normalizeTemplate (template: NuxtTemplate<any> | string): Resolv
}
if (!template.filename) {
const srcPath = parse(template.src)
template.filename = template.fileName ||
template.filename = (template as any).fileName ||
`${basename(srcPath.dir)}.${srcPath.name}.${hash(template.src)}${srcPath.ext}`
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxi/src/commands/info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default defineNuxtCommand({
Builder: builder,
UserConfig: Object.keys(nuxtConfig).map(key => '`' + key + '`').join(', '),
RuntimeModules: listModules(nuxtConfig.modules),
BuildModules: listModules(nuxtConfig.buildModules)
BuildModules: listModules(nuxtConfig.buildModules || [])
}

console.log('RootDir:', rootDir)
Expand Down
1 change: 0 additions & 1 deletion packages/nuxi/src/utils/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export const writeTypes = async (nuxt: Nuxt) => {
}

const references: TSReference[] = [
...nuxt.options.buildModules,
...nuxt.options.modules,
...nuxt.options._modules
]
Expand Down
6 changes: 1 addition & 5 deletions packages/nuxt/src/app/composables/asyncData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,7 @@ export function useAsyncData<
options.server = options.server ?? true
options.default = options.default ?? getDefault

// TODO: remove support for `defer` in Nuxt 3 RC
if ((options as any).defer) {
console.warn('[useAsyncData] `defer` has been renamed to `lazy`. Support for `defer` will be removed in RC.')
}
options.lazy = options.lazy ?? (options as any).defer ?? false
options.lazy = options.lazy ?? false
options.immediate = options.immediate ?? true

// Setup nuxt instance payload
Expand Down
Loading

0 comments on commit 5ac9d85

Please sign in to comment.