Skip to content

Commit

Permalink
chore: fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Dec 12, 2024
1 parent c87cd8a commit a0bcc0b
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 47 deletions.
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default tseslint.config(
{
allowModules: [
'vite',
'esbuild',
'less',
'sass',
'sass-embedded',
Expand Down
11 changes: 3 additions & 8 deletions packages/vite/rollup.dts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,11 @@ const identifierReplacements: Record<string, Record<string, string>> = {
Plugin$1: 'rolldown.Plugin',
PluginContext$1: 'rolldown.PluginContext',
TransformPluginContext$1: 'rolldown.TransformPluginContext',
TransformResult$3: 'rolldown.TransformResult',
TransformResult$2: 'rolldown.TransformResult',
},
'rolldown/experimental': {
TransformOptions$2: 'rolldown_experimental_TransformOptions',
TransformResult$2: 'rolldown_experimental_TransformResult',
},
esbuild: {
TransformResult$1: 'esbuild_TransformResult',
TransformOptions$1: 'esbuild_TransformOptions',
BuildOptions$1: 'esbuild_BuildOptions',
TransformOptions$1: 'rolldown_experimental_TransformOptions',
TransformResult$1: 'rolldown_experimental_TransformResult',
},
'node:https': {
Server$1: 'HttpsServer',
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from 'rolldown/experimental'
import type { RollupCommonJSOptions } from 'dep-types/commonjs'
import type { RollupDynamicImportVarsOptions } from 'dep-types/dynamicImportVars'
import type { TransformOptions } from 'esbuild'
import type { EsbuildTarget } from 'types/internal/esbuildOptions'
import type { ChunkMetadata } from 'types/metadata'
import { withTrailingSlash } from '../shared/utils'
import {
Expand Down Expand Up @@ -101,7 +101,7 @@ export interface BuildEnvironmentOptions {
* https://esbuild.github.io/content-types/#javascript for more details.
* @default 'modules'
*/
target?: 'modules' | TransformOptions['target'] | false
target?: 'modules' | EsbuildTarget | false
/**
* whether to inject module preload polyfill.
* Note: does not apply to library mode.
Expand Down Expand Up @@ -154,7 +154,7 @@ export interface BuildEnvironmentOptions {
* doesn't support the #RGBA syntax.
* @default target
*/
cssTarget?: TransformOptions['target'] | false
cssTarget?: EsbuildTarget | false
/**
* Override CSS minification specifically instead of defaulting to `build.minify`,
* so you can configure minification for JS and CSS separately.
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export type {
StylusPreprocessorOptions,
} from './plugins/css'
export type { JsonOptions } from './plugins/json'
export type { TransformOptions as EsbuildTransformOptions } from 'esbuild'
export type { EsbuildTransformOptions } from 'types/internal/esbuildOptions'
export type { ESBuildOptions, ESBuildTransformResult } from './plugins/esbuild'
export type { Manifest, ManifestChunk } from './plugins/manifest'
export type { ResolveOptions, InternalResolveOptions } from './plugins/resolve'
Expand Down
16 changes: 2 additions & 14 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import path from 'node:path'
import { promisify } from 'node:util'
import { performance } from 'node:perf_hooks'
import colors from 'picocolors'
import type { BuildOptions as EsbuildBuildOptions } from 'esbuild'
import { init, parse } from 'es-module-lexer'
import { isDynamicPattern } from 'tinyglobby'
import {
Expand All @@ -13,6 +12,7 @@ import {
type OutputOptions as RollupOutputOptions,
rolldown,
} from 'rolldown'
import type { DepsOptimizerEsbuildOptions } from 'types/internal/esbuildOptions'
import type { ResolvedConfig } from '../config'
import {
arraify,
Expand Down Expand Up @@ -96,19 +96,7 @@ export interface DepOptimizationConfig {
*
* https://esbuild.github.io/api
*/
esbuildOptions?: Omit<
EsbuildBuildOptions,
| 'bundle'
| 'entryPoints'
| 'external'
| 'write'
| 'watch'
| 'outdir'
| 'outfile'
| 'outbase'
| 'outExtension'
| 'metafile'
>
esbuildOptions?: DepsOptimizerEsbuildOptions
rollupOptions?: Omit<RollupOptions, 'input' | 'logLevel' | 'output'> & {
output?: Omit<
RollupOutputOptions,
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import fs from 'node:fs'
import fsp from 'node:fs/promises'
import path from 'node:path'
import { performance } from 'node:perf_hooks'
import type { Loader } from 'esbuild'
import { scan, transform } from 'rolldown/experimental'
import type { PartialResolvedId, Plugin } from 'rolldown'
import colors from 'picocolors'
Expand Down Expand Up @@ -335,6 +334,8 @@ function globEntries(pattern: string | string[], environment: ScanEnvironment) {
})
}

type Loader = 'js' | 'ts' | 'jsx' | 'tsx'

export const scriptRE =
/(<script(?:\s+[a-z_:][-\w:]*(?:\s*=\s*(?:"[^"]*"|'[^']*'|[^"'<>=\s]+))?)*\s*>)(.*?)<\/script>/gis
export const commentRE = /<!--.*?-->/gs
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import type Stylus from 'stylus'
import type Less from 'less'
import type { Alias } from 'dep-types/alias'
import type { LightningCSSOptions } from 'types/internal/lightningcssOptions'
import type { TransformOptions } from 'esbuild'
import type { RawSourceMap } from '@ampproject/remapping'
import { WorkerWithFallback } from 'artichokie'
import { globSync } from 'tinyglobby'
Expand All @@ -37,6 +36,7 @@ import type {
TransformAttributeResult as LightningCssTransformAttributeResult,
TransformResult as LightningCssTransformResult,
} from 'lightningcss'
import type { EsbuildTransformOptions } from 'types/internal/esbuildOptions'
import { getCodeWithSourcemap, injectSourcesContent } from '../server/sourcemap'
import type { EnvironmentModuleNode } from '../server/moduleGraph'
import {
Expand Down Expand Up @@ -1989,8 +1989,8 @@ async function minifyCSS(

function resolveMinifyCssEsbuildOptions(
options: ESBuildOptions,
): TransformOptions {
const base: TransformOptions = {
): EsbuildTransformOptions {
const base: EsbuildTransformOptions = {
charset: options.charset ?? 'utf8',
logLevel: options.logLevel,
logLimit: options.logLimit,
Expand Down
34 changes: 17 additions & 17 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import path from 'node:path'
import colors from 'picocolors'
import type {
Loader,
Message,
TransformOptions,
TransformResult,
} from 'esbuild'
import type { RawSourceMap } from '@ampproject/remapping'
import type { InternalModuleFormat, SourceMap } from 'rolldown'
import type { TSConfckParseResult } from 'tsconfck'
import { TSConfckCache, TSConfckParseError, parse } from 'tsconfck'
import type { FSWatcher } from 'dep-types/chokidar'
import type {
EsbuildLoader,
EsbuildMessage,
EsbuildTransformOptions,
EsbuildTransformResult as RawEsbuildTransformResult,
} from 'types/internal/esbuildOptions'
import {
combineSourcemaps,
createDebugger,
Expand Down Expand Up @@ -41,7 +41,7 @@ export const defaultEsbuildSupported = {
'import-meta': true,
}

export interface ESBuildOptions extends TransformOptions {
export interface ESBuildOptions extends EsbuildTransformOptions {
include?: string | RegExp | string[] | RegExp[]
exclude?: string | RegExp | string[] | RegExp[]
jsxInject?: string
Expand All @@ -51,7 +51,7 @@ export interface ESBuildOptions extends TransformOptions {
minify?: never
}

export type ESBuildTransformResult = Omit<TransformResult, 'map'> & {
export type ESBuildTransformResult = Omit<RawEsbuildTransformResult, 'map'> & {
map: SourceMap
}

Expand Down Expand Up @@ -83,7 +83,7 @@ const importEsbuild = () => {
export async function transformWithEsbuild(
code: string,
filename: string,
options?: TransformOptions,
options?: EsbuildTransformOptions,
inMap?: object,
config?: ResolvedConfig,
watcher?: FSWatcher,
Expand All @@ -102,7 +102,7 @@ export async function transformWithEsbuild(
} else if (ext === 'cts' || ext === 'mts') {
loader = 'ts'
} else {
loader = ext as Loader
loader = ext as EsbuildLoader
}
}

Expand Down Expand Up @@ -183,7 +183,7 @@ export async function transformWithEsbuild(
}
}

const resolvedOptions: TransformOptions = {
const resolvedOptions: EsbuildTransformOptions = {
sourcemap: true,
// ensure source file name contains full query
sourcefile: filename,
Expand Down Expand Up @@ -227,7 +227,7 @@ export async function transformWithEsbuild(
// patch error information
if (e.errors) {
e.frame = ''
e.errors.forEach((m: Message) => {
e.errors.forEach((m: EsbuildMessage) => {
if (
m.text === 'Experimental decorators are not currently enabled' ||
m.text ===
Expand All @@ -252,7 +252,7 @@ export function esbuildPlugin(config: ResolvedConfig): Plugin {

// Remove optimization options for dev as we only need to transpile them,
// and for build as the final optimization is in `buildEsbuildPlugin`
const transformOptions: TransformOptions = {
const transformOptions: EsbuildTransformOptions = {
target: 'esnext',
charset: 'utf8',
...esbuildTransformOptions,
Expand Down Expand Up @@ -307,7 +307,7 @@ export function esbuildPlugin(config: ResolvedConfig): Plugin {

const rollupToEsbuildFormatMap: Record<
string,
TransformOptions['format'] | undefined
EsbuildTransformOptions['format'] | undefined
> = {
es: 'esm',
cjs: 'cjs',
Expand Down Expand Up @@ -381,7 +381,7 @@ export const buildEsbuildPlugin = (config: ResolvedConfig): Plugin => {
export function resolveEsbuildTranspileOptions(
config: ResolvedConfig,
format: InternalModuleFormat,
): TransformOptions | null {
): EsbuildTransformOptions | null {
const target = config.build.target
const minify = config.build.minify === 'esbuild'

Expand All @@ -395,7 +395,7 @@ export function resolveEsbuildTranspileOptions(
const isEsLibBuild = config.build.lib && format === 'es'
const esbuildOptions = config.esbuild || {}

const options: TransformOptions = {
const options: EsbuildTransformOptions = {
charset: 'utf8',
...esbuildOptions,
loader: 'js',
Expand Down Expand Up @@ -467,7 +467,7 @@ export function resolveEsbuildTranspileOptions(
}
}

function prettifyMessage(m: Message, code: string): string {
function prettifyMessage(m: EsbuildMessage, code: string): string {
let res = colors.yellow(m.text)
if (m.location) {
res += `\n` + generateCodeFrame(code, m.location)
Expand Down
28 changes: 28 additions & 0 deletions packages/vite/types/internal/esbuildOptions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */

// @ts-ignore `esbuild` may not be installed
import type esbuild from 'esbuild'

/* eslint-enable @typescript-eslint/ban-ts-comment */

export type EsbuildTarget = string | string[]

export type EsbuildLoader = esbuild.Loader
export type EsbuildTransformOptions = esbuild.TransformOptions
export type EsbuildTransformResult = esbuild.TransformResult

export type EsbuildMessage = esbuild.Message

export type DepsOptimizerEsbuildOptions = Omit<
esbuild.BuildOptions,
| 'bundle'
| 'entryPoints'
| 'external'
| 'write'
| 'watch'
| 'outdir'
| 'outfile'
| 'outbase'
| 'outExtension'
| 'metafile'
>

0 comments on commit a0bcc0b

Please sign in to comment.