Skip to content

Commit

Permalink
fix: bundleConfigFile cjs and mts
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Jun 24, 2022
1 parent 9d16253 commit 438a9b8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
dynamicImport,
isExternalUrl,
isObject,
isTS,
lookupFile,
mergeAlias,
mergeConfig,
Expand Down Expand Up @@ -873,8 +874,7 @@ export async function loadConfigFromFile(
const bundled = await bundleConfigFile(resolvedPath, true)
dependencies = bundled.dependencies

const isTS = /\.[cm]?ts$/.test(resolvedPath)
if (isTS) {
if (isTS(resolvedPath)) {
// before we can register loaders without requiring users to run node
// with --experimental-loader themselves, we have to do a hack here:
// bundle the config file w/ ts transforms first, write it to disk,
Expand Down Expand Up @@ -956,7 +956,7 @@ async function bundleConfigFile(
{
name: 'inject-file-scope-variables',
setup(build) {
build.onLoad({ filter: /\.[jt]s$/ }, async (args) => {
build.onLoad({ filter: /\.[cm]?[jt]s$/ }, async (args) => {
const contents = await fs.promises.readFile(args.path, 'utf8')
const injectValues =
`const __dirname = ${JSON.stringify(path.dirname(args.path))};` +
Expand All @@ -966,7 +966,7 @@ async function bundleConfigFile(
)};`

return {
loader: args.path.endsWith('.ts') ? 'ts' : 'js',
loader: isTS(args.path) ? 'ts' : 'js',
contents: injectValues + contents
}
})
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1056,3 +1056,5 @@ export function stripBomTag(content: string): string {

return content
}

export const isTS = (filename: string): boolean => /\.[cm]?ts$/.test(filename)

0 comments on commit 438a9b8

Please sign in to comment.