Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: esbuild plugin config logic #12493

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 35 additions & 50 deletions packages/vite/src/node/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,30 +117,39 @@ export async function transformWithEsbuild(
}
}

tsconfigRaw = {
...tsconfigRaw,
compilerOptions: {
...compilerOptionsForFile,
...tsconfigRaw?.compilerOptions,
},
const compilerOptions = {
...compilerOptionsForFile,
...tsconfigRaw?.compilerOptions,
}

const { compilerOptions } = tsconfigRaw
if (compilerOptions) {
// esbuild derives `useDefineForClassFields` from `target` instead of `tsconfig.compilerOptions.target`
// https://github.com/evanw/esbuild/issues/2584
// but we want `useDefineForClassFields` to be derived from `tsconfig.compilerOptions.target`
if (compilerOptions.useDefineForClassFields === undefined) {
const lowercaseTarget = compilerOptions.target?.toLowerCase() ?? 'es3'
if (lowercaseTarget.startsWith('es')) {
const esVersion = lowercaseTarget.slice(2)
compilerOptions.useDefineForClassFields =
esVersion === 'next' || +esVersion >= 2022
} else {
compilerOptions.useDefineForClassFields = false
}
// esbuild derives `useDefineForClassFields` from `target` instead of `tsconfig.compilerOptions.target`
// https://github.com/evanw/esbuild/issues/2584
// but we want `useDefineForClassFields` to be derived from `tsconfig.compilerOptions.target`
if (compilerOptions.useDefineForClassFields === undefined) {
const lowercaseTarget = compilerOptions.target?.toLowerCase() ?? 'es3'
if (lowercaseTarget.startsWith('es')) {
const esVersion = lowercaseTarget.slice(2)
compilerOptions.useDefineForClassFields =
esVersion === 'next' || +esVersion >= 2022
} else {
compilerOptions.useDefineForClassFields = false
}
}

// esbuild uses tsconfig fields when both the normal options and tsconfig was set
// but we want to prioritize the normal options
if (options) {
options.jsx && (compilerOptions.jsx = undefined)
options.jsxFactory && (compilerOptions.jsxFactory = undefined)
options.jsxFragment && (compilerOptions.jsxFragmentFactory = undefined)
options.jsxImportSource && (compilerOptions.jsxImportSource = undefined)
options.target && (compilerOptions.target = undefined)
}

tsconfigRaw = {
...tsconfigRaw,
compilerOptions,
}
}

const resolvedOptions = {
Expand All @@ -152,29 +161,6 @@ export async function transformWithEsbuild(
tsconfigRaw,
} as ESBuildOptions

// esbuild uses tsconfig fields when both the normal options and tsconfig was set
// but we want to prioritize the normal options
if (
options &&
typeof resolvedOptions.tsconfigRaw === 'object' &&
resolvedOptions.tsconfigRaw.compilerOptions
) {
options.jsx && (resolvedOptions.tsconfigRaw.compilerOptions.jsx = undefined)
options.jsxFactory &&
(resolvedOptions.tsconfigRaw.compilerOptions.jsxFactory = undefined)
options.jsxFragment &&
(resolvedOptions.tsconfigRaw.compilerOptions.jsxFragmentFactory =
undefined)
options.jsxImportSource &&
(resolvedOptions.tsconfigRaw.compilerOptions.jsxImportSource = undefined)
options.target &&
(resolvedOptions.tsconfigRaw.compilerOptions.target = undefined)
}

delete resolvedOptions.include
delete resolvedOptions.exclude
delete resolvedOptions.jsxInject
Comment on lines -174 to -176
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were needed because there is a cast here: https://github.com/vitejs/vite/pull/12493/files#diff-6d149ac9706cd508b52db0c059a0f01d670620a9a5a73cd7b122ce62b6b69471R205, from our own extended options. These fields are no longer present when calling transformWithEsbuild after the changes in this PR.


try {
const result = await transform(code, resolvedOptions)
let map: SourceMap
Expand Down Expand Up @@ -210,17 +196,16 @@ export async function transformWithEsbuild(
}

export function esbuildPlugin(options: ESBuildOptions): Plugin {
const filter = createFilter(
options.include || /\.(m?ts|[jt]sx)$/,
options.exclude || /\.js$/,
)
const { jsxInject, include, exclude, ...esbuildTransformOptions } = options

const filter = createFilter(include || /\.(m?ts|[jt]sx)$/, exclude || /\.js$/)

// 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 = {
target: 'esnext',
charset: 'utf8',
...options,
...esbuildTransformOptions,
minify: false,
minifyIdentifiers: false,
minifySyntax: false,
Expand Down Expand Up @@ -256,8 +241,8 @@ export function esbuildPlugin(options: ESBuildOptions): Plugin {
this.warn(prettifyMessage(m, code))
})
}
if (options.jsxInject && /\.(?:j|t)sx\b/.test(id)) {
result.code = options.jsxInject + ';' + result.code
if (jsxInject && /\.(?:j|t)sx\b/.test(id)) {
result.code = jsxInject + ';' + result.code
}
return {
code: result.code,
Expand Down