extensionless
Advanced tools
Comparing version 1.1.7 to 1.1.8
{ | ||
"name": "extensionless", | ||
"version": "1.1.7", | ||
"version": "1.1.8", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -6,2 +6,3 @@ Node.js loader for import specifiers as file paths without extensions or as directory paths | ||
Install: | ||
``` | ||
@@ -14,2 +15,3 @@ npm i extensionless | ||
Start `node` with the following flag added: | ||
``` | ||
@@ -21,7 +23,34 @@ --experimental-loader=extensionless | ||
You can now use import specifiers as file paths without extensions or as directory paths: | ||
```js | ||
import mod from './mod' | ||
// imports from the first existing file in the candidates list as follows | ||
// ['./mod.js', './mod.json', './mod/index.js', './mod/index.json'] | ||
``` | ||
| ||
If the specifier ends with a path separator, it only looks for index files in that directory: | ||
```js | ||
import mod from '../mod/' | ||
// imports from the first existing file in the candidates list as follows | ||
// ['../mod/index.js', '../mod/index.json'] | ||
``` | ||
| ||
To configure this package, you can add the field `extensionless` to `package.json`: | ||
``` | ||
```json | ||
"extensionless": { | ||
"lookFor": ["mjs", "js", "json"] // default: ["js", "json"] | ||
"lookFor": ["mjs", "js", "json"] | ||
} | ||
``` | ||
| Field | Default Value | | ||
| -------- | -------------------- | | ||
| lookFor | ```["js", "json"]``` | |
import {access, existsSync, readFileSync} from 'fs' | ||
import {dirname, extname, isAbsolute, join, normalize, sep} from 'path' | ||
import {cwd} from 'process' | ||
import {argv, cwd} from 'process' | ||
import {fileURLToPath} from 'url' | ||
let entrypoint = process.argv[1] | ||
let pkgJson, curDir, upDir = isAbsolute(entrypoint ?? '') ? dirname(entrypoint) : cwd() | ||
let pkgJson, curDir, upDir = isAbsolute(argv[1] ?? '') ? dirname(argv[1]) : cwd() | ||
do { | ||
let filePath = join(curDir = upDir, 'package.json') | ||
let path = join(curDir = upDir, 'package.json') | ||
if (existsSync(filePath)) { | ||
pkgJson = JSON.parse(readFileSync(filePath, 'utf8')) | ||
if (existsSync(path)) { | ||
pkgJson = JSON.parse(readFileSync(path, 'utf8')) | ||
} | ||
@@ -15,0 +13,0 @@ } while (!pkgJson && curDir !== (upDir = dirname(curDir))) |
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
4585
54
36