pkg-to-jsr is a zero-config generator that creates a jsr.json file from your existing package.json, powered by zod-mini for fast runtime validation.
It simplifies the process of preparing your package for publication on JSR.
- π§ Zero configuration required - just run and go!
- πͺ Automatically generates
jsr.jsonfrompackage.json - π¦ Handles complex
exportsconfigurations with ease - π― Supports
includeandexcludeoptions for precise publishing control - π Streamlines your workflow for JSR publication
You can use pkg-to-jsr without installation using npx:
npx pkg-to-jsrAlternatively, you can use other package managers:
# Using Yarn
yarn dlx pkg-to-jsr
# Using pnpm
pnpm dlx pkg-to-jsr
# Using Bun
bunx pkg-to-jsrFor global installation:
npm install -g pkg-to-jsrRun the following command in your project directory:
npx pkg-to-jsrThis will generate a jsr.json file based on your package.json.
--root <path>: Specify the root directory containing thepackage.jsonfile (default: current working directory)
Here are some examples of how pkg-to-jsr transforms your package.json into jsr.json:
package.json:
{
"name": "package",
"jsrName": "@scope/package",
"version": "1.0.0",
"exports": "./index.js"
}Generated jsr.json:
{
"name": "@scope/package",
"version": "1.0.0",
"exports": {
".": "./index.js"
}
}package.json:
{
"name": "package",
"author": "ryoppippi",
"version": "1.0.0",
"exports": {
".": {
"jsr": "./src/index.ts",
"import": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"./utils": {
"jsr": "./src/utils.ts",
"import": "./dist/utils.js",
"types": "./dist/utils.d.ts"
}
},
"files": [
"dist",
"!dist/**/*.test.js"
],
"jsrInclude": [
"src"
],
"jsrExclude": [
"src/**/*.test.ts"
]
}Generated jsr.json:
{
"name": "@ryoppippi/package",
"version": "1.0.0",
"exports": {
".": "./src/index.ts",
"./utils": "./src/utils.ts"
},
"publish": {
"include": ["dist", "src"],
"exclude": ["dist/**/*.test.js", "src/**/*.test.ts"]
}
}pkg-to-jsr performs the following steps:
- π Locates your
package.jsonfile - π€ Extracts relevant information such as
name,version, andexports - βοΈ Generates a
jsr.jsonfile with the correct structure for JSR
More details on implementation can be found in the source code.
You can see example projects in the tests.
pkg-to-jsr determines the package name for jsr.json using the following logic:
- π·οΈ If a
jsrNamefield exists inpackage.jsonand is correctly formatted (@scope/package-name), it is used. - π¦ If
jsrNameis not present, it checks thenamefield inpackage.json. If this is correctly formatted for JSR, it is used. - π§ If
nameis not in JSR format, it combines thenameandauthorfields. For example, ifnameis "package" andauthoris "ryoppippi", it generates@ryoppippi/package. - β If none of the above methods produce a valid name, an error is thrown.
This approach allows maximum flexibility while ensuring compliance with JSR naming conventions.
The tool intelligently handles various exports configurations:
- π§΅ String exports are converted to object format
- π§© Complex exports with
jsr,import, and other conditions are handled - π If a
jsrfield is specified in the exports, it takes priority over other fields β οΈ Invalid or unsupported exports are warned about and skipped
pkg-to-jsr generates the publish.include and publish.exclude fields in jsr.json by merging and filtering information from multiple sources:
- π
jsrIncludearray inpackage.json: All entries are considered for inclusion - π«
jsrExcludearray inpackage.json: All entries are considered for exclusion - π
filesarray inpackage.json:- Files without a leading
!are considered for inclusion - Files with a leading
!are considered for exclusion (with the!removed)
- Files without a leading
The final include and exclude lists in jsr.json are the result of merging and filtering these sources:
- The
includelist combines unique entries from bothjsrIncludeand the positive entries infiles, excluding any paths that are injsrExclude - The
excludelist combines unique entries from bothjsrExcludeand the negative entries infiles(with!removed), excluding any paths that are injsrInclude
This approach provides fine-grained control over what gets published to JSR while maintaining compatibility with existing files configurations and allowing for explicit inclusion and exclusion rules.
Example:
package.json:
{
"files": [
"dist",
"src",
"!dist/**/*.test.js"
],
"jsrInclude": [
"src",
"types"
],
"jsrExclude": [
"src/**/*.test.ts",
"dist"
]
}Generated jsr.json:
{
"publish": {
"include": ["src", "types"],
"exclude": ["dist/**/*.test.js", "src/**/*.test.ts"]
}
}In this example:
srcis included because it's in bothfilesandjsrIncludetypesis included because it's injsrIncludedistis excluded because it's injsrExclude, overriding its presence infiles- Test files in both
distandsrcare excluded
This merged and filtered configuration ensures that all necessary files are included while respecting explicit inclusion and exclusion rules, providing precise control over the package contents for JSR publication.
Contributions are welcome! Please feel free to submit a Pull Request.