Skip to content

Commit

Permalink
Fix resolving non-js files
Browse files Browse the repository at this point in the history
Closes GH-5.
Closes GH-6.

Reviewed-by: Titus Wormer <[email protected]>
Reviewed-by: Nicolò Ribaudo <[email protected]>
  • Loading branch information
ifiokjr authored Jun 11, 2022
1 parent 937d3f3 commit 521dce7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 9 deletions.
25 changes: 18 additions & 7 deletions lib/get-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ function mimeToFormat(mime) {
}

/**
* @type {Record<string, (parsed: URL) => string|null>}
* @callback ProtocolHandler
* @param {URL} parsed
* @param {{parentURL: string}} context
* @param {boolean} ignoreErrors
* @returns {string|null}
*/

/**
* @type {Record<string, ProtocolHandler>}
*/
const protocolHandlers = Object.assign(Object.create(null), {
'data:': getDataProtocolModuleFormat,
Expand All @@ -59,9 +67,9 @@ function getDataProtocolModuleFormat(parsed) {
}

/**
* @param {URL} url
* @type {ProtocolHandler}
*/
function getFileProtocolModuleFormat(url) {
function getFileProtocolModuleFormat(url, _context, ignoreErrors) {
const filepath = fileURLToPath(url)
const ext = path.extname(filepath)
if (ext === '.js') {
Expand All @@ -70,6 +78,7 @@ function getFileProtocolModuleFormat(url) {

const format = extensionFormatMap[ext]
if (format) return format
if (ignoreErrors) return null

throw new ERR_UNKNOWN_FILE_EXTENSION(ext, filepath)
}
Expand All @@ -80,24 +89,26 @@ function getHttpProtocolModuleFormat() {

/**
* @param {URL} url
* @param {{parentURL: string}} context
* @returns {string|null}
*/
export function defaultGetFormatWithoutErrors(url) {
export function defaultGetFormatWithoutErrors(url, context) {
if (!hasOwnProperty.call(protocolHandlers, url.protocol)) {
return null
}

return protocolHandlers[url.protocol](url)
return protocolHandlers[url.protocol](url, context, true)
}

/**
* @param {string} url
* @param {{parentURL: string}} context
* @returns {string|null}
*/
export function defaultGetFormat(url) {
export function defaultGetFormat(url, context) {
const parsed = new URL(url)

return hasOwnProperty.call(protocolHandlers, parsed.protocol)
? protocolHandlers[parsed.protocol](parsed)
? protocolHandlers[parsed.protocol](parsed, context, false)
: null
}
4 changes: 2 additions & 2 deletions lib/resolve.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const packageJsonCache = new Map()
* @returns {void}
*/
function emitLegacyIndexDeprecation(url, packageJsonUrl, base, main) {
const format = defaultGetFormatWithoutErrors(url)
const format = defaultGetFormatWithoutErrors(url, {parentURL: base.href})
if (format !== 'module') return
const path = fileURLToPath(url.href)
const pkgPath = fileURLToPath(new URL('.', packageJsonUrl))
Expand Down Expand Up @@ -1275,6 +1275,6 @@ export function defaultResolve(specifier, context = {}) {
// Do NOT cast `url` to a string: that will work even when there are real
// problems, silencing them
url: url.href,
format: defaultGetFormatWithoutErrors(url)
format: defaultGetFormatWithoutErrors(url, {parentURL})
}
}
14 changes: 14 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,5 +644,19 @@ test('resolve(specifier, base?, conditions?)', async function (t) {
'should be able to resolve a self-import from a sub-file'
)

t.is(
await resolve('package-custom-extensions', import.meta.url),
new URL('node_modules/package-custom-extensions/b.ts', import.meta.url)
.href,
'should be able to resolve a custom `.ts` extension'
)

t.is(
await resolve('package-custom-extensions/c', import.meta.url),
new URL('node_modules/package-custom-extensions/d.wasm', import.meta.url)
.href,
'should be able to resolve a custom `.wasm` extension'
)

t.end()
})
Empty file.
Empty file.
Empty file.
8 changes: 8 additions & 0 deletions test/node_modules/package-custom-extensions/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 521dce7

Please sign in to comment.