{"_id":"babel-plugin-import-redirect","_rev":"5-560ba13ff5dd10234fd302b8bdf954be","name":"babel-plugin-import-redirect","description":"Import, export, require path redirect plugin for Babel","dist-tags":{"latest":"1.1.1"},"versions":{"1.0.0":{"name":"babel-plugin-import-redirect","version":"1.0.0","description":"Import, export, require path redirect plugin for Babel","main":"lib/index.js","module":"src/index.js","files":["src","lib"],"scripts":{"test":"jest --config='./__tests__/config.json'","test:coverage":"npm run test -- --coverage --silent","lint":"eslint . --ext .js --ext .jsx --ignore-path .gitignore --cache","build":"babel src --out-dir lib","prepublish":"npm run build","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"author":{"name":"Velenir","email":"velenir@gmail.com"},"license":"MIT","keywords":["babel","babel-plugin","import","redirect","require","export","path","rewrite","resolve","replace","rename","mock","module"],"dependencies":{"babylon":"^6.16.1","resolve":"^1.3.2"},"devDependencies":{"babel-cli":"^6.24.1","babel-core":"^6.24.1","babel-eslint":"^7.2.1","babel-jest":"^19.0.0","babel-plugin-syntax-dynamic-import":"^6.18.0","babel-preset-es2015":"^6.24.1","cz-conventional-changelog":"^2.0.0","eslint":"^3.19.0","eslint-plugin-import":"^2.2.0","eslint-plugin-jest":"^19.0.1","jest":"^19.0.2","semantic-release":"^6.3.2"},"engines":{"node":">=4"},"config":{"commitizen":{"path":"./node_modules/cz-conventional-changelog"}},"repository":{"type":"git","url":"git+https://github.com/Velenir/babel-plugin-import-redirect.git"},"gitHead":"f0d57f003db9cfe8684ad3af9fb208b772864714","bugs":{"url":"https://github.com/Velenir/babel-plugin-import-redirect/issues"},"homepage":"https://github.com/Velenir/babel-plugin-import-redirect#readme","_id":"babel-plugin-import-redirect@1.0.0","_shasum":"0f3ff19f810a9c3a26a889b3351262e21075f892","_from":".","_npmVersion":"4.5.0","_nodeVersion":"7.9.0","_npmUser":{"name":"velenir","email":"velenir@gmail.com"},"dist":{"shasum":"0f3ff19f810a9c3a26a889b3351262e21075f892","tarball":"https://registry.npmjs.org/babel-plugin-import-redirect/-/babel-plugin-import-redirect-1.0.0.tgz","integrity":"sha512-hA18aF6z4NFMlOZpRBsKe1Q/EYnevFCsKI/zLf/GnNb6tcxMIQdk4iaYm+ei6eAXYnXhOlBLvguTA6BGnA2qOQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIBm9763HuLPs0ECF8o73ivVgWxQTwE1bddgEwnsA2y1EAiEAsb5y1/mGzWPxR8MlS6sPWK8QL8Y/kWUMI/5/upenn4Y="}]},"maintainers":[{"name":"velenir","email":"velenir@gmail.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/babel-plugin-import-redirect-1.0.0.tgz_1493395254353_0.9951036549173295"}},"1.1.0":{"name":"babel-plugin-import-redirect","version":"1.1.0","description":"Import, export, require path redirect plugin for Babel","main":"lib/index.js","module":"src/index.js","files":["src","lib"],"scripts":{"test":"jest --config='./__tests__/config.json'","test:coverage":"npm run test -- --coverage --silent","lint":"eslint . --ext .js --ext .jsx --ignore-path .gitignore --cache","build":"babel src --out-dir lib","prepublish":"npm run build","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"author":{"name":"Velenir","email":"velenir@gmail.com"},"license":"MIT","keywords":["babel","babel-plugin","import","redirect","require","export","path","rewrite","resolve","replace","rename","mock","module"],"dependencies":{"babylon":"^6.16.1","resolve":"^1.3.2"},"devDependencies":{"babel-cli":"^6.24.1","babel-core":"^6.24.1","babel-eslint":"^7.2.1","babel-jest":"^19.0.0","babel-plugin-syntax-dynamic-import":"^6.18.0","babel-preset-es2015":"^6.24.1","cz-conventional-changelog":"^2.0.0","eslint":"^3.19.0","eslint-plugin-import":"^2.2.0","eslint-plugin-jest":"^19.0.1","jest":"^19.0.2","semantic-release":"^6.3.2"},"engines":{"node":">=4"},"config":{"commitizen":{"path":"./node_modules/cz-conventional-changelog"}},"repository":{"type":"git","url":"git+https://github.com/Velenir/babel-plugin-import-redirect.git"},"gitHead":"1d39fa8b35f5ff01fd006d5f6c9c7183f7ed6eb5","bugs":{"url":"https://github.com/Velenir/babel-plugin-import-redirect/issues"},"homepage":"https://github.com/Velenir/babel-plugin-import-redirect#readme","_id":"babel-plugin-import-redirect@1.1.0","_shasum":"b836cbdebd227e01b811f560b197da830e301b26","_from":".","_npmVersion":"4.5.0","_nodeVersion":"7.9.0","_npmUser":{"name":"velenir","email":"velenir@gmail.com"},"dist":{"shasum":"b836cbdebd227e01b811f560b197da830e301b26","tarball":"https://registry.npmjs.org/babel-plugin-import-redirect/-/babel-plugin-import-redirect-1.1.0.tgz","integrity":"sha512-8/ThjhxaZKW6cb+03SAuDUSDE65Xc7m2dbhR08QZnQxQusonlch70EKbAwSYtnmgOWxRKV9Woe+oRvsWgI0CKA==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQC/ummwKaBifkF99dW81+3TaB7raLzDNIrcrGno1dGHfgIganbFvelSsZ58xl/zXpU9VuvszQGZSy4mo4lCu/tROKs="}]},"maintainers":[{"name":"velenir","email":"velenir@gmail.com"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/babel-plugin-import-redirect-1.1.0.tgz_1493573627732_0.9691764989402145"}},"1.1.1":{"name":"babel-plugin-import-redirect","version":"1.1.1","description":"Import, export, require path redirect plugin for Babel","main":"lib/index.js","module":"src/index.js","files":["src","lib"],"scripts":{"test":"jest --config='./__tests__/config.json'","test:coverage":"npm run test -- --coverage --silent","lint":"eslint . --ext .js --ext .jsx --ignore-path .gitignore --cache","build":"babel src --out-dir lib","prepublish":"npm run build","semantic-release":"semantic-release pre && npm publish && semantic-release post"},"author":{"name":"Velenir","email":"velenir@gmail.com"},"license":"MIT","keywords":["babel","babel-plugin","import","redirect","require","export","path","rewrite","resolve","replace","rename","mock","module"],"dependencies":{"babylon":"^6.17.4","resolve":"^1.3.2"},"devDependencies":{"babel-cli":"^6.24.1","babel-core":"^6.25.0","babel-eslint":"^8.0.0","babel-jest":"21.0.0","babel-plugin-syntax-dynamic-import":"^6.18.0","babel-preset-es2015":"^6.24.1","cz-conventional-changelog":"^2.0.0","eslint":"4.16.0","eslint-plugin-import":"^2.6.0","eslint-plugin-jest":"21.7.0","jest":"^19.0.2","semantic-release":"^12.2.2"},"engines":{"node":">=4"},"config":{"commitizen":{"path":"./node_modules/cz-conventional-changelog"}},"repository":{"type":"git","url":"git+https://github.com/Velenir/babel-plugin-import-redirect.git"},"gitHead":"cf1a7cb4433614b11d7343e6384bec615e42aa98","bugs":{"url":"https://github.com/Velenir/babel-plugin-import-redirect/issues"},"homepage":"https://github.com/Velenir/babel-plugin-import-redirect#readme","_id":"babel-plugin-import-redirect@1.1.1","_npmVersion":"5.6.0","_nodeVersion":"9.4.0","_npmUser":{"name":"velenir","email":"velenir@gmail.com"},"dist":{"integrity":"sha512-VBbt2UXq3jenD6CEAZulZEKhvGW8MNhoTP7rhqfe8L0GfzVGCXdS5Xlu7NXHqzleiZewANRCG6O+C+cWn8jbdw==","shasum":"a5d72828f0ca87469f8c35b2792e6a527157bfc9","tarball":"https://registry.npmjs.org/babel-plugin-import-redirect/-/babel-plugin-import-redirect-1.1.1.tgz","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDMKHKwMLrBIZ10mbXgiX3natzj5MWK5DhW5AOIne+RcgIhAPrYI7vaEYh2xTYV+7HRkAe6OgLOdlKE0wqwsLcCrrC6"}]},"maintainers":[{"name":"velenir","email":"velenir@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/babel-plugin-import-redirect-1.1.1.tgz_1517817774079_0.23149310681037605"}}},"readme":"# babel-plugin-import-redirect\n\n[![Build Status](https://travis-ci.org/Velenir/babel-plugin-import-redirect.svg?branch=master)](https://travis-ci.org/Velenir/babel-plugin-import-redirect) [![npm version](https://badge.fury.io/js/babel-plugin-import-redirect.svg)](https://badge.fury.io/js/babel-plugin-import-redirect) [![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/) [![dependencies Status](https://david-dm.org/velenir/babel-plugin-import-redirect/status.svg)](https://david-dm.org/velenir/babel-plugin-import-redirect) [![devDependencies](https://david-dm.org/velenir/babel-plugin-import-redirect/dev-status.svg)](https://david-dm.org/velenir/babel-plugin-import-redirect?type=dev) [![Greenkeeper badge](https://badges.greenkeeper.io/Velenir/babel-plugin-import-redirect.svg)](https://greenkeeper.io/) [![codecov](https://codecov.io/gh/Velenir/babel-plugin-import-redirect/branch/master/graph/badge.svg)](https://codecov.io/gh/Velenir/babel-plugin-import-redirect) [![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/velenir/babel-plugin-import-redirect/blob/master/LICENSE)\n\nA [Babel](https://babeljs.io/) plugin that allows to point **import**, **export from** declarations, **require()** and simple dynamic **import()** (only string literal as its argument) calls to custom paths. This can be especially useful in testing — for swapping regular production and development files and modules with their mock implementations.\n\nFor example, this plugin allows to transform:\n\n```\nimport \"./path/to/file\";\nexport {variable} from \"./path/to/different/file\";\nrequire(\"module\");\nimport(\"different_module\");\n```\n\nto\n\n```\nimport \"./mocks/mockFile\";\nexport {variable} from \"./mocks/differentMockFile\";\nrequire(\"./mocks/mockModule\");\nimport(\"yet_another_module\");\n```\n\n# Usage\n\nInstall the plugin with\n\n```\nnpm install --save-dev babel-plugin-import-redirect\n```\n\nThen add it to your babel configuration (e.g. in *.babelrc*). A rather exhaustive setup may look like this:\n\n```\n{\n  \"plugins\": [\n    \"syntax-dynamic-import\",\n    [\"import-redirect\",\n    {\n      \"root\": \"./tests/mocks\",\n      \"extraFunctions\": [\"custom_require_function\", \"SystemJS.import\"],\n      \"promisifyReplacementFor\": \"SystemJS.import\",\n      \"redirect\": {\n        \"connect\": \"./connect.mocked\",\n        \"path/to/(\\\\w+)\\\\.js$\": \"./$1.mocked\",\n        \"\\\\.css$\" : false,\n        \"path/to/globals\": {\"MY_GLOBAL_1\": true, \"MY_GLOBAL_2\": 42}\n      }\n    }]\n  ]\n}\n```\n\n> Transforming dynamic **import()** requires that [babel-plugin-syntax-dynamic-import](https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-dynamic-import) be included in `plugins` before `import-redirect`\n\nNow when you transpile your source files, any path inside **import**, **export from** declarations, **require()** and dynamic **import()** calls that matches a *redirect.key* is resolved to point to the file from a corresponding *redirect.value*.\n\n## Example\n\nTo provide an example, given a project structure of\n\n```\n./\n  node_modules/\n    connect/\n  tests/\n    mocks/\n      connect.mocked.js\n      lib.mocked.js\n  src/\n    index.js\n    helpers/\n      globals.js\n    libs/\n      lib.js\n    style.css\n  .babelrc\n```\n\nwith *index.js* of\n\n```\nimport \"./style.css\";\nimport connect from \"connect\";\nexport {default as libFunction} from \"./helpers/lib\";\nimport {MY_GLOBAL_1, MY_GLOBAL_2} from \"./helpers/globals\";\n// ...\n```\n\nand *.babelrc* of\n\n```\n{\n  \"plugins\": [\n    [\"import-redirect\",\n    {\n      \"root\": \"./tests/mocks\",\n      \"redirect\": {\n        \"connect\": \"./connect.mocked\",\n        \"/libs/(\\\\w+)\\\\.js$\": \"./$1.mocked\",\n        \"\\\\.css$\" : false,\n        \"helpers/globals.js$\": {\"MY_GLOBAL_1\": true, \"MY_GLOBAL_2\": 42}\n      }\n    }]\n  ]\n}\n```\n\nThe *index.js* will transpile to\n\n```\nimport connect from \"../tests/mocks/connect.mocked\";\nexport {default as libFunction} \"../tests/mocks/lib.mocked\";\nconst {MY_GLOBAL_1, MY_GLOBAL_2} = {\"MY_GLOBAL_1\": true, \"MY_GLOBAL_2\": 42};\n// ...\n```\n\nThe transpilation to make it happen will be performed as follows:\n\n1. **import \"./style.css\";**``\n    1. `./style.css` path is resolved to `project/src/style.css` absolute path\n    1. `project/src/style.css` is matched against `new RegExp(\"\\\\.css$\")`\n    1. the corresponding value of false triggers removal of the import declaration\n> removed\n\n2. **import connect from \"connect\";**\n    1. `connect` path is resolved to `project/node_modules/connect/...` absolute path\n    2. the path matches against `new RegExp(\"connect\")`\n    3. the redirected path of `./test/mocks/connect.mocked` is resolved relative to `index.js` as  `../tests/mocks/connect.mocked`\n    4. the path is changed to `../tests/mocks/connect.mocked`\n> replaced with `import connect from \"../tests/mocks/connect.mocked\";`\n\n3. **import libFunction from \"./helpers/lib\";**\n    1. `./helpers/lib` path is resolved to `project/node_modules/helpers/lib.js` absolute path\n    2. the path matches against `new RegExp(\"/libs/(\\\\w+)\\\\.js$\")`\n    3. as the redirect value contains a replacement group (`$1`), it is converted to `./tests/mocks/lib.mocked`\n    4. the redirected path of `./tests/mocks/lib.mocked` is resolved relative to `index.js` as  `../tests/mocks/lib.mocked`\n    5. the path is changed to `../tests/mocks/lib.mocked`\n> replaced with `import libFunction from \"../tests/mocks/lib.mocked\";`\n\n4. **import {MY_GLOBAL_1, MY_GLOBAL_2} from \"./helpers/globals\";**\n    1. `./helpers/globals` path is resolved to `project/src/helpers/globals.js` absolute path\n    2. `project/src/helpers/globals.js` matches against `new RegExp(\"helpers/globals.js$\")`\n    3. the corresponding value of an object triggers replacement of the import declaration with a variable declaration\n> replaced with `const {MY_GLOBAL_1, MY_GLOBAL_2} = {\"MY_GLOBAL_1\": true, \"MY_GLOBAL_2\": 42};`\n\n## Options\n\n```\n{\n  \"root\": String,\n  \"extraFunctions\": String | Array<String>,\n  \"promisifyReplacementFor\": String | Array<String>,\n  \"redirect\": {\n    matchPattern: replacement\n  },\n  \"extensions\": Array<String>,\n  \"suppressResolveWarning\": Boolean\n}\n```\n\n+ `root` : path, relative to which `replacement` paths are resolved. Equals project root folder by default.\n+ `extraFunctions` : functions to consider when matching against keys in redirect in addition to **import**, **export from** declarations, **require()** and dynamic **import()**. It can be a simple function name (`\"custom_require\"`) or an object.property pair (`\"SystemJS.import\"`).\n+ `promisifyReplacementFor` : functions, in addition to `import()`, for which `replacement` Objects should be wrapped in `Promise.resolve()`.\n+ `redirect` : Object with `matchPattern` keys and `replacement` values.\n+ `extensions`: Array of extensions to use for resolving filenames. Equals `[\".js\", \".jsx\", \".es\", \"es6\"]` by default, providing custom extensions will override the default.\n+ `suppressResolveWarning`: Boolean, `false` by default. During path resolution plugin shows a warning when it can't find a module. It will still do its best to resolve to the right path. This option suppresses that warning.\n\n#### matchPattern\n\nA `String` to be used as a pattern in a `RegExp`. This `RegExp` will be matched against the source of **import** and **export from** declarations and the first argument of **require()**, **import()** and functions from `extraFunctions`. If the match is successful the whole expression will be transformed depending on the corresponding `replacement`.\n\nTake care to escape (`\\`) every special character, namely backslash (`\\`). That is, escape twice every time you would escape once in a literal regexp. E.g. a `RegExp` constructed from `\"\\\\w+\"` pattern is equivalent to `/\\w+/`, to use backslash in your pattern escape it like so `\"\\\\\\\\\"`.\n\n> To match *only* the node module `required_module` and not accidentally pick up paths that would otherwise match `/required_module/` (e.g. `\"./src/my_required_module/index.js\"`), it is recommended to specialize the pattern like this: `\"/node_modules/required_module/\"`.\n\n#### replacement\nCan be\n+ A `String` path to a file to be used in place of the originally `require`d / `import`ed file. The path will be resolved relative to `root` if provided or to project root folder (`process.cwd()`) otherwise.\nIf `replacement` contains a replacement group (e.g. `$1`), a corresponding parenthesized match result from the `matchPattern` will be substituted in prior to resolving the path.\nE.g. given a project structure of\n\n```\n./\n  src/\n    index.js\n    lib.js\n  mocks/\n    lib.js\n```\n\n`require(\"./lib\");` inside `./src/index.js` file when matched against `\"/(\\\\w+).js\": \"./mocks/$1\"` with no `root` provided will transpile to `require(\"../mocks/lib\");`.\n\n+ `false`, which will result in removal of simple `import` declarations, `require()`, `import()` and custom require function calls without side effects. That is, functions which are not part of a larger expression:\n\n```\n// will be removed\nrequire(\"path/to/file\");\nimport(\"path/to/file\");\n\n// won't be removed\nconst lib = require(\"path/to/file\");\nrequire(\"path/to/file\").prop;\nfn(require(\"path/to/file\"));\nimport(\"path/to/file\").then(module => module.default);\n```\n\nand simple, non-named, non-namespace, non-default import statements:\n\n```\n// will be removed\nimport \"path/to/file\";\n\n// won't be removed\nimport lib from \"pat/to/file\";\nimport * as lib from \"pat/to/file\";\nimport {lib} from \"pat/to/file\";\n```\n\n+ An `Object`, which will result in removal of simple `import` declarations, `require()`, `import()` and custom require function calls without side effects (same as for `false`) and in replacement of default, named, namespace `import`s, `require()`, `import()` and custom require function calls with these objects. This `Object` must be JSON-serialazable.\nAdditionally **replacement** objects for `import()` calls and calls of custom functions from `promisifyReplacementFor` will be wrapped in `Promise.resolve()`.\n\nE.g. for a `\"path/to/file\": {\"key\": val}` **matchPattern - replacement** pair\n\n| will be removed                                          | was removed\n|:---------------------------------------------------------|:-|\n| `import \"path/to/file\";`                                 |  |\n| `require(\"path/to/file\");`                               |  |\n| `import(\"path/to/file\");`                                |  |\n| **will be replaced**                                     | **was replaced with**\n| `const lib = require(\"path/to/file\");`                   | `const lib = {\"key\": val};`\n| `require(\"path/to/file\").prop; `                         | `({\"key\": val}).prop;`\n| `fn(require(\"path/to/file\"));`                           | `fn({\"key\": val});`\n| `import(\"path/to/file\").then(module => module.default);` | `Promise.resolve({\"key\": val}).then(module => module.default);`\n| `import lib from \"pat/to/file\";`                         | `const {default: lib} = {\"key\": val};`\n| `import * as lib from \"pat/to/file\";`                    | `const lib = {\"key\": val};`\n| `import {lib} from \"pat/to/file\";`                       | `const {lib} = {\"key\": val};`\n| `import lib, {lib1 as lib2} from \"./style.css\";`         | `const {default: lib, lib1: lib2} = {\"key\": val};`\n| `import lib, * as libAll from \"./style.css\";`            | `const libAll = {\"key\": val}, {default: lib} = libAll;`\n\n> To summarize,\n> + a `String` replacement handles **import**, **export from** declarations, **require()**, simple dynamic **import()** and custom function calls.\n> + `false` replacement removes aforementioned declarations and function calls, except for **export from**, without side effects.\n> + `Object` replacement does the same as `false` and also replaces relevant expressions with side effects.\n","maintainers":[{"name":"velenir","email":"velenir@gmail.com"}],"time":{"modified":"2022-06-13T04:01:39.269Z","created":"2017-04-28T16:00:55.107Z","1.0.0":"2017-04-28T16:00:55.107Z","1.1.0":"2017-04-30T17:33:48.550Z","1.1.1":"2018-02-05T08:02:55.153Z"},"homepage":"https://github.com/Velenir/babel-plugin-import-redirect#readme","keywords":["babel","babel-plugin","import","redirect","require","export","path","rewrite","resolve","replace","rename","mock","module"],"repository":{"type":"git","url":"git+https://github.com/Velenir/babel-plugin-import-redirect.git"},"author":{"name":"Velenir","email":"velenir@gmail.com"},"bugs":{"url":"https://github.com/Velenir/babel-plugin-import-redirect/issues"},"license":"MIT","readmeFilename":"README.md"}