From 71868579058512b51991718655e089a78b99d39c Mon Sep 17 00:00:00 2001 From: Shinigami Date: Sun, 19 Dec 2021 12:35:25 +0100 Subject: [PATCH] chore: prefer type imports (#5835) --- .eslintrc.cjs | 6 ++- packages/create-vite/__tests__/cli.spec.ts | 3 +- .../create-vite/template-vue-ts/src/env.d.ts | 2 +- .../playground/react-emotion/vite.config.ts | 3 +- packages/playground/react/vite.config.ts | 3 +- packages/playground/shims.d.ts | 2 +- packages/playground/testUtils.ts | 2 +- .../tsconfig-json/nested-with-extends/main.ts | 1 + .../playground/tsconfig-json/nested/main.ts | 1 + packages/playground/tsconfig-json/src/main.ts | 1 + packages/playground/vue/CustomBlockPlugin.ts | 2 +- .../worker/__tests__/worker.spec.ts | 2 +- packages/plugin-legacy/index.d.ts | 2 +- .../jsx-runtime/babel-import-to-require.ts | 5 +- .../src/jsx-runtime/babel-restore-jsx.ts | 2 +- .../src/jsx-runtime/restore-jsx.ts | 3 +- packages/plugin-vue-jsx/index.d.ts | 6 +-- packages/plugin-vue/src/compiler.ts | 2 +- packages/plugin-vue/src/handleHotUpdate.ts | 6 +-- packages/plugin-vue/src/index.ts | 6 +-- packages/plugin-vue/src/main.ts | 9 ++-- packages/plugin-vue/src/script.ts | 4 +- packages/plugin-vue/src/style.ts | 6 +-- packages/plugin-vue/src/template.ts | 6 +-- .../plugin-vue/src/utils/descriptorCache.ts | 4 +- packages/plugin-vue/src/utils/error.ts | 4 +- packages/vite/src/client/client.ts | 4 +- packages/vite/src/client/overlay.ts | 2 +- .../vite/src/node/__tests__/config.spec.ts | 11 ++-- packages/vite/src/node/build.ts | 18 ++++--- packages/vite/src/node/cli.ts | 7 +-- packages/vite/src/node/config.ts | 49 +++++++---------- packages/vite/src/node/http.ts | 10 ++-- packages/vite/src/node/importGlob.ts | 2 +- packages/vite/src/node/logger.ts | 11 ++-- .../src/node/optimizer/esbuildDepPlugin.ts | 6 +-- packages/vite/src/node/optimizer/index.ts | 5 +- .../src/node/optimizer/registerMissing.ts | 2 +- packages/vite/src/node/optimizer/scan.ts | 11 ++-- packages/vite/src/node/packages.ts | 4 +- packages/vite/src/node/plugin.ts | 14 ++--- packages/vite/src/node/plugins/asset.ts | 6 +-- .../src/node/plugins/assetImportMetaUrl.ts | 4 +- .../vite/src/node/plugins/clientInjections.ts | 4 +- packages/vite/src/node/plugins/css.ts | 12 ++--- packages/vite/src/node/plugins/dataUri.ts | 2 +- packages/vite/src/node/plugins/define.ts | 6 +-- packages/vite/src/node/plugins/esbuild.ts | 15 +++--- packages/vite/src/node/plugins/html.ts | 12 ++--- .../vite/src/node/plugins/importAnalysis.ts | 9 ++-- .../src/node/plugins/importAnalysisBuild.ts | 9 ++-- packages/vite/src/node/plugins/index.ts | 4 +- packages/vite/src/node/plugins/json.ts | 2 +- .../vite/src/node/plugins/loadFallback.ts | 2 +- packages/vite/src/node/plugins/manifest.ts | 6 +-- .../src/node/plugins/modulePreloadPolyfill.ts | 4 +- packages/vite/src/node/plugins/preAlias.ts | 4 +- packages/vite/src/node/plugins/reporter.ts | 4 +- packages/vite/src/node/plugins/resolve.ts | 14 ++--- .../vite/src/node/plugins/ssrRequireHook.ts | 4 +- packages/vite/src/node/plugins/terser.ts | 6 +-- packages/vite/src/node/plugins/wasm.ts | 4 +- packages/vite/src/node/plugins/worker.ts | 6 +-- packages/vite/src/node/preview.ts | 17 +++--- .../server/__tests__/pluginContainer.spec.ts | 8 +-- packages/vite/src/node/server/hmr.ts | 10 ++-- packages/vite/src/node/server/index.ts | 53 +++++++++---------- .../vite/src/node/server/middlewares/base.ts | 4 +- .../vite/src/node/server/middlewares/error.ts | 8 +-- .../src/node/server/middlewares/indexHtml.ts | 9 ++-- .../vite/src/node/server/middlewares/proxy.ts | 8 +-- .../node/server/middlewares/spaFallback.ts | 2 +- .../src/node/server/middlewares/static.ts | 10 ++-- .../vite/src/node/server/middlewares/time.ts | 2 +- .../src/node/server/middlewares/transform.ts | 4 +- packages/vite/src/node/server/moduleGraph.ts | 4 +- packages/vite/src/node/server/openBrowser.ts | 2 +- .../vite/src/node/server/pluginContainer.ts | 12 ++--- packages/vite/src/node/server/send.ts | 4 +- packages/vite/src/node/server/sourcemap.ts | 2 +- .../vite/src/node/server/transformRequest.ts | 4 +- packages/vite/src/node/server/ws.ts | 20 +++---- packages/vite/src/node/ssr/ssrExternal.ts | 5 +- .../vite/src/node/ssr/ssrManifestPlugin.ts | 4 +- packages/vite/src/node/ssr/ssrModuleLoader.ts | 5 +- packages/vite/src/node/ssr/ssrStacktrace.ts | 5 +- packages/vite/src/node/ssr/ssrTransform.ts | 8 +-- packages/vite/src/node/utils.ts | 4 +- packages/vite/types/alias.d.ts | 2 +- packages/vite/types/chokidar.d.ts | 4 +- packages/vite/types/http-proxy.d.ts | 8 +-- packages/vite/types/importMeta.d.ts | 27 +++++----- packages/vite/types/shims.d.ts | 10 ++-- packages/vite/types/ws.d.ts | 12 ++--- scripts/jestPerTestSetup.ts | 9 ++-- 95 files changed, 337 insertions(+), 337 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 578e00417db95b..48a0bd7773b0d7 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -89,7 +89,11 @@ module.exports = defineConfig({ '@typescript-eslint/no-inferrable-types': 'off', '@typescript-eslint/no-non-null-assertion': 'off', // maybe we should turn this on in a new PR '@typescript-eslint/no-unused-vars': 'off', // maybe we should turn this on in a new PR - '@typescript-eslint/no-var-requires': 'off' + '@typescript-eslint/no-var-requires': 'off', + '@typescript-eslint/consistent-type-imports': [ + 'error', + { prefer: 'type-imports' } + ] }, overrides: [ { diff --git a/packages/create-vite/__tests__/cli.spec.ts b/packages/create-vite/__tests__/cli.spec.ts index 307ce36a5a2b35..c52998172149e6 100644 --- a/packages/create-vite/__tests__/cli.spec.ts +++ b/packages/create-vite/__tests__/cli.spec.ts @@ -1,5 +1,6 @@ /* eslint-disable node/no-extraneous-import */ -import { commandSync, ExecaSyncReturnValue, SyncOptions } from 'execa' +import type { ExecaSyncReturnValue, SyncOptions } from 'execa' +import { commandSync } from 'execa' import { mkdirpSync, readdirSync, remove, writeFileSync } from 'fs-extra' import { join } from 'path' diff --git a/packages/create-vite/template-vue-ts/src/env.d.ts b/packages/create-vite/template-vue-ts/src/env.d.ts index d27eb5a311f66a..aafef9509dd5c4 100644 --- a/packages/create-vite/template-vue-ts/src/env.d.ts +++ b/packages/create-vite/template-vue-ts/src/env.d.ts @@ -1,7 +1,7 @@ /// declare module '*.vue' { - import { DefineComponent } from 'vue' + import type { DefineComponent } from 'vue' // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types const component: DefineComponent<{}, {}, any> export default component diff --git a/packages/playground/react-emotion/vite.config.ts b/packages/playground/react-emotion/vite.config.ts index 5b60bc7e26d145..9364c8f616c2f5 100644 --- a/packages/playground/react-emotion/vite.config.ts +++ b/packages/playground/react-emotion/vite.config.ts @@ -1,6 +1,7 @@ import react from '@vitejs/plugin-react' +import type { UserConfig } from 'vite' -const config: import('vite').UserConfig = { +const config: UserConfig = { plugins: [ react({ jsxImportSource: '@emotion/react', diff --git a/packages/playground/react/vite.config.ts b/packages/playground/react/vite.config.ts index e8780031511dac..c6955a131d375f 100644 --- a/packages/playground/react/vite.config.ts +++ b/packages/playground/react/vite.config.ts @@ -1,6 +1,7 @@ import react from '@vitejs/plugin-react' +import type { UserConfig } from 'vite' -const config: import('vite').UserConfig = { +const config: UserConfig = { plugins: [react()], build: { // to make tests faster diff --git a/packages/playground/shims.d.ts b/packages/playground/shims.d.ts index 99ed8eb232b14f..ced8fb1ad585ae 100644 --- a/packages/playground/shims.d.ts +++ b/packages/playground/shims.d.ts @@ -4,7 +4,7 @@ declare module 'css-color-names' { } declare module '*.vue' { - import { ComponentOptions } from 'vue' + import type { ComponentOptions } from 'vue' const component: ComponentOptions export default component } diff --git a/packages/playground/testUtils.ts b/packages/playground/testUtils.ts index 2be13b1aee8cc4..3c6cba769b3ffe 100644 --- a/packages/playground/testUtils.ts +++ b/packages/playground/testUtils.ts @@ -5,7 +5,7 @@ import fs from 'fs' import path from 'path' import colors from 'css-color-names' -import { ElementHandle } from 'playwright-chromium' +import type { ElementHandle } from 'playwright-chromium' import type { Manifest } from 'vite' export function slash(p: string): string { diff --git a/packages/playground/tsconfig-json/nested-with-extends/main.ts b/packages/playground/tsconfig-json/nested-with-extends/main.ts index 557f496c4fe1dc..497708b4f4d226 100644 --- a/packages/playground/tsconfig-json/nested-with-extends/main.ts +++ b/packages/playground/tsconfig-json/nested-with-extends/main.ts @@ -1,4 +1,5 @@ // @ts-nocheck +// eslint-disable-next-line @typescript-eslint/consistent-type-imports import { NestedWithExtendsTypeOnlyClass } from './not-used-type' class NestedWithExtendsBase { diff --git a/packages/playground/tsconfig-json/nested/main.ts b/packages/playground/tsconfig-json/nested/main.ts index c3c23c44ef59f8..306efd0b1a87d9 100644 --- a/packages/playground/tsconfig-json/nested/main.ts +++ b/packages/playground/tsconfig-json/nested/main.ts @@ -1,4 +1,5 @@ // @ts-nocheck +// eslint-disable-next-line @typescript-eslint/consistent-type-imports import { NestedTypeOnlyClass } from './not-used-type' class NestedBase { diff --git a/packages/playground/tsconfig-json/src/main.ts b/packages/playground/tsconfig-json/src/main.ts index 05f1d1a02796ae..6ae1fe03b7d023 100644 --- a/packages/playground/tsconfig-json/src/main.ts +++ b/packages/playground/tsconfig-json/src/main.ts @@ -2,6 +2,7 @@ import '../nested/main' import '../nested-with-extends/main' +// eslint-disable-next-line @typescript-eslint/consistent-type-imports import { MainTypeOnlyClass } from './not-used-type' class MainBase { diff --git a/packages/playground/vue/CustomBlockPlugin.ts b/packages/playground/vue/CustomBlockPlugin.ts index b434bbc24cf324..4f5def023902bc 100644 --- a/packages/playground/vue/CustomBlockPlugin.ts +++ b/packages/playground/vue/CustomBlockPlugin.ts @@ -1,4 +1,4 @@ -import { Plugin } from 'vite' +import type { Plugin } from 'vite' export const vueI18nPlugin: Plugin = { name: 'vue-i18n', diff --git a/packages/playground/worker/__tests__/worker.spec.ts b/packages/playground/worker/__tests__/worker.spec.ts index 46b136edd66d2a..b84ea39fef5d0a 100644 --- a/packages/playground/worker/__tests__/worker.spec.ts +++ b/packages/playground/worker/__tests__/worker.spec.ts @@ -1,7 +1,7 @@ import fs from 'fs' import path from 'path' import { untilUpdated, isBuild, testDir } from '../../testUtils' -import { Page } from 'playwright-chromium' +import type { Page } from 'playwright-chromium' test('normal', async () => { await page.click('.ping') diff --git a/packages/plugin-legacy/index.d.ts b/packages/plugin-legacy/index.d.ts index 612e6cacda9702..8f340f11cba074 100644 --- a/packages/plugin-legacy/index.d.ts +++ b/packages/plugin-legacy/index.d.ts @@ -1,4 +1,4 @@ -import { Plugin } from 'vite' +import type { Plugin } from 'vite' export interface Options { /** diff --git a/packages/plugin-react/src/jsx-runtime/babel-import-to-require.ts b/packages/plugin-react/src/jsx-runtime/babel-import-to-require.ts index 936d88b0c8472b..dc7129862fd976 100644 --- a/packages/plugin-react/src/jsx-runtime/babel-import-to-require.ts +++ b/packages/plugin-react/src/jsx-runtime/babel-import-to-require.ts @@ -1,3 +1,4 @@ +import type * as babelCore from '@babel/core' import type { types as t, Visitor } from '@babel/core' /** @@ -9,9 +10,7 @@ import type { types as t, Visitor } from '@babel/core' * * var _jsx = require("react/jsx-runtime").jsx */ -export function babelImportToRequire({ - types: t -}: typeof import('@babel/core')): { +export function babelImportToRequire({ types: t }: typeof babelCore): { visitor: Visitor } { return { diff --git a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts b/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts index da08f5327a4eae..669a0aeeced207 100644 --- a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts +++ b/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts @@ -2,7 +2,7 @@ * https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx * @license GNU General Public License v3.0 */ -import * as babel from '@babel/core' +import type * as babel from '@babel/core' /** * Visitor factory for babel, converting React.createElement(...) to ... diff --git a/packages/plugin-react/src/jsx-runtime/restore-jsx.ts b/packages/plugin-react/src/jsx-runtime/restore-jsx.ts index 095f7c586e43a1..5cc7042a32c55e 100644 --- a/packages/plugin-react/src/jsx-runtime/restore-jsx.ts +++ b/packages/plugin-react/src/jsx-runtime/restore-jsx.ts @@ -1,3 +1,4 @@ +import type * as babelCore from '@babel/core' import type { PluginItem, types as t } from '@babel/core' type RestoredJSX = [result: t.File | null | undefined, isCommonJS: boolean] @@ -8,7 +9,7 @@ const jsxNotFound: RestoredJSX = [null, false] /** Restore JSX from `React.createElement` calls */ export async function restoreJSX( - babel: typeof import('@babel/core'), + babel: typeof babelCore, code: string, filename: string ): Promise { diff --git a/packages/plugin-vue-jsx/index.d.ts b/packages/plugin-vue-jsx/index.d.ts index 2cac80ba56632e..a702c09baa8417 100644 --- a/packages/plugin-vue-jsx/index.d.ts +++ b/packages/plugin-vue-jsx/index.d.ts @@ -1,6 +1,6 @@ -import { Plugin } from 'vite' -import { VueJSXPluginOptions } from '@vue/babel-plugin-jsx' -import { FilterPattern } from '@rollup/pluginutils' +import type { Plugin } from 'vite' +import type { VueJSXPluginOptions } from '@vue/babel-plugin-jsx' +import type { FilterPattern } from '@rollup/pluginutils' declare interface FilterOptions { include?: FilterPattern diff --git a/packages/plugin-vue/src/compiler.ts b/packages/plugin-vue/src/compiler.ts index 59350659b9a31e..0fb4eef7d2677a 100644 --- a/packages/plugin-vue/src/compiler.ts +++ b/packages/plugin-vue/src/compiler.ts @@ -5,7 +5,7 @@ declare module 'vue/compiler-sfc' { } } -import * as _compiler from 'vue/compiler-sfc' +import type * as _compiler from 'vue/compiler-sfc' export function resolveCompiler(root: string): typeof _compiler { // resolve from project root first, then fallback to peer dep (if any) diff --git a/packages/plugin-vue/src/handleHotUpdate.ts b/packages/plugin-vue/src/handleHotUpdate.ts index d93e647a1498bf..2bce3950563367 100644 --- a/packages/plugin-vue/src/handleHotUpdate.ts +++ b/packages/plugin-vue/src/handleHotUpdate.ts @@ -1,13 +1,13 @@ import _debug from 'debug' -import { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc' +import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc' import { createDescriptor, getDescriptor, setPrevDescriptor } from './utils/descriptorCache' import { getResolvedScript, setResolvedScript } from './script' -import { ModuleNode, HmrContext } from 'vite' -import { ResolvedOptions } from '.' +import type { ModuleNode, HmrContext } from 'vite' +import type { ResolvedOptions } from '.' const debug = _debug('vite:hmr') diff --git a/packages/plugin-vue/src/index.ts b/packages/plugin-vue/src/index.ts index 51ac057232aef0..f12dbd5b4cc0ef 100644 --- a/packages/plugin-vue/src/index.ts +++ b/packages/plugin-vue/src/index.ts @@ -1,13 +1,13 @@ import fs from 'fs' -import { Plugin, ViteDevServer } from 'vite' +import type { Plugin, ViteDevServer } from 'vite' import { createFilter } from '@rollup/pluginutils' -import { +import type { SFCBlock, SFCScriptCompileOptions, SFCStyleCompileOptions, SFCTemplateCompileOptions } from 'vue/compiler-sfc' -import * as _compiler from 'vue/compiler-sfc' +import type * as _compiler from 'vue/compiler-sfc' import { resolveCompiler } from './compiler' import { parseVueRequest } from './utils/query' import { getDescriptor, getSrcDescriptor } from './utils/descriptorCache' diff --git a/packages/plugin-vue/src/main.ts b/packages/plugin-vue/src/main.ts index 11dad79cc61733..29c5cffa4cfa81 100644 --- a/packages/plugin-vue/src/main.ts +++ b/packages/plugin-vue/src/main.ts @@ -1,18 +1,19 @@ import qs from 'querystring' import path from 'path' -import { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc' -import { ResolvedOptions } from '.' +import type { SFCBlock, SFCDescriptor } from 'vue/compiler-sfc' +import type { ResolvedOptions } from '.' import { createDescriptor, getPrevDescriptor, setSrcDescriptor } from './utils/descriptorCache' -import { PluginContext, SourceMap, TransformPluginContext } from 'rollup' +import type { PluginContext, SourceMap, TransformPluginContext } from 'rollup' import { normalizePath } from '@rollup/pluginutils' import { resolveScript, isUseInlineTemplate } from './script' import { transformTemplateInMain } from './template' import { isOnlyTemplateChanged, isEqualBlock } from './handleHotUpdate' -import { RawSourceMap, SourceMapConsumer, SourceMapGenerator } from 'source-map' +import type { RawSourceMap } from 'source-map' +import { SourceMapConsumer, SourceMapGenerator } from 'source-map' import { createRollupError } from './utils/error' import { transformWithEsbuild } from 'vite' import { EXPORT_HELPER_ID } from './helper' diff --git a/packages/plugin-vue/src/script.ts b/packages/plugin-vue/src/script.ts index 241eca21eefea1..93610dcf7f6a36 100644 --- a/packages/plugin-vue/src/script.ts +++ b/packages/plugin-vue/src/script.ts @@ -1,5 +1,5 @@ -import { SFCDescriptor, SFCScriptBlock } from 'vue/compiler-sfc' -import { ResolvedOptions } from '.' +import type { SFCDescriptor, SFCScriptBlock } from 'vue/compiler-sfc' +import type { ResolvedOptions } from '.' import { resolveTemplateCompilerOptions } from './template' // ssr and non ssr builds would output different script content diff --git a/packages/plugin-vue/src/style.ts b/packages/plugin-vue/src/style.ts index c27e16591f1de9..ad9d981412f52d 100644 --- a/packages/plugin-vue/src/style.ts +++ b/packages/plugin-vue/src/style.ts @@ -1,6 +1,6 @@ -import { SFCDescriptor } from 'vue/compiler-sfc' -import { TransformPluginContext } from 'rollup' -import { ResolvedOptions } from '.' +import type { SFCDescriptor } from 'vue/compiler-sfc' +import type { TransformPluginContext } from 'rollup' +import type { ResolvedOptions } from '.' // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export async function transformStyle( diff --git a/packages/plugin-vue/src/template.ts b/packages/plugin-vue/src/template.ts index 9ee386b1c90d69..72e9588967556e 100644 --- a/packages/plugin-vue/src/template.ts +++ b/packages/plugin-vue/src/template.ts @@ -1,13 +1,13 @@ import path from 'path' import slash from 'slash' -import { +import type { SFCDescriptor, SFCTemplateCompileOptions, SFCTemplateCompileResults, CompilerOptions } from 'vue/compiler-sfc' -import { PluginContext, TransformPluginContext } from 'rollup' -import { ResolvedOptions } from '.' +import type { PluginContext, TransformPluginContext } from 'rollup' +import type { ResolvedOptions } from '.' import { getResolvedScript } from './script' import { createRollupError } from './utils/error' diff --git a/packages/plugin-vue/src/utils/descriptorCache.ts b/packages/plugin-vue/src/utils/descriptorCache.ts index 990f679d0ac08b..c0b77e72d3e613 100644 --- a/packages/plugin-vue/src/utils/descriptorCache.ts +++ b/packages/plugin-vue/src/utils/descriptorCache.ts @@ -2,8 +2,8 @@ import fs from 'fs' import path from 'path' import slash from 'slash' import hash from 'hash-sum' -import { CompilerError, SFCDescriptor } from 'vue/compiler-sfc' -import { ResolvedOptions, VueQuery } from '..' +import type { CompilerError, SFCDescriptor } from 'vue/compiler-sfc' +import type { ResolvedOptions, VueQuery } from '..' // compiler-sfc should be exported so it can be re-used export interface SFCParseResult { diff --git a/packages/plugin-vue/src/utils/error.ts b/packages/plugin-vue/src/utils/error.ts index 1f3e48d7c7ed44..50999910fabb7b 100644 --- a/packages/plugin-vue/src/utils/error.ts +++ b/packages/plugin-vue/src/utils/error.ts @@ -1,5 +1,5 @@ -import { CompilerError } from 'vue/compiler-sfc' -import { RollupError } from 'rollup' +import type { CompilerError } from 'vue/compiler-sfc' +import type { RollupError } from 'rollup' export function createRollupError( id: string, diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 188d6f1a5eb01b..c801b716d0318c 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -1,4 +1,4 @@ -import { +import type { ErrorPayload, FullReloadPayload, HMRPayload, @@ -6,7 +6,7 @@ import { Update, UpdatePayload } from 'types/hmrPayload' -import { CustomEventName } from 'types/customEvent' +import type { CustomEventName } from 'types/customEvent' import { ErrorOverlay, overlayId } from './overlay' // eslint-disable-next-line node/no-missing-import import '@vite/env' diff --git a/packages/vite/src/client/overlay.ts b/packages/vite/src/client/overlay.ts index 536b9df167a83f..150c570fbc8aaf 100644 --- a/packages/vite/src/client/overlay.ts +++ b/packages/vite/src/client/overlay.ts @@ -1,4 +1,4 @@ -import { ErrorPayload } from 'types/hmrPayload' +import type { ErrorPayload } from 'types/hmrPayload' const template = /*html*/ `