{"_id":"@aminya/babel-plugin-replace-import-extension","name":"@aminya/babel-plugin-replace-import-extension","dist-tags":{"latest":"1.2.0"},"versions":{"1.2.0":{"name":"@aminya/babel-plugin-replace-import-extension","version":"1.2.0","description":"Babel plugin to replace extension of file name written in import statement and dynamic import","main":"src/index.js","repository":{"type":"git","url":"git+https://github.com/aminya/babel-plugin-replace-import-extension.git"},"keywords":["babel","plugin","replace","convert","transform","rewrite","import","extension","ext","add","append","remove","delete","file","name","filename","js","mjs","cjs","require","es","module","commonjs","dynamic"],"author":{"name":"silane"},"license":"Apache-2.0","devDependencies":{"@babel/core":"^7.17.10","@babel/plugin-transform-modules-commonjs":"^7.17.9","jest":"^28.1.0"},"scripts":{"test":"jest"},"bugs":{"url":"https://github.com/aminya/babel-plugin-replace-import-extension/issues"},"homepage":"https://github.com/aminya/babel-plugin-replace-import-extension#readme","_id":"@aminya/babel-plugin-replace-import-extension@1.2.0","_integrity":"sha512-Lmi9bryhA3BrBbScv8IGgFGI0uFrMWd/hrlmyaHpT7rIvjGpUm5Dyo59wIGxr73sQgpBC8d9WpTjgTmSo9D+Qg==","_resolved":"/tmp/b7f968bc0569a0fabfdcb5b95a3ce6ac/aminya-babel-plugin-replace-import-extension-1.2.0.tgz","_from":"file:aminya-babel-plugin-replace-import-extension-1.2.0.tgz","_nodeVersion":"20.8.1","_npmVersion":"10.1.0","dist":{"integrity":"sha512-Lmi9bryhA3BrBbScv8IGgFGI0uFrMWd/hrlmyaHpT7rIvjGpUm5Dyo59wIGxr73sQgpBC8d9WpTjgTmSo9D+Qg==","shasum":"23cdd8a43a40e637fcbaea6254f3402f26432364","tarball":"https://registry.npmjs.org/@aminya/babel-plugin-replace-import-extension/-/babel-plugin-replace-import-extension-1.2.0.tgz","fileCount":5,"unpackedSize":25514,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIATD/7SWcJapuM0R/8LRJRxEL9aJTJkXtCLuzilf3CB1AiEA6JBsw+Dzi1NqSGXqRZ5BXbhK9MVdzkBkK5rXAwuYbHk="}]},"_npmUser":{"name":"aminya","email":"aminyahyaabadi74@gmail.com"},"directories":{},"maintainers":[{"name":"aminya","email":"aminyahyaabadi74@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/babel-plugin-replace-import-extension_1.2.0_1697409302690_0.8340016922782758"},"_hasShrinkwrap":false}},"time":{"created":"2023-10-15T22:35:02.563Z","1.2.0":"2023-10-15T22:35:02.964Z","modified":"2023-10-15T22:35:03.334Z"},"maintainers":[{"name":"aminya","email":"aminyahyaabadi74@gmail.com"}],"description":"Babel plugin to replace extension of file name written in import statement and dynamic import","homepage":"https://github.com/aminya/babel-plugin-replace-import-extension#readme","keywords":["babel","plugin","replace","convert","transform","rewrite","import","extension","ext","add","append","remove","delete","file","name","filename","js","mjs","cjs","require","es","module","commonjs","dynamic"],"repository":{"type":"git","url":"git+https://github.com/aminya/babel-plugin-replace-import-extension.git"},"author":{"name":"silane"},"bugs":{"url":"https://github.com/aminya/babel-plugin-replace-import-extension/issues"},"license":"Apache-2.0","readme":"# @aminya/babel-plugin-replace-import-extension\n\nNote: this is a temporary fork until [this pull request](https://github.com/silane/babel-plugin-replace-import-extension/pull/9) is merged.\n\nBabel plugin to replace extension of file name written in import statement and\ndynamic import.\n\n## Installation\n```shell\nnpm install --save-dev @aminya/babel-plugin-replace-import-extension\n```\n\n## Example\nWith the option:\n```json\n{ \"extMapping\": { \".js\": \".mjs\" }}\n```\n\n### In\n```javascript\nimport { foo } from './module1.js';\nexport { bar } from './module2.js'; // Works for re-exporting\nconst promise = import('./module3' + '.js'); // Also works for dynamic import!\n```\n\n### Out\n```javascript\nimport { foo } from './module1.mjs';\nexport { bar } from './module2.mjs';\n\n// In dynamic import, function to replace extension is inserted.\n// Note the actual code is not exactly the same.\nconst promise = import(__transformExtension('./module3' + '.js'));\n```\n\n## Why We Need This Plugin?\nWhen you develop a npm package that includes both ESModule and CommonJS version\nof the code, there is two ways to tell Node which file is which version.\n\n- Distinguish files by their extension, `mjs` for ESModule and `cjs` for\n  CommonJS.\n- If two versions are located in separate directories, put a `package.json`\n  with a `type` field specified to the directory.\n\nIf you choose the former and you write your code in ESModule and transpile it\nto CommonJS, you have to change the extension of the files while transpiling.\n\nIn Babel CLI, extension of the output file name can be changed with\n`--out-file-extension` option. But the file name referenced inside the code\nis not changed. In this case, this plugin comes into play.\n\nNote that the conversion is performed only on relative file name\n(starts with `./` or `../`), because built-in packages or packages importing\nfrom `node_modules` should not be converted.\n\n## Usage\nIf project root `package.json` has `type` field of `module`, Babel config of\n```json\n{\n  \"plugins\": [\n    [\"replace-import-extension\", { \"extMapping\": { \".js\": \".cjs\" }}],\n    [\"@babel/transform-modules-commonjs\"]\n  ]\n}\n```\nwill convert the file extension from `.js` to `.cjs` and convert ESModule to\nCommonJS, allowing both version's code exist together while Node can handle\neach versions correctly. (`@babel/plugin-transform-modules-commonjs` must be\ninstalled.) Or if you also need other translations, `@babel/env` preset can be\nused together like,\n```json\n{\n  \"presets\": [[\"@babel/env\"]],\n  \"plugins\": [\n    [\"replace-import-extension\", { \"extMapping\": { \".js\": \".cjs\" }}]\n  ]\n}\n```\n\n\nIf project root `package.json` has no `type` field or has `type` field of\n`cjs`, ESModule files must be explicitly marked by `mjs` extension, which can\nbe done by Babel config of\n```json\n{\n  \"plugins\": [\n    [\"replace-import-extension\", { \"extMapping\": { \".js\": \".mjs\" }}]\n  ]\n}\n```\nOnce again, `--out-file-extension` option must be used together to change the\noutput file extension.\n\n## Supporting both `mjs` and `cjs` in the same package\n\nIf you are using `.mjs` for your source files, you can use babel to generate `.cjs` files for backwards compatibility:\n\n```json\n{\n  \"presets\": [[\"@babel/env\"]],\n  \"plugins\": [\n    [\"replace-import-extension\", { \"extMapping\": { \".mjs\": \".cjs\" }}]\n  ]\n}\n```\n\nIn your `package.json` specify the entries accordingly:\n\n```json\n{\n  \"main\": \"dist/index.cjs\",\n  \"module\": \"src/index.mjs\",\n  \"source\": \"src/index.mjs\",\n  \"exports\": {\n    \".\": {\n      \"require\": \"dist/index.cjs\",\n      \"import\": \"src/index.mjs\"\n    },\n    \"src/index.mjs\": {\n      \"import\": \"src/index.mjs\"\n    },\n    \"dist/index.cjs\": {\n      \"require\": \"dist/index.cjs\",\n      \"import\": \"dist/index.cjs\"\n    }\n  },\n  \"scripts\": {\n    \"build.cjs\": \"babel -d dist/ src/ --out-file-extension .cjs\",\n    \"prepare\": \"npm run build.cjs\"\n  }\n}\n```\n\n## Options\n### `extMapping`\n`Object`, defaults to `{}`.\n\nMapping of original extension to converted extension.\nLeading `.` is mandatory.\n\nBoth the original and the converted extensions can be empty string `''`, which means\nno extension. You can use this feature to add or remove extension.\n","readmeFilename":"README.md"}