Gathering detailed insights and metrics for pkgroll
Gathering detailed insights and metrics for pkgroll
Gathering detailed insights and metrics for pkgroll
Gathering detailed insights and metrics for pkgroll
npm install pkgroll
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
Node Version
NPM Version
1,236 Stars
154 Commits
30 Forks
6 Watching
2 Branches
11 Contributors
Updated on 27 Nov 2024
TypeScript (100%)
Cumulative downloads
Total Downloads
Last day
-7.6%
6,927
Compared to previous day
Last week
6.1%
36,116
Compared to previous week
Last month
25.4%
142,478
Compared to previous month
Last year
1,020.1%
749,742
Compared to previous year
pkgroll is a JavaScript package bundler powered by Rollup that automatically builds your package from entry-points defined in package.json
. No config necessary!
Write your code in TypeScript/ESM and run pkgroll
to get ESM/CommonJS/.d.ts outputs!
package.json#exports
to define entry-points.d.ts
bundlingAlready a sponsor? Join the discussion in the Development repo!
1npm install --save-dev pkgroll
Setup your project with source files in src
and output in dist
(configurable).
Define package entry-files in package.json
.
These configurations are for Node.js to determine how to import the package.
Pkgroll leverages the same configuration to determine how to build the package.
1{ 2 "name": "my-package", 3 4 // Set "module" or "commonjs" (https://nodejs.org/api/packages.html#type) 5 // "type": "module", 6 7 // Define the output files 8 "main": "./dist/index.cjs", 9 "module": "./dist/index.mjs", 10 "types": "./dist/index.d.cts", 11 12 // Define output files for Node.js export maps (https://nodejs.org/api/packages.html#exports) 13 "exports": { 14 "require": { 15 "types": "./dist/index.d.cts", 16 "default": "./dist/index.cjs" 17 }, 18 "import": { 19 "types": "./dist/index.d.mts", 20 "default": "./dist/index.mjs" 21 } 22 }, 23 24 // bin files will be compiled to be executable with the Node.js hashbang 25 "bin": "./dist/cli.js", 26 27 // (Optional) Add a build script referencing `pkgroll` 28 "scripts": { 29 "build": "pkgroll" 30 } 31 32 // ... 33}
Paths that start with ./dist/
are automatically mapped to files in the ./src/
directory.
Package roll!
1npm run build # or npx pkgroll
Pkgroll parses package entry-points from package.json
by reading properties main
, module
, types
, and exports
.
The paths in ./dist
are mapped to paths in ./src
(configurable with --src
and --dist
flags) to determine bundle entry-points.
Pkgroll detects the format for each entry-point based on the file extension or the package.json
property it's placed in, using the same lookup logic as Node.js.
package.json property | Output format |
---|---|
main | Auto-detect |
module | ESM Note: This unofficial property is not supported by Node.js and is mainly used by bundlers. |
types | TypeScript declaration |
exports | Auto-detect |
exports.require | CommonJS |
exports.import | Auto-detect |
exports.types | TypeScript declaration |
bin | Auto-detect Also patched to be executable with the Node.js hashbang. |
Auto-detect infers the type by extension or package.json#type
:
Extension | Output format |
---|---|
.cjs | CommonJS |
.mjs | ECMAScript Modules |
.js | Determined by package.json#type , defaulting to CommonJS |
Packages to externalize are detected by reading dependency types in package.json
. Only dependencies listed in devDependencies
are bundled in.
When generating type declarations (.d.ts
files), this also bundles and tree-shakes type dependencies declared in devDependencies
as well.
1// package.json 2{ 3 // ... 4 5 "peerDependencies": { 6 // Externalized 7 }, 8 "dependencies": { 9 // Externalized 10 }, 11 "optionalDependencies": { 12 // Externalized 13 }, 14 "devDependencies": { 15 // Bundled 16 }, 17}
Aliases can be configured in the import map, defined in package.json#imports
.
For native Node.js import mapping, all entries must be prefixed with #
to indicate an internal subpath import. Pkgroll takes advantage of this behavior to define entries that are not prefixed with #
as an alias.
Native Node.js import mapping supports conditional imports (eg. resolving different paths for Node.js and browser), but Pkgroll does not.
⚠️ Aliases are not supported in type declaration generation. If you need type support, do not use aliases.
1{ 2 // ... 3 4 "imports": { 5 // Mapping '~utils' to './src/utils.js' 6 "~utils": "./src/utils.js", 7 8 // Native Node.js import mapping (can't reference ./src) 9 "#internal-package": "./vendors/package/index.js", 10 } 11}
Pkgroll uses esbuild to handle TypeScript and JavaScript transformation and minification.
The target specifies the environments the output should support. Depending on how new the target is, it can generate less code using newer syntax. Read more about it in the esbuild docs.
By default, the target is set to the version of Node.js used. It can be overwritten with the --target
flag:
1pkgroll --target=es2020 --target=node14.18.0
It will also automatically detect and include the target
specified in tsconfig.json#compilerOptions
.
node:
protocolNode.js builtin modules can be prefixed with the node:
protocol for explicitness:
1import fs from 'node:fs/promises'
This is a new feature and may not work in older versions of Node.js. While you can opt out of using it, your dependencies may still be using it (example package using node:
: path-exists).
Pass in a Node.js target that that doesn't support it to strip the node:
protocol from imports:
1pkgroll --target=node12.19
tsconfig.json
pathBy default, Pkgroll looks for tsconfig.json
configuration file in the current working directory. You can pass in a custom tsconfig.json
path with the --tsconfig
flag:
1pkgroll --tsconfig=tsconfig.build.json
Similarly to the target, the export condition specifies which fields to read from when evaluating export and import maps.
For example, to simulate import resolutions in Node.js, pass in node
as the export condition:
1pkgroll --export-condition=node
Node.js ESM offers interoperability with CommonJS via static analysis. However, not all bundlers compile ESM to CJS syntax in a way that is statically analyzable.
Because pkgroll uses Rollup, it's able to produce CJS modules that are minimal and interoperable with Node.js ESM.
This means you can technically output in CommonJS to get ESM and CommonJS support.
require()
in ESMSometimes it's useful to use require()
or require.resolve()
in ESM. ESM code that uses require()
can be seamlessly compiled to CommonJS, but when compiling to ESM, Node.js will error because require
doesn't exist in the module scope.
When compiling to ESM, Pkgroll detects require()
usages and shims it with createRequire(import.meta.url)
.
Pass in compile-time environment variables with the --env
flag.
This will replace all instances of process.env.NODE_ENV
with 'production'
and remove unused code:
1pkgroll --env.NODE_ENV=production
Pass in the --minify
flag to minify assets.
1pkgroll --minify
Run the bundler in watch mode during development:
1pkgroll --watch
Clean dist directory before bundling:
1pkgroll --clean-dist
Pass in the --sourcemap
flag to emit a source map file:
1pkgroll --sourcemap
Or to inline them in the distribution files:
1pkgroll --sourcemap=inline
Rollup has the best tree-shaking performance, outputs simpler code, and produces seamless CommonJS and ESM formats (minimal interop code). Notably, CJS outputs generated by Rollup supports named exports so it can be parsed by Node.js ESM. TypeScript & minification transformations are handled by esbuild for speed.
ESM and CommonJS outputs
As the Node.js ecosystem migrates to ESM, there will be both ESM and CommonJS users. A bundler helps accommodate both distribution types.
Dependency bundling yields smaller and faster installation.
Tree-shaking only pulls in used code from dependencies, preventing unused code and unnecessary files (eg. README.md
, package.json
, etc.) from getting downloaded.
Removing dependencies also eliminates dependency tree traversal, which is one of the biggest bottlenecks.
Inadvertent breaking changes
Dependencies can introduce breaking changes due to a discrepancy in environment support criteria, by accident, or in rare circumstances, maliciously.
Compiling dependencies will make sure new syntax & features are downgraded to support the same environments. And also prevent any unexpected changes from sneaking in during installation.
Type dependencies must be declared in the dependencies
object in package.json
, instead of devDependencies
, to be resolved by the consumer.
This may seem counterintuitive because types are a development enhancement. By bundling them in with your package, you remove the need for an external type dependency. Additionally, bundling only keeps the types that are actually used which helps minimize unnecessary bloat.
Minification strips dead-code, comments, white-space, and shortens variable names.
No vulnerabilities found.
No security vulnerabilities found.