{"id":13739587,"url":"https://github.com/zheeeng/babel-plugin-rewrite-module-path","last_synced_at":"2025-04-10T14:21:05.140Z","repository":{"id":80217656,"uuid":"413167148","full_name":"zheeeng/babel-plugin-rewrite-module-path","owner":"zheeeng","description":"Rewrite module resolving path","archived":false,"fork":false,"pushed_at":"2024-07-18T16:21:58.000Z","size":184,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-11T01:31:23.239Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zheeeng.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-10-03T18:46:24.000Z","updated_at":"2021-11-07T01:52:24.000Z","dependencies_parsed_at":"2024-09-30T13:30:48.574Z","dependency_job_id":"fbc26501-e02a-43b2-9143-3231fc9a4d75","html_url":"https://github.com/zheeeng/babel-plugin-rewrite-module-path","commit_stats":{"total_commits":26,"total_committers":2,"mean_commits":13.0,"dds":"0.038461538461538436","last_synced_commit":"2bc8c7ff4c6d0bb40235073ffd53d6c2662cd9ef"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zheeeng%2Fbabel-plugin-rewrite-module-path","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zheeeng%2Fbabel-plugin-rewrite-module-path/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zheeeng%2Fbabel-plugin-rewrite-module-path/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zheeeng%2Fbabel-plugin-rewrite-module-path/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zheeeng","download_url":"https://codeload.github.com/zheeeng/babel-plugin-rewrite-module-path/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248154995,"owners_count":21056542,"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-03T04:00:35.508Z","updated_at":"2025-04-10T14:21:05.123Z","avatar_url":"https://github.com/zheeeng.png","language":"TypeScript","readme":"# babel-plugin-rewrite-module-path\n\n\u003cdiv align=\"center\"\u003e\n\n[![Known Vulnerabilities][known-vulnerabilities-image]][known-vulnerabilities-url]\n[![Maintainability][maintainability-image]][maintainability-url]\n![publish workflow][publish-workflow-image]\n[![Test Coverage][test-coverage-image]][test-coverage-url]\n[![license][license-image]][license-url]\n[![GitHub issues][github-issues-image]][github-issues-url]\n![NPM bundle size(minified + gzip)][bundle-size-image]\n\n[known-vulnerabilities-image]: https://snyk.io/test/github/zheeeng/babel-plugin-rewrite-module-path/badge.svg\n[known-vulnerabilities-url]: https://snyk.io/test/github/zheeeng/babel-plugin-rewrite-module-path\n\n[maintainability-image]: https://api.codeclimate.com/v1/badges/d3eaf22221bf57742429/maintainability\n[maintainability-url]: https://codeclimate.com/github/zheeeng/babel-plugin-rewrite-module-path/maintainability\n\n[publish-workflow-image]: https://github.com/zheeeng/babel-plugin-rewrite-module-path/actions/workflows/publish.yml/badge.svg\n\n[test-coverage-image]: https://api.codeclimate.com/v1/badges/d3eaf22221bf57742429/test_coverage\n[test-coverage-url]: https://codeclimate.com/github/zheeeng/babel-plugin-rewrite-module-path/test_coverage\n\n[license-image]: https://img.shields.io/github/license/mashape/apistatus.svg\n[license-url]: https://github.com/zheeeng/babel-plugin-rewrite-module-path/blob/master/LICENSE\n\n[github-issues-image]: https://img.shields.io/github/issues/zheeeng/babel-plugin-rewrite-module-path\n[github-issues-url]: https://github.com/zheeeng/babel-plugin-rewrite-module-path/issues\n\n[bundle-size-image]: https://img.shields.io/bundlephobia/minzip/babel-plugin-rewrite-module-path.svg\n\n[![NPM](https://nodei.co/npm/babel-plugin-rewrite-module-path.png?downloads=true\u0026downloadRank=true\u0026stars=true)](https://nodei.co/npm/babel-plugin-rewrite-module-path/)\n\n\u003c/div\u003e\n\n\u003e Rewrite module resolving path\n\ncase 1: **Rewrite a specific module subpath to `node_modules`**\n\n```diff\n- import foo from \"@monorepo/shared/modules/foo\"\n+ import foo from \"@monorepo/shared/node_modules/foo\"\n```\n\nvia\n\n```json\n// .babelrc.json\n{\n  \"plugins\": [\"babel-plugin-rewrite-module-path\", {\n      \"rewriteMapper\": {\n          \"^@monorepo/shared/modules/\": \"@monorepo/shared/node_modules/\"\n      }\n  }]\n}\n```\n\ncase 2: **Rewrite a cjs importing to esm importing**\n\n```diff\n- import inversify from \"inversify/lib/inversify\"\n+ import inversify from \"inversify/es/inversify\"\n```\n\nvia\n\n```json\n// .babelrc.json\n{\n  \"plugins\": [\"babel-plugin-rewrite-module-path\", {\n      \"rewriteMapper\": {\n          \"^inversify/lib/\": \"inversify/es/\"\n      }\n  }]\n}\n```\n\ncase 3: **Rewrite a lodash importing to lodash fp importing**(for typescript user don't forget to config the paths compiler options of tsconfig.json to adjust intelligence)\n\n```diff\n- import flatMap from \"lodash/flatMap\"\n+ import flatMap from \"lodash/fp/flatMap\"\n```\n\nvia\n\n```json\n// .babelrc.json\n{\n  \"plugins\": [\"babel-plugin-rewrite-module-path\", {\n      \"rewriteMapper\": {\n          \"^lodash/\": \"lodash/fp/\"\n      }\n  }]\n}\n```\n\n---\n\n## Scenes\n\n* Take the advantage of rewriting to shorten the importing path. / Config the importing path to a preferred/reader-friendly one.\n* Rewrite module subpath for hacking the resolving mechanism. e.g. in some bundling situation.\n* It could be an alternative approach to declaring [`exports`](https://nodejs.org/api/packages.html#packages_package_entry_points) filed to tell the node modules resolver how to resolve a `node_modules` package.\n\n    ```json\n    // package.json\n    {\n        \"exports\": {\n            \"modules/*\": \"./node_modules/*\"\n        }\n    }\n    ```\n\n  \u003e For Javascript user, you could use a custom folder name as replacement to `node_modules`, it's common for implementing a monorepo project. e.g.\n\n    ```js\n    // '../../modules/..' will be rewritten to '../../node_modules/..'\n    import singletonModule from '@monorepo/shared/modules/inversify'\n    ```\n\n  \u003e For Typescript user, you need additional works to support intelligence through module path rewriting, see [typescript paths](https://www.typescriptlang.org/tsconfig#paths) for more details. e.g.\n\n    ```json\n    // tsconfig.json\n    {\n        \"compilerOptions\": {\n            \"baseUrl\": \".\",\n                \"paths\": {\n                    // we already have type-safe node_modules aliasing, still need a runtime rewriting for development and production building.\n                    \"@monorepo/shared/modules/*\": [\"./packages/shared/node_modules/*\", \"./packages/shared/node_modules/@types/*\"]\n                }\n        }\n    }\n    ```\n\n---\n\n## Glance at the rewritable statements\n\nSupports these module importing usages:\n\n* `Import Declaration`\n* `Export All Declaration`\n* `Export Named Declaration`\n* `Dynamic Importing`\n* `require`\n* `require.context`\n* `import.meta.glob`\n* `import.meta.globEager`.\n\nrewrite\n\n```js\nimport foo from '@monorepo/shared/modules/foo'\n\nimport * as foo from '@monorepo/shared/modules/foo'\n\nimport '@monorepo/shared/modules/foo'\n\nexport * from '@monorepo/shared/modules/foo'\n\nexport { bar } from '@monorepo/shared/modules/foo'\n\nexport { default as foo } from '@monorepo/shared/modules/foo'\n\nconst foo = await import('@monorepo/shared/modules/foo')\n\nconst { bar } = await import('@monorepo/shared/modules/foo')\n\nimport('@monorepo/shared/modules/foo')\n\nconst foo = require('@monorepo/shared/modules/foo')\n\nconst foo = import.meta.glob('@monorepo/shared/modules/foo')\n\nconst foo = import.meta.globEager('@monorepo/shared/modules/foo')\n\nconst foo = require.context('@monorepo/shared/modules/foo')\n```\n\nto\n\n```js\nimport foo from '@monorepo/shared/node_modules/foo'\n\nimport * as foo from '@monorepo/shared/node_modules/foo'\n\nimport '@monorepo/shared/node_modules/foo'\n\nexport * from '@monorepo/shared/node_modules/foo'\n\nexport { bar } from '@monorepo/shared/node_modules/foo'\n\nexport { default as foo } from '@monorepo/shared/node_modules/foo'\n\nconst foo = await import('@monorepo/shared/node_modules/foo')\n\nconst { bar } = await import('@monorepo/shared/node_modules/foo')\n\nimport('@monorepo/shared/node_modules/foo')\n\nconst foo = require('@monorepo/shared/node_modules/foo')\n\nconst foo = import.meta.glob('@monorepo/shared/node_modules/foo')\n\nconst foo = import.meta.globEager('@monorepo/shared/node_modules/foo')\n\nconst foo = require.context('@monorepo/shared/node_modules/foo')\n```\n\n## Usage\n\n* install:\n\n```sh\nnpm install babel-plugin-rewrite-module-path\n```\n\n* config via babelrc:\n\n```json\n// .babelrc.json\n{\n  \"plugins\": [\"babel-plugin-rewrite-module-path\", {\n      \"rewriteMapper\": {\n          \"/modules/\", \"/node_modules/\"\n      }\n  }]\n}\n```\n\n## Full Option\n\nThe plugin options signatures:\n\n```ts\nexport type RewriteModulePathOptions = {\n    rewriteMapper: Record\u003cstring, string\u003e,\n    transform?: {\n        importDeclaration?: boolean,\n        exportDeclaration?: boolean,\n        dynamicImport?: boolean,\n        importGlob?: boolean,\n        importGlobEager?: boolean,\n        require?: boolean,\n        requireContext?: boolean,\n    }\n}\n```\n\nThe default options:\n\n```ts\nexport const defaultRewriteModulePathOptions = {\n    transform: {\n        importDeclaration: true,\n        exportDeclaration: true,\n        dynamicImport: true,\n        importGlob: true,\n        importGlobEager: true,\n        require: true,\n        requireContext: true,\n    }\n}\n```\n\n## References\n\n* [Nodejs Package Entry Points](https://nodejs.org/api/packages.html#packages_package_entry_points)\n* [tsconfig#compilerOptions#paths](https://www.typescriptlang.org/tsconfig#paths)\n  ","funding_links":[],"categories":["Plugins"],"sub_categories":["Module Resolution"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzheeeng%2Fbabel-plugin-rewrite-module-path","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzheeeng%2Fbabel-plugin-rewrite-module-path","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzheeeng%2Fbabel-plugin-rewrite-module-path/lists"}