extensionless
Advanced tools
Comparing version 1.2.1 to 1.2.2
{ | ||
"name": "extensionless", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -29,4 +29,10 @@ Node.js loader for import specifiers as file paths without extensions or as directory paths | ||
import mod from './mod' assert {type: 'json'} | ||
// ['./mod.json', './mod/index.json'] | ||
import mod from '../mod' assert {type: 'json'} | ||
// ['../mod.json', '../mod/index.json'] | ||
import api from '/apps/api' | ||
// ['/apps/api.js', '/apps/api/index.js'] | ||
import web from 'file:///apps/web' | ||
// ['file:///apps/web.js', 'file:///apps/web/index.js'] | ||
``` | ||
@@ -36,3 +42,3 @@ | ||
If the specifier ends with a path separator, it only looks for index files in that directory: | ||
If it can be deduced from the specifier that its target is a directory, the resolver looks for only the index files: | ||
@@ -42,7 +48,19 @@ ```js | ||
import mod from '../mod/' | ||
// ['../mod/index.js'] | ||
import cur from '.' | ||
// ['./index.js'] | ||
import up from '..' | ||
// ['../index.js'] | ||
import mod from './mod/' | ||
// ['./mod/index.js'] | ||
import mod from '../mod/' assert {type: 'json'} | ||
// ['../mod/index.json'] | ||
import api from '/apps/api/' | ||
// ['/apps/api/index.js'] | ||
import web from 'file:///apps/web/' | ||
// ['file:///apps/web/index.js'] | ||
``` | ||
@@ -49,0 +67,0 @@ |
import {access, existsSync, readFileSync} from 'fs' | ||
import {dirname, extname, isAbsolute, join, normalize, sep} from 'path' | ||
import {dirname, extname, isAbsolute, join, normalize} from 'path' | ||
import {argv, cwd} from 'process' | ||
@@ -20,14 +20,17 @@ import {fileURLToPath} from 'url' | ||
Array.isArray(lookFor) && lookFor.length && lookFor.every(a => typeof a === 'string' && /^[a-z]+\w*$/i.test(a)) | ||
|| (console.error('\x1b[33m%s\x1b[0m', `The package.json field 'extensionless.lookFor' must be an array of alphanumeric strings!`), process.exit(1)) | ||
|| (console.error('\x1b[31m%s\x1b[0m', `The package.json field 'extensionless.lookFor' must be an array of alphanumeric strings!`), process.exit(1)) | ||
let extToSkip = ['.wasm', '.cjs', '.mjs', '.js', '.json'], none = [[], []] | ||
let indexFiles = [lookFor.map(e => `index.${e}`), ['index.json']] | ||
let candidates = [lookFor.map(e => `.${e}`).concat(lookFor.map(e => `${sep}index.${e}`)), ['.json', `${sep}index.json`]] | ||
let findPostfix = async (specifier, {importAssertions, parentURL}, isAbsPath) => { | ||
let postfixes = specifier.endsWith(sep) ? indexFiles : extToSkip.includes(extname(specifier)) ? none : candidates | ||
let relatives = indexFiles.map(i => i.map(f => `/${f}`)) | ||
let extToSkip = ['.js', '.cjs', '.mjs', '.json', '.node', '.wasm'], none = [[], []] | ||
let candidates = relatives.map(r => r.map(p => extname(p)).concat(r)) | ||
let findPostfix = async (spec, {importAssertions, parentURL}, isAbsPath, isRelSpec) => { | ||
let postfixes = spec.endsWith('/') ? indexFiles : isRelSpec ? relatives | ||
: extToSkip.includes(extname(spec)) ? none : candidates | ||
for (let postfix of postfixes[+(importAssertions?.type === 'json')]) { | ||
let path = isAbsPath ? specifier + postfix : join(dirname(fileURLToPath(parentURL)), specifier + postfix) | ||
let path = join(isAbsPath ? '' : dirname(fileURLToPath(parentURL)), spec + postfix) | ||
if (await new Promise(resolve => access(path, e => resolve(!e)))) { | ||
if (await new Promise(resolve => access(normalize(path), e => resolve(!e)))) { | ||
return postfix | ||
@@ -38,11 +41,12 @@ } | ||
let relPrefixes = [...new Set(['./', '../', `.${sep}`, `..${sep}`])] | ||
let relSpecs = ['.', '..'], relPrefixes = ['./', '../'] | ||
export async function resolve(specifier, context, nextResolve) { | ||
let spec = specifier.startsWith('file://') ? fileURLToPath(specifier) : specifier | ||
let isAbsPath = isAbsolute(spec) | ||
let postfix = (isAbsPath || relPrefixes.some(p => spec.startsWith(p))) | ||
&& await findPostfix(normalize(spec), context, isAbsPath) || '' | ||
let isAbsPath = isAbsolute(spec), isRelSpec = relSpecs.includes(spec) | ||
let postfix = (isAbsPath || isRelSpec || relPrefixes.some(p => spec.startsWith(p))) | ||
&& await findPostfix(spec, context, isAbsPath, isRelSpec) || '' | ||
return await nextResolve(specifier + postfix) | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5311
38
78