{"name":"nf3","version":"0.2.0","license":"MIT","_id":"nf3@0.2.0","maintainers":[{"name":"pi0","email":"pyapar@gmail.com"}],"homepage":"https://github.com/unjs/nf3#readme","bugs":{"url":"https://github.com/unjs/nf3/issues"},"dist":{"shasum":"27d800f936a2e9deff748e472d1bd456ad99bca1","tarball":"http://registry-npm.ywwl.com/nf3/download/nf3-0.2.0.tgz","fileCount":179,"integrity":"sha512-TtkYFah0uejbqjkDHIf75pasYzbnXNPoZ4eRZHIBV/GwMfLG8woxXIawEo82R/Kaanw0UDxTjO47k2sI+fhcHg==","signatures":[{"sig":"MEUCIBtuKlmq9aGbv3mkmjPL9lbgnnqwzkw1YUjcR2mW0ldPAiEA9qnpEA5+U4uipm4JkxfBLVZU01YHUM2pdVOZkhS1n6s=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":720947,"size":203984,"noattachment":false,"key":"/nf3/-/nf3-0.2.0.tgz"},"type":"module","types":"./dist/index.d.mts","exports":{".":"./dist/index.mjs"},"gitHead":"7d873a11858de372ed2b6586959b47bce5e2db75","scripts":{"dev":"vitest dev","lint":"eslint . && prettier -c .","test":"pnpm lint && pnpm test:types && vitest run --coverage","build":"obuild","prepack":"pnpm build","release":"pnpm test && pnpm build && changelogen --release && npm publish && git push --follow-tags","lint:fix":"automd && eslint . --fix && prettier -w .","test:types":"tsc --noEmit --skipLibCheck"},"_npmUser":{"name":"pi0","email":"pyapar@gmail.com"},"repository":{"url":"git+https://github.com/unjs/nf3.git","type":"git"},"_npmVersion":"11.6.0","description":"<!-- automd:badges color=yellow codecov packagephobia -->","directories":{},"sideEffects":false,"_nodeVersion":"24.9.0","_hasShrinkwrap":false,"packageManager":"pnpm@10.22.0","devDependencies":{"mlly":"^1.8.0","pathe":"^2.0.3","automd":"^0.4.2","eslint":"^9.39.1","obuild":"^0.4.1","rollup":"^4.53.2","semver":"^7.7.3","vitest":"^4.0.9","exsolve":"^1.0.8","prettier":"^3.6.2","rolldown":"^1.0.0-beta.50","pkg-types":"^2.3.0","oxc-minify":"^0.97.0","typescript":"^5.9.3","@types/node":"^24.10.1","@vercel/nft":"^0.30.3","changelogen":"^0.6.2","@types/semver":"^7.7.1","eslint-config-unjs":"^0.5.0","@vitest/coverage-v8":"^4.0.9","rollup-plugin-esbuild":"^6.2.1","@rollup/plugin-node-resolve":"^16.0.3"},"_npmOperationalInternal":{"tmp":"tmp/nf3_0.2.0_1763382936556_0.19568693870223663","host":"s3://npm-registry-packages-npm-production"},"readme":"# 📦 nf3\n\n<!-- automd:badges color=yellow codecov packagephobia -->\n\n[![npm version](https://img.shields.io/npm/v/nf3?color=yellow)](https://npmjs.com/package/nf3)\n[![npm downloads](https://img.shields.io/npm/dm/nf3?color=yellow)](https://npm.chart.dev/nf3)\n[![install size](https://badgen.net/packagephobia/install/nf3?color=yellow)](https://packagephobia.com/result?p=nf3)\n[![codecov](https://img.shields.io/codecov/c/gh/unjs/nf3?color=yellow)](https://codecov.io/gh/unjs/nf3)\n\n<!-- /automd -->\n\nThis plugin traces and copies only the `node_modules` that are actually required at runtime for your built output — powered by [@vercel/nft](https://github.com/vercel/nft).\n\nBundling external dependencies can sometimes fail or cause issues, especially when modules rely on relative paths, native bindings, or dynamic imports.\n\nTo solve this, the plugin analyzes your build output, traces its runtime dependencies, and copies a **tree-shaken**, **deduplicated**, and **runtime-only** subset of `node_modules` into `dist/node_modules`.\nThe result is a minimal, self-contained distribution directory that just works.\n\nOriginally extracted from [Nitro](https://nitro.build) and used for optimizing `nf3` package dist itself!\n\n## Usage\n\n### API\n\n```js\nimport { traceNodeModules } from \"nf3\";\n\nawait traceNodeModules([\"./index.mjs\"], {\n  //   outDir: \"dist\",\n  // chmod: 0o755,\n  // writePackageJson: true,\n  // traceAlias: {},\n  // hooks: {},\n  // nft: {}, // https://github.com/vercel/nft#options\n});\n```\n\n### Rollup/Rolldown/Vite Plugin\n\n```js\nimport { externals } from \"nf3/plugin\";\n\nexport default {\n  plugins: [\n    externals({\n      // rootDir: \".\",\n      // conditions: [\"node\", \"import\", \"default\"],\n      // include: [/^@my-scope\\//],\n      // exclude: [\"fsevents\"],\n      // traceInclude: [\"some-lib\"],\n      // trace: {\n      //   fullTraceInclude: [\"some-lib\"],\n      // }\n    }),\n  ],\n};\n```\n\n### Full Trace Include\n\nBy default, only files detected by [`@vercel/nft`](https://github.com/vercel/nft) are included in the output. Some packages may require all their files to be present at runtime (e.g., packages with dynamic requires or asset files).\n\nUse `fullTraceInclude` to specify package names that should have **all** files copied to the output:\n\n```js\nrollupNodeFileTrace({\n  trace: {\n    fullTraceInclude: [\"some-package\", \"@scope/another-package\"],\n  },\n});\n```\n\n> [!NOTE]\n> Requires Node.js >= 22.0.0 (`fs.promises.glob`).\n\n### Hooks\n\nAfter the Rollup plugin traces the required files, `traceNodeModules` processes them into an optimized `node_modules` output.\n\nEach phase can be extended through hooks:\n\n```js\nrollupNodeFileTrace({\n  hooks: {\n    traceStart: (files) => {},\n    traceResult: (result) => {},\n    tracedFiles: (files) => {},\n    tracedPackages: (packages) => {},\n    tracedLayout: (layout) => {},\n  },\n});\n```\n\n### Output Layout\n\n`traceNodeModules` returns the final output layout (also passed to the `tracedLayout` hook). All output paths are relative to `outDir`.\n\n```js\nconst layout = await traceNodeModules(\n  [\"/app/node_modules/bcrypt/index.js\", \"/app/packages/a/node_modules/bcrypt/index.js\"],\n  { rootDir: \"/app\", outDir: \"dist\" },\n);\n\n// layout.packages.bcrypt\n// {\n//   name: \"bcrypt\",\n//   hoisted: \"1.0.0\", // version linked at `node_modules/bcrypt`\n//   versions: {\n//     \"1.0.0\": { path: \"/app/node_modules/bcrypt\", outPath: \"node_modules/.nf3/bcrypt@1.0.0\", hoisted: true },\n//     \"2.0.0\": { path: \"/app/packages/a/node_modules/bcrypt\", outPath: \"node_modules/.nf3/bcrypt@2.0.0\", hoisted: false },\n//   },\n// }\n\n// layout.files (input file -> output file)\n// {\n//   \"/app/node_modules/bcrypt/index.js\": \"node_modules/.nf3/bcrypt@1.0.0/index.js\",\n//   \"/app/packages/a/node_modules/bcrypt/index.js\": \"node_modules/.nf3/bcrypt@2.0.0/index.js\",\n// }\n```\n\nWhen multiple versions of a package are traced, each is written to `node_modules/.nf3/<name>@<version>` and linked under the `node_modules` of the traced packages that depend on it. One version is linked at `node_modules/<name>`, chosen by (1) most traced dependants, (2) the version Node.js resolves from `rootDir`, then (3) the newest. Versions imported only by bundled code are not reachable through the bare specifier unless hoisted; use `layout.files` to import them by path.\n\n### Transforming\n\nBefore writing files, you can transform some of them.\n\n**Example:**\n\n```js\nimport { minify } from \"oxc-minify\";\n\nrollupNodeFileTrace({\n  transform: [\n    {\n      filter: (id) => /\\.[mc]?js$/.test(id),\n      handler: (code, id) => minify(id, code, {}).code,\n    },\n  ],\n});\n```\n\n## Packages DB\n\nNF3 exports a list of known packages that include native code and require platform-specific builds.\n\nThese packages cannot be bundled and should be traced as external dependencies most of the time and often need tracing.\n\n```js\nimport { NodeNativePackages } from \"nf3/db\";\n```\n\nNF3 exports a list of packages that need full tracing (all files copied instead of only NFT-detected ones), because they use dynamic requires or runtime asset loading that static analysis cannot detect. You can use this with the `traceInclude` plugin option to ensure they are always traced:\n\n```js\nimport { FullTracePackages } from \"nf3/db\";\n\nexternals({\n  traceInclude: [...FullTracePackages],\n});\n```\n\nNF3 also exports a list of packages that must be externalized rather than bundled, due to bundler compatibility issues with their module format or dynamic imports.\n\n```js\nimport { NonBundleablePackages } from \"nf3/db\";\n```\n\n## Development\n\n<details>\n\n<summary>local development</summary>\n\n- Clone this repository\n- Install latest LTS version of [Node.js](https://nodejs.org/en/)\n- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`\n- Install dependencies using `pnpm install`\n- Run interactive tests using `pnpm dev`\n\n</details>\n\n## License\n\nPublished under the [MIT](https://github.com/unjs/nf3/blob/main/LICENSE) license.\n","publish_time":1763382936837,"_cnpm_publish_time":1763382936837,"dist-tags":{"latest":"0.4.3"}}