diff --git a/packages/vite/src/node/constants.ts b/packages/vite/src/node/constants.ts index 5a108652ef7ce6..b5fb126e97beaa 100644 --- a/packages/vite/src/node/constants.ts +++ b/packages/vite/src/node/constants.ts @@ -24,6 +24,7 @@ export const ESBUILD_MODULES_TARGET = [ export const DEFAULT_EXTENSIONS = [ '.mjs', '.js', + '.mts', '.ts', '.jsx', '.tsx', @@ -41,7 +42,7 @@ export const DEFAULT_CONFIG_FILES = [ export const JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/ -export const OPTIMIZABLE_ENTRY_RE = /\.(?:(m|c)?js|ts)$/ +export const OPTIMIZABLE_ENTRY_RE = /\.(?:[cm]?[jt]s)$/ export const SPECIAL_QUERY_RE = /[\?&](?:worker|sharedworker|raw|url)\b/ diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index 2954accf1fc909..b544bc9f060d20 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -113,7 +113,7 @@ export interface DepOptimizationConfig { * List of file extensions that can be optimized. A corresponding esbuild * plugin must exist to handle the specific extension. * - * By default, Vite can optimize `.mjs`, `.js`, and `.ts` files. This option + * By default, Vite can optimize `.mjs`, `.js`, `.ts`, and `.mts` files. This option * allows specifying additional extensions. * * @experimental @@ -1009,7 +1009,7 @@ function needsInterop( } if (output) { - // if a peer dependency used require() on a ESM dependency, esbuild turns the + // if a peer dependency used require() on an ESM dependency, esbuild turns the // ESM dependency's entry chunk into a single default export... detect // such cases by checking exports mismatch, and force interop. const generatedExports: string[] = output.exports diff --git a/packages/vite/src/node/plugins/esbuild.ts b/packages/vite/src/node/plugins/esbuild.ts index 6ed963a5d5981c..9a95c1be816580 100644 --- a/packages/vite/src/node/plugins/esbuild.ts +++ b/packages/vite/src/node/plugins/esbuild.ts @@ -78,6 +78,8 @@ export async function transformWithEsbuild( if (ext === 'cjs' || ext === 'mjs') { loader = 'js' + } else if (ext === 'cts' || ext === 'mts') { + loader = 'ts' } else { loader = ext as Loader } @@ -170,7 +172,7 @@ export async function transformWithEsbuild( export function esbuildPlugin(options: ESBuildOptions = {}): Plugin { const filter = createFilter( - options.include || /\.(tsx?|jsx)$/, + options.include || /\.(m?ts|[jt]sx)$/, options.exclude || /\.js$/ ) diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index 331f9ba0ceb667..4398ec70f56121 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -261,7 +261,7 @@ export const isDataUrl = (url: string): boolean => dataUrlRE.test(url) export const virtualModuleRE = /^virtual-module:.*/ export const virtualModulePrefix = 'virtual-module:' -const knownJsSrcRE = /\.((j|t)sx?|mjs|vue|marko|svelte|astro)($|\?)/ +const knownJsSrcRE = /\.((j|t)sx?|m[jt]s|vue|marko|svelte|astro)($|\?)/ export const isJSRequest = (url: string): boolean => { url = cleanUrl(url) if (knownJsSrcRE.test(url)) { diff --git a/playground/resolve/index.html b/playground/resolve/index.html index bca49d55388e27..674819fc0195a6 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -52,6 +52,18 @@
fail
+fail
+ +fail
+fail
@@ -167,6 +179,12 @@