{"id":13602547,"url":"https://github.com/vercel/webpack-asset-relocator-loader","last_synced_at":"2025-04-04T12:09:00.519Z","repository":{"id":37937918,"uuid":"168409412","full_name":"vercel/webpack-asset-relocator-loader","owner":"vercel","description":"Used in ncc while emitting and relocating any asset references","archived":false,"fork":false,"pushed_at":"2024-08-30T10:51:37.000Z","size":2673,"stargazers_count":102,"open_issues_count":19,"forks_count":33,"subscribers_count":61,"default_branch":"main","last_synced_at":"2025-03-28T11:07:50.055Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.com/@vercel/webpack-asset-relocator-loader","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vercel.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-30T20:22:17.000Z","updated_at":"2025-02-11T08:01:57.000Z","dependencies_parsed_at":"2023-02-07T12:01:00.293Z","dependency_job_id":"5d667296-816a-4cf7-9e07-96043836bc09","html_url":"https://github.com/vercel/webpack-asset-relocator-loader","commit_stats":{"total_commits":282,"total_committers":13,"mean_commits":"21.692307692307693","dds":"0.24822695035460995","last_synced_commit":"3907f6bde4c0c915ffc22b4d7f34603503ccae58"},"previous_names":["zeit/webpack-asset-relocator-loader"],"tags_count":59,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vercel%2Fwebpack-asset-relocator-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vercel%2Fwebpack-asset-relocator-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vercel%2Fwebpack-asset-relocator-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vercel%2Fwebpack-asset-relocator-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vercel","download_url":"https://codeload.github.com/vercel/webpack-asset-relocator-loader/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247174423,"owners_count":20896078,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T18:01:27.366Z","updated_at":"2025-04-04T12:09:00.500Z","avatar_url":"https://github.com/vercel.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# Asset Relocator Loader for Webpack\n\n[![Build Status](https://circleci.com/gh/vercel/webpack-asset-relocator-loader.svg?\u0026style=shield)](https://circleci.com/gh/vercel/workflows/webpack-asset-relocator-loader)\n[![codecov](https://codecov.io/gh/vercel/webpack-asset-relocator-loader/branch/main/graph/badge.svg)](https://codecov.io/gh/vercel/webpack-asset-relocator-loader)\n\nAsset relocation loader used in ncc for performing Node.js builds while emitting and relocating any asset references.\n\n## Usage\n\n### Installation\n```bash\nnpm i -g @vercel/webpack-asset-relocator-loader\n```\n\n### Usage\n\nAdd this loader as a Webpack plugin for any JS files.\n\nAny `.node` files included will also support binary relocation.\n\n```js\n{\n  target: \"node\",\n  output: {\n    libraryTarget: \"commonjs2\"\n  },\n  module: {\n    rules: [\n      {\n        // For node binary relocations, include \".node\" files as well here\n        test: /\\.(m?js|node)$/,\n        // it is recommended for Node builds to turn off AMD support\n        parser: { amd: false },\n        use: {\n          loader: '@vercel/webpack-asset-relocator-loader',\n          options: {\n            // optional, base folder for asset emission (eg assets/name.ext)\n            outputAssetBase: 'assets',\n            // optional, restrict asset emissions to only the given folder.\n            filterAssetBase: process.cwd(),\n            // optional, permit entire __dirname emission\n            // eg `const nonAnalyzable = __dirname` can emit everything in the folder\n            emitDirnameAll: false,\n            // optional, permit entire filterAssetBase emission\n            // eg `const nonAnalyzable = process.cwd()` can emit everything in the cwd()\n            emitFilterAssetBaseAll: false,\n            // optional, custom functional asset emitter\n            // takes an asset path and returns the replacement\n            // or returns false to skip emission\n            customEmit: (path, { id, isRequire }) =\u003e false | '\"./custom-replacement\"',\n            // optional, a list of asset names already emitted or\n            // defined that should not be emitted\n            existingAssetNames: [],\n            wrapperCompatibility: false, // optional, default\n            // build for process.env.NODE_ENV = 'production'\n            production: true, // optional, default is undefined\n            cwd: process.cwd(), // optional, default\n            debugLog: false, // optional, default\n          }\n        }\n      }\n    ]\n  }\n}\n```\n\nAssets will be emitted using `emitAsset`, with their references updated in the code by the loader to the new output location.\n\n### Asset Permissions and Symlinks\n\nAsset symlinks and permissions are maintained in the loader, but aren't passed to Webpack as `emit` doesn't support these.\n\nThis information can be obtained from the loader through the API calls `getAssetMeta()` and `getSymlinks()`:\n\n```js\nconst relocateLoader = require('webpack-asset-relocator-loader');\n\nwebpack({...}).run((err, stats) =\u003e {\n  const assetMeta = relocateLoader.getAssetMeta();\n  const symlinks = relocateLoader.getSymlinks();\n});\n```\n\nThey will always contain the most recent build state.\n\n### Caching\n\nWhen using Webpack 5 caching, asset permissions need to be maintained through their own cache, and the public path needs to be injected into the build.\n\nTo ensure these cases work out, make sure to run `initAssetCache` in the build, with the `options.outputAssetBase` argument:\n\n```js\nconst relocateLoader = require('webpack-asset-relocator-loader');\n\nwebpack({\n  // ...\n\n  plugins: [\n    {\n      apply(compiler) {\n        compiler.hooks.compilation.tap(\"webpack-asset-relocator-loader\", compilation =\u003e {\n          relocateLoader.initAssetCache(compilation, outputAssetBase);\n        });\n      }\n    }\n  ]\n});\n```\n\n## How it Works\n\n### Asset Relocation\n\nAssets are detected using static analysis of code, based on very specific triggers designed for common Node.js workflows to provide build support for a very high (but not perfect) level of compatibility with existing Node.js libraries.\n\n* `process.cwd()`, `__filename`, `__dirname`, `path.*()`, `require.resolve` are all statically analyzed when possible.\n* File emissions for exact asset paths\n* Whole directory asset emissions for exact directory paths\n* Wildcard asset emissions for variable path expressions\n\nWhen an asset is emitted, the pure expression referencing the asset path is replaced with a new expression to the relocated asset and the asset emitted. In the case of wildcard emission, the dynamic parts of the expression are maintained.\n\n### Binary Relocation\n\nNode binary loading conventions cover the following triggers for binary relocations:\n* `require('bindings')(...)`\n* `nbind.init(..)`\n* `node-pre-gyp` include patterns\n\nAny shared libraries loaded by these binaries will also be emitted as well.\n\n### Node.js Compatibility Features\n\nIn addition to asset relocation, this loader also provides a couple\nof compatibility features for Webpack Node.js builds as part of its analysis.\n\nThese include:\n\n* `require.main === module` checks are retained for the entry point being built.\n* `options.wrapperCompatibility`: Automatically handles common AMD / Browserify wrappers to ensure they are properly built by Webpack. See the `utils/wrappers.js` file for the exact transformations currently provided.\n* `require.resolve` support in the target environment, while also supporting emission in the build environment.\n* Dynamic `require` statements are analyzed to exact paths wherever possible, and when not possible to analyze, turned into dynamic requires in the target environment.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvercel%2Fwebpack-asset-relocator-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvercel%2Fwebpack-asset-relocator-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvercel%2Fwebpack-asset-relocator-loader/lists"}