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

feat: supports cts and mts files #9268

Merged
merged 7 commits into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/vite/src/node/constants.ts
Expand Up @@ -24,6 +24,7 @@ export const ESBUILD_MODULES_TARGET = [
export const DEFAULT_EXTENSIONS = [
'.mjs',
'.js',
'.mts',
'.ts',
'.jsx',
'.tsx',
Expand All @@ -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/

Expand Down
4 changes: 2 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/esbuild.ts
Expand Up @@ -78,6 +78,8 @@ export async function transformWithEsbuild(

if (ext === 'cjs' || ext === 'mjs') {
bluwy marked this conversation as resolved.
Show resolved Hide resolved
loader = 'js'
} else if (ext === 'cts' || ext === 'mts') {
loader = 'ts'
} else {
loader = ext as Loader
}
Expand Down Expand Up @@ -170,7 +172,7 @@ export async function transformWithEsbuild(

export function esbuildPlugin(options: ESBuildOptions = {}): Plugin {
const filter = createFilter(
options.include || /\.(tsx?|jsx)$/,
options.include || /\.([cm]?ts|[jt]sx)$/,
sxzz marked this conversation as resolved.
Show resolved Hide resolved
options.exclude || /\.js$/
)

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -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)) {
Expand Down
18 changes: 18 additions & 0 deletions playground/resolve/index.html
Expand Up @@ -52,6 +52,18 @@ <h2>
</h2>
<p class="tsx-extension">fail</p>

<h2>
A ts module can import another ESM module using its corresponding mjs file
name
</h2>
<p class="mjs-extension">fail</p>

<h2>
A ts module can import another CommonJS module using its corresponding cjs
file name
</h2>
<p class="cjs-extension">fail</p>

<h2>Resolve file name containing dot</h2>
<p class="dot">fail</p>

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

import { msgCjs as tsCjsExtensionMsg } from './ts-extension'
text('.cjs-extension', tsCjsExtensionMsg)

import { msgMjs as tsMjsExtensionMsg } from './ts-extension'
text('.mjs-extension', tsMjsExtensionMsg)

// filename with dot
import { bar } from './util/bar.util'
text('.dot', bar())
Expand Down
1 change: 1 addition & 0 deletions playground/resolve/ts-extension/hellocjs.cts
@@ -0,0 +1 @@
export const msgCjs = '[success] use .cjs extension to import a CommonJS module'
1 change: 1 addition & 0 deletions playground/resolve/ts-extension/hellomjs.mts
@@ -0,0 +1 @@
export const msgMjs = '[success] use .mjs extension to import an ESM module'
4 changes: 3 additions & 1 deletion playground/resolve/ts-extension/index.ts
@@ -1,5 +1,7 @@
import { msg } from './hello.js'
import { msgJsx } from './hellojsx.jsx'
import { msgTsx } from './hellotsx.js'
import { msgCjs } from './hellocjs.cjs'
import { msgMjs } from './hellomjs.mjs'

export { msg, msgJsx, msgTsx }
export { msg, msgJsx, msgTsx, msgCjs, msgMjs }