### Install pkgroll Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Installs pkgroll as a development dependency using npm. ```sh npm install --save-dev pkgroll ``` -------------------------------- ### Clean Distribution Directory Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Cleans the output directory (e.g., `dist`) before starting the bundling process by using the `--clean-dist` flag. ```sh pkgroll --clean-dist ``` -------------------------------- ### pkgroll package.json Configuration Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Demonstrates how to configure package entry-points, output formats, and build scripts in package.json for pkgroll. ```json5 { "name": "my-package", // Set "module" or "commonjs" (https://nodejs.org/api/packages.html#type) // "type": "module", // Define the output files "main": "./dist/index.cjs", "module": "./dist/index.mjs", "types": "./dist/index.d.cts", // Define output files for Node.js export maps (https://nodejs.org/api/packages.html#exports) "exports": { "require": { "types": "./dist/index.d.cts", "default": "./dist/index.cjs", }, "import": { "types": "./dist/index.d.mts", "default": "./dist/index.mjs", }, }, // bin files will be compiled to be executable with the Node.js hashbang "bin": "./dist/cli.js", // (Optional) Add a build script referencing `pkgroll` "scripts": { "build": "pkgroll", }, // ... } ``` -------------------------------- ### Run pkgroll build Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Executes the pkgroll build process using npm scripts or npx. ```sh npm run build # or npx pkgroll ``` -------------------------------- ### pkgroll vs. tsup Comparison Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Compares pkgroll with tsup, emphasizing pkgroll's zero-config nature and its reliance on Rollup for robust ESM to CJS compilation. It points out tsup's manual configuration requirement and potential limitations in ESM/CJS interoperability. ```markdown - **pkgroll**: Zero-config, uses Rollup for bundling, esbuild for transformations. Better ESM to CJS compilation. - **tsup**: Requires manual configuration, uses esbuild for bundling and transformations. May have limitations in ESM to CJS compilation. ``` -------------------------------- ### Enable Minification Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Activates code minification for the bundled output by passing the `--minify` flag. ```sh pkgroll --minify ``` -------------------------------- ### pkgroll Dependency Handling Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Illustrates how pkgroll differentiates between bundled and externalized dependencies based on package.json configurations. ```json5 // package.json { // ... "peerDependencies": { // Externalized }, "dependencies": { // Externalized }, "optionalDependencies": { // Externalized }, "devDependencies": { // Bundled }, } ``` -------------------------------- ### Enable Watch Mode Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Runs Pkgroll in watch mode, automatically rebundling files when changes are detected during development. ```sh pkgroll --watch ``` -------------------------------- ### Rollup and esbuild Integration in pkgroll Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Explains the technical choices behind pkgroll, highlighting Rollup's superior tree-shaking and code output quality, and esbuild's speed for transformations and minification. This combination ensures efficient and clean code generation for both CommonJS and ESM formats. ```javascript // pkgroll uses Rollup for bundling and tree-shaking // and esbuild for transformations and minification. // This approach ensures clean code output and efficient bundling. ``` -------------------------------- ### Generate Source Maps Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Enables the generation of source map files for debugging. Source maps can be emitted as separate files or inlined directly into the bundled code. ```sh pkgroll --sourcemap pkgroll --sourcemap=inline ``` -------------------------------- ### Use Custom Tsconfig Path Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Explains how to specify a custom path to the `tsconfig.json` file using the `--tsconfig` flag, allowing for different build configurations. ```sh pkgroll --tsconfig=tsconfig.build.json ``` -------------------------------- ### Publish Configuration with pkgroll Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Demonstrates how to use the `publishConfig` field in `package.json` to override specific fields during package publication. This allows for environment-specific configurations, such as different entry points or types for development versus production. ```json { "name": "my-package", "version": "1.0.0", "main": "dist/index.js", "module": "dist/index.mjs", "types": "dist/index.d.ts", "publishConfig": { "main": "dist/prod.index.js", "module": "dist/prod.index.mjs", "types": "dist/prod.index.d.ts" } } ``` -------------------------------- ### Configure Aliases with Tsconfig Paths Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Shows how to define module aliases in `tsconfig.json` using the `compilerOptions.paths` configuration. This allows for pattern matching and mapping to specific file paths. ```json5 { "compilerOptions": { "paths": { "@foo/*": [ "./src/foo/*" ], "~bar": [ "./src/bar/index.ts" ] } } } ``` -------------------------------- ### Pass Environment Variables Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Shows how to pass compile-time environment variables using the `--env` flag. These variables can be used to conditionally compile code or set build-specific configurations. ```sh pkgroll --env.NODE_ENV=production ``` -------------------------------- ### Configure Aliases with Import Map Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Demonstrates how to configure module aliases in `package.json` using the `imports` field. Aliases can be defined with or without the '#' prefix, though Node.js requires it for subpath imports. ```json5 { "imports": { "~utils": "./src/utils.js", "#internal-package": "./vendors/package/index.js" } } ``` -------------------------------- ### Set Target Environment Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Specifies the target JavaScript environments for the bundled output using the `--target` flag. This influences the generated code's syntax and features. Multiple targets can be specified. ```sh pkgroll --target=es2020 --target=node14.18.0 ``` -------------------------------- ### Shim 'require()' in ESM Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Illustrates how Pkgroll automatically shims `require()` calls within ESM code using `createRequire(import.meta.url)` to ensure compatibility when compiling to ESM. ```js import { createRequire } from 'module'; const require = createRequire(import.meta.url); // Usage of require or require.resolve const path = require.resolve('some-package'); ``` -------------------------------- ### Strip 'node:' Protocol Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Demonstrates how to strip the `node:` protocol prefix from Node.js built-in module imports. This is useful when targeting older Node.js versions that do not support this feature. ```sh pkgroll --target=node12.19 ``` -------------------------------- ### Set Export Condition Source: https://github.com/privatenumber/pkgroll/blob/master/README.md Configures the export condition used for resolving package entry points, similar to how Node.js handles `exports` and `imports` maps. Using `node` simulates Node.js resolution. ```sh pkgroll --export-condition=node ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.