Skip to content

Commit

Permalink
chore: use the longest extension
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Jul 11, 2023
1 parent b476fc4 commit f70a95a
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/vitest/src/runtime/external-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { dirname } from 'node:path'
import { Module as _Module, createRequire } from 'node:module'
import { readFileSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { extname } from 'pathe'
import { basename, extname } from 'pathe'
import { isNodeBuiltin } from 'vite-node/utils'
import type { RuntimeRPC } from '../types'

Expand Down Expand Up @@ -261,6 +261,23 @@ export class ExternalModulesExecutor {
return m
}

private findLongestRegisteredExtension(filename: string) {
const name = basename(filename)
let currentExtension
let index
let startIndex = 0
// eslint-disable-next-line no-cond-assign
while ((index = name.indexOf('.', startIndex)) !== -1) {
startIndex = index + 1
if (index === 0)
continue // Skip dotfiles like .gitignore
currentExtension = (name.slice(index))
if (this.extensions[currentExtension])
return currentExtension
}
return '.js'
}

public createRequire = (filename: string) => {
const _require = createRequire(filename)
const require = ((id: string) => {
Expand Down Expand Up @@ -292,7 +309,7 @@ export class ExternalModulesExecutor {
if (cached)
return cached.exports

const extension = extname(filename)
const extension = this.findLongestRegisteredExtension(filename)
const loader = this.extensions[extension] || this.extensions['.js']
loader(module, filename)

Expand Down

0 comments on commit f70a95a

Please sign in to comment.