Gathering detailed insights and metrics for package-json-exports
Gathering detailed insights and metrics for package-json-exports
Gathering detailed insights and metrics for package-json-exports
Gathering detailed insights and metrics for package-json-exports
npm install package-json-exports
Typescript
Module System
Node Version
NPM Version
72.2
Supply Chain
98.5
Quality
74.5
Maintenance
100
Vulnerability
99.3
License
Total Downloads
741
Last Day
2
Last Week
14
Last Month
28
Last Year
122
Minified
Minified + Gzipped
Latest Version
0.0.2
Package Id
package-json-exports@0.0.2
Unpacked Size
40.38 kB
Size
7.91 kB
File Count
38
NPM Version
8.19.3
Node Version
18.13.0
Publised On
29 Jan 2023
Cumulative downloads
Total Downloads
Last day
100%
2
Compared to previous day
Last week
7.7%
14
Compared to previous week
Last month
460%
28
Compared to previous month
Last year
-56.6%
122
Compared to previous year
A file based package.json
exports
generator depending on fast-glob
.
1pnpm i -D package-json-exports
As a library maintainer, maintaining exports
in the package.json
file can be challenging when there are many files in the package, especially if we choose to provide individual files for certain reasons, instead of bundling them into a single file. Keeping the exports
up-to-date manually when the files change is error-prone and time-consuming. This library solves this issue by generating exports
automatically based on the files in the package. It can be integrated into the build process and used with rules to generate exports
with ease.
1import { generateExports } from "package-json-exports"; 2 3const packageJson = JSON.parse(await fs.readFile("package.json", "utf-8")); 4packageJson.main = "./index.js"; 5packageJson.module = "./esm/index.js"; 6packageJson.types = "./index.d.ts"; 7packageJson.exports = await generateExports(options); 8 9await fs.writeFile("package.json", JSON.stringify(packageJson, null, 2));
The exports
in this library's package.json
follows DAISHI KATO's How Jotai Specifies Package Entry Points, and here is the generating script: scripts/generatePackageJson.ts.
rules
Rule[]
The rules to generate the exports
.
Each rule is applied in order, and later rules override previous ones.
Each rule only executes fast-glob once. To improve performance, try to use fewer rules.
rules.pattern
string
The matching pattern for fast-glob
.
rules.exports
(matchedFilePath: string) => condition[]
You can return an array of conditions. A condition is an array of the property path from exports
. For example, if you want to generate:
1{ 2 "exports": { 3 "foo": { 4 "bar": "./src/index.js" 5 } 6 } 7}
the condition should be ["foo", "bar"]
.
You can return several conditions:
1{ 2 exports: (matchedFilePath) => { 3 return [ 4 ["foo", "bar"], 5 ["foo", "baz"], 6 ]; 7 }, 8}
The result will be:
1{ 2 "exports": { 3 "foo": { 4 "bar": "./src/index.js", 5 "baz": "./src/index.js" 6 } 7 } 8}
You can also return []
to ignore the file.
Notice that the matchedFilePath
is always starts with ./
.
rules.fastGlobOptions
FastGlob.Options
This option is passed to fast-glob
when matching the files. It's useful when you want to ignore some files.
defaultConditionKey
string
undefined
The default condition key to use when conditions conflict.
For example if there are files:
src/index.js
And the rules are:
1[ 2 { 3 pattern: "**/*.js", 4 exports: (matchedFilePath) => { 5 return [[generateExports.pathCondition(matchedFilePath), "require"]]; 6 }, 7 }, 8 { 9 pattern: "**/*", 10 exports: (matchedFilePath) => { 11 return [[generateExports.pathCondition(matchedFilePath)]]; 12 }, 13 }, 14];
The result will be:
1{ 2 "exports": { 3 "./src/index.js": { 4 "require": "./src/index.js", 5 "default": "./src/index.js" 6 } 7 } 8}
Without setting defaultConditionKey
, it throws an error when conditions conflict.
fastGlobOptions
FastGlob.Options
You can set the cwd
and ignore
options here. dot
option is set to true
by default. You can override it by setting it to false
.
simplify
boolean
false
Simplify the exports. It will replace the object with only one property with the value of the property recursively.
For example, if the exports was:
1{ 2 "exports": { 3 "./src/index.js": { 4 "browser": { 5 "import": "./src/index.js" 6 } 7 }, 8 "./src/foo.js": { 9 "require": "./src/foo.js", 10 "default": "./src/foo.js" 11 } 12 } 13}
It will be simplified to:
1{ 2 "exports": { 3 "./src/index.js": "./src/index.js", 4 "./src/foo.js": { 5 "require": "./src/foo.js", 6 "default": "./src/foo.js" 7 } 8 } 9}
If the exports was:
1{ 2 "exports": { 3 "node": { 4 "production": { 5 "import": "./dist/index.js" 6 } 7 } 8 } 9}
It will be simplified to:
1{ 2 "exports": "./dist/index.js" 3}
generateExports.normalizePath
(path: string) => string
This function can be used to normalize the path with leading ./
.
For example:
1generateExports.pathCondition("foo"); // ➡️ "./foo" 2generateExports.pathCondition("./foo"); // ➡️ "./foo" 3generateExports.pathCondition("./foo.js"); // ➡️ "./foo.js"
generateExports.removeExtension
(path: string, extname?: string) => string
This function can be used to remove the extension name.
For example:
1generateExports.pathCondition("foo"); // ➡️ "foo" 2generateExports.pathCondition("./foo"); // ➡️ "./foo" 3generateExports.pathCondition("./foo.js"); // ➡️ "./foo" 4generateExports.pathCondition("./foo/bar.js"); // ➡️ "./foo/bar"
generateExports.pathCondition
(path: string, extname?: string) => string
pathCondition
is a combination of normalizePath
and removeExtension
. This function can be used to generate either the path condition. It will remove the extension name and add ./
if the path doesn't start with ./
.
For example:
1generateExports.pathCondition("foo"); // ➡️ "./foo" 2generateExports.pathCondition("./foo"); // ➡️ "./foo" 3generateExports.pathCondition("./foo.js"); // ➡️ "./foo" 4generateExports.pathCondition("./foo.js", ".js"); // ➡️ "./foo" 5generateExports.pathCondition("./foo.d.ts"); // ➡️ "./foo.d" 6generateExports.pathCondition("./foo.d.ts", ".d.ts"); // ➡️ "./foo"
package.json
.ViPro ©️ MIT
No vulnerabilities found.
No security vulnerabilities found.
@polkadot-api/json-rpc-provider
This package exports the interface of `polkadot-api` with the chain.
@polkadot-api/json-rpc-provider-proxy
This package exports `getSyncProvider`, a function to create `JsonRpcProvider`s that will act as if the connection happen synchronously.
resolve-pkg-maps
Resolve package.json exports & imports maps
@yarn-tool/sort-package-json-exports