Skip to content

Commit

Permalink
feat: supports cts and mts files (#9268)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz authored Jul 22, 2022
1 parent 0358b04 commit 0602017
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 6 deletions.
3 changes: 2 additions & 1 deletion packages/vite/src/node/constants.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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 || /\.(m?ts|[jt]sx)$/,
options.exclude || /\.js$/
)

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 0602017

Please sign in to comment.