Skip to content

Commit 0602017

Browse files
authored
feat: supports cts and mts files (#9268)
1 parent 0358b04 commit 0602017

File tree

8 files changed

+31
-6
lines changed

8 files changed

+31
-6
lines changed

packages/vite/src/node/constants.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const ESBUILD_MODULES_TARGET = [
2424
export const DEFAULT_EXTENSIONS = [
2525
'.mjs',
2626
'.js',
27+
'.mts',
2728
'.ts',
2829
'.jsx',
2930
'.tsx',
@@ -41,7 +42,7 @@ export const DEFAULT_CONFIG_FILES = [
4142

4243
export const JS_TYPES_RE = /\.(?:j|t)sx?$|\.mjs$/
4344

44-
export const OPTIMIZABLE_ENTRY_RE = /\.(?:(m|c)?js|ts)$/
45+
export const OPTIMIZABLE_ENTRY_RE = /\.(?:[cm]?[jt]s)$/
4546

4647
export const SPECIAL_QUERY_RE = /[\?&](?:worker|sharedworker|raw|url)\b/
4748

packages/vite/src/node/optimizer/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export interface DepOptimizationConfig {
113113
* List of file extensions that can be optimized. A corresponding esbuild
114114
* plugin must exist to handle the specific extension.
115115
*
116-
* By default, Vite can optimize `.mjs`, `.js`, and `.ts` files. This option
116+
* By default, Vite can optimize `.mjs`, `.js`, `.ts`, and `.mts` files. This option
117117
* allows specifying additional extensions.
118118
*
119119
* @experimental
@@ -1009,7 +1009,7 @@ function needsInterop(
10091009
}
10101010

10111011
if (output) {
1012-
// if a peer dependency used require() on a ESM dependency, esbuild turns the
1012+
// if a peer dependency used require() on an ESM dependency, esbuild turns the
10131013
// ESM dependency's entry chunk into a single default export... detect
10141014
// such cases by checking exports mismatch, and force interop.
10151015
const generatedExports: string[] = output.exports

packages/vite/src/node/plugins/esbuild.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export async function transformWithEsbuild(
7878

7979
if (ext === 'cjs' || ext === 'mjs') {
8080
loader = 'js'
81+
} else if (ext === 'cts' || ext === 'mts') {
82+
loader = 'ts'
8183
} else {
8284
loader = ext as Loader
8385
}
@@ -170,7 +172,7 @@ export async function transformWithEsbuild(
170172

171173
export function esbuildPlugin(options: ESBuildOptions = {}): Plugin {
172174
const filter = createFilter(
173-
options.include || /\.(tsx?|jsx)$/,
175+
options.include || /\.(m?ts|[jt]sx)$/,
174176
options.exclude || /\.js$/
175177
)
176178

packages/vite/src/node/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ export const isDataUrl = (url: string): boolean => dataUrlRE.test(url)
261261
export const virtualModuleRE = /^virtual-module:.*/
262262
export const virtualModulePrefix = 'virtual-module:'
263263

264-
const knownJsSrcRE = /\.((j|t)sx?|mjs|vue|marko|svelte|astro)($|\?)/
264+
const knownJsSrcRE = /\.((j|t)sx?|m[jt]s|vue|marko|svelte|astro)($|\?)/
265265
export const isJSRequest = (url: string): boolean => {
266266
url = cleanUrl(url)
267267
if (knownJsSrcRE.test(url)) {

playground/resolve/index.html

+18
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ <h2>
5252
</h2>
5353
<p class="tsx-extension">fail</p>
5454

55+
<h2>
56+
A ts module can import another ESM module using its corresponding mjs file
57+
name
58+
</h2>
59+
<p class="mjs-extension">fail</p>
60+
61+
<h2>
62+
A ts module can import another CommonJS module using its corresponding cjs
63+
file name
64+
</h2>
65+
<p class="cjs-extension">fail</p>
66+
5567
<h2>Resolve file name containing dot</h2>
5668
<p class="dot">fail</p>
5769

@@ -167,6 +179,12 @@ <h2>resolve package that contains # in path</h2>
167179
import { msgTsx as tsTsxExtensionMsg } from './ts-extension'
168180
text('.tsx-extension', tsTsxExtensionMsg)
169181

182+
import { msgCjs as tsCjsExtensionMsg } from './ts-extension'
183+
text('.cjs-extension', tsCjsExtensionMsg)
184+
185+
import { msgMjs as tsMjsExtensionMsg } from './ts-extension'
186+
text('.mjs-extension', tsMjsExtensionMsg)
187+
170188
// filename with dot
171189
import { bar } from './util/bar.util'
172190
text('.dot', bar())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const msgCjs = '[success] use .cjs extension to import a CommonJS module'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const msgMjs = '[success] use .mjs extension to import an ESM module'
+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import { msg } from './hello.js'
22
import { msgJsx } from './hellojsx.jsx'
33
import { msgTsx } from './hellotsx.js'
4+
import { msgCjs } from './hellocjs.cjs'
5+
import { msgMjs } from './hellomjs.mjs'
46

5-
export { msg, msgJsx, msgTsx }
7+
export { msg, msgJsx, msgTsx, msgCjs, msgMjs }

0 commit comments

Comments
 (0)