extensionless
Advanced tools
Comparing version 1.2.4 to 1.2.5
{ | ||
"name": "extensionless", | ||
"version": "1.2.4", | ||
"version": "1.2.5", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -41,2 +41,16 @@ Node.js loader for import specifiers as file paths without extensions or as directory paths | ||
To configure the module, add the field `extensionless` to your project's `package.json`: | ||
```json | ||
"extensionless": { | ||
"lookFor": ["js", "mjs", "cjs"] | ||
} | ||
``` | ||
| Field | Default Value | | ||
| --------- | ------------- | | ||
| `lookFor` | `["js"]` | | ||
| ||
When it can be deduced from the specifier that its target is a directory, the resolver looks for only the index files: | ||
@@ -68,12 +82,16 @@ | ||
To configure this package, you can add the field `extensionless` to `package.json`: | ||
This loader also adds support for Windows path resolution with which you can use forward or backward slashes as separators. | ||
```json | ||
"extensionless": { | ||
"lookFor": ["js", "mjs", "cjs"] | ||
} | ||
```js | ||
import mod from '.\\mod' | ||
// ['./mod.js', './mod/index.js'] | ||
import mod from '..\\mod\\' assert {type: 'json'} | ||
// ['../mod/index.json'] | ||
import api from 'C:/apps/api' | ||
// ['/C:/apps/api.js', '/C:/apps/api/index.js'] | ||
import web from 'C:\\apps\\web\\' | ||
// ['/C:/apps/web/index.js'] | ||
``` | ||
| Field | Default Value | | ||
| --------- | ------------- | | ||
| `lookFor` | `["js"]` | |
import {access, existsSync, readFileSync} from 'fs' | ||
import {dirname, extname, isAbsolute, join, win32} from 'path' | ||
import {dirname, extname, isAbsolute, join, posix, win32} from 'path' | ||
import {argv, cwd} from 'process' | ||
import {fileURLToPath} from 'url' | ||
@@ -22,5 +21,2 @@ let pkgJson, curDir, upDir = isAbsolute(argv[1] ?? '') ? dirname(argv[1]) : cwd() | ||
let relSpecs = ['.', '..'], relPrefixes = ['./', '../'] | ||
let extToSkip = ['.js', '.cjs', '.mjs', '.json', '.node', '.wasm'], none = [[], []] | ||
let indexFiles = [lookFor.map(e => `index.${e}`), ['index.json']] | ||
@@ -30,16 +26,21 @@ let relatives = indexFiles.map(i => i.map(f => `/${f}`)) | ||
let relSpecs = ['.', '..'], separators = [posix.sep, win32.sep] | ||
let relPrefixes = separators.flatMap(s => relSpecs.map(r => r + s)) | ||
let extToSkip = ['.js', '.cjs', '.mjs', '.json', '.node', '.wasm'], empty = [[''], ['']] | ||
export async function resolve(specifier, {importAssertions, parentURL}, nextResolve) { | ||
let spec = specifier.startsWith('file://') ? fileURLToPath(specifier) : specifier | ||
let isAbsPath = isAbsolute(spec), isRelSpec = relSpecs.includes(spec) | ||
let isAbsPath = isAbsolute(specifier), isRelSpec = relSpecs.includes(specifier) | ||
let isRelative = isRelSpec || relPrefixes.some(p => specifier.startsWith(p)) | ||
if (isAbsPath || isRelSpec || relPrefixes.some(p => spec.startsWith(p))) { | ||
let posts = spec.endsWith('/') ? indexFiles : isRelSpec ? relatives | ||
: extToSkip.includes(extname(spec)) ? none : candidates | ||
if (isAbsPath || isRelative || specifier.startsWith('file://')) { | ||
let postfixes = isRelSpec ? relatives | ||
: separators.some(s => specifier.endsWith(s)) ? indexFiles | ||
: extToSkip.includes(extname(specifier)) ? empty : candidates | ||
for (let post of posts[+(importAssertions?.type === 'json')]) { | ||
let path = join(isAbsPath ? '' : dirname(fileURLToPath(parentURL)), spec + post) | ||
let reqsWin32Fix = path.startsWith(win32.sep) | ||
for (let postfix of postfixes[+(importAssertions?.type === 'json')]) { | ||
let fileSpec = isAbsPath ? `file://${specifier}` : specifier | ||
let fileUrl = new URL(fileSpec + postfix, parentURL) | ||
if (await new Promise(r => access(path.substring(+reqsWin32Fix), e => r(!e)))) { | ||
return await nextResolve(specifier + post) | ||
if (!postfix || await new Promise(r => access(fileUrl, e => r(!e)))) { | ||
return await nextResolve(fileUrl.href) | ||
} | ||
@@ -46,0 +47,0 @@ } |
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
5738
38
96