{"id":16539923,"url":"https://github.com/hazyzh/babel-plugin-import-separation","last_synced_at":"2026-05-20T14:18:33.547Z","repository":{"id":29356895,"uuid":"120696838","full_name":"Hazyzh/babel-plugin-import-separation","owner":"Hazyzh","description":"a plugin for babel to load on demand from import.","archived":false,"fork":false,"pushed_at":"2022-12-03T16:07:36.000Z","size":235,"stargazers_count":0,"open_issues_count":6,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-15T12:19:50.678Z","etag":null,"topics":["babel","import","plugins"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/Hazyzh.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-02-08T01:57:41.000Z","updated_at":"2019-07-15T04:09:23.000Z","dependencies_parsed_at":"2023-01-14T14:44:53.229Z","dependency_job_id":null,"html_url":"https://github.com/Hazyzh/babel-plugin-import-separation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hazyzh%2Fbabel-plugin-import-separation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hazyzh%2Fbabel-plugin-import-separation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hazyzh%2Fbabel-plugin-import-separation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hazyzh%2Fbabel-plugin-import-separation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Hazyzh","download_url":"https://codeload.github.com/Hazyzh/babel-plugin-import-separation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241774020,"owners_count":20018202,"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":["babel","import","plugins"],"created_at":"2024-10-11T18:51:07.327Z","updated_at":"2026-05-20T14:18:33.480Z","avatar_url":"https://github.com/Hazyzh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# babel-plugin-import-separation\n\n此`plugin`为了按需引入库里面的文件和内容\n\n---\n\n## 在哪里去添加 babel-plugin-import-separation\n\n- [babelrc](https://babeljs.io/docs/usage/babelrc/)\n- [babel-loader](https://github.com/babel/babel-loader)\n\n## 为什么要用 babel-plugin-import-separation\n\n此插件参考[babel-plugin-import](https://github.com/ant-design/babel-plugin-import)相关思路，在使用`babel-plugin-import`中有一些无法解决的问题，这里用另一种思路去解决问题，解决打包过程中一些问题。\n\n- 应用`cdn`资源时候可以按需引入样式文件\n- 不改写代码内部变量，只重新定义了引入变量\n\n在`babel-plugin-import`中，作者重写了引入内容，以新变量代替了原来引入的内容，并且在后续代码中修改了使用到的变量为修改后的名字，效果如下\n\n```javascript\nimport { Button } from 'antd';\nReactDOM.render(\u003cButton\u003exxxx\u003c/Button\u003e);\n\n      ↓ ↓ ↓ ↓ ↓ ↓\n      \nvar _button = require('antd/lib/button');\nReactDOM.render(\u003c_button\u003exxxx\u003c/_button\u003e);\n```\n\n本插件只改变`import`相关内容，用相同变量名覆盖之前引入的内容，不该写后续代码逻辑，转换后代码如下\n```javascript\nimport Button from 'antd/lib/button';\nReactDOM.render(\u003cButton\u003exxxx\u003c/Button\u003e);\n```\n\n而且相关配置支持只引入样式文件，不该写其他内容，满足了`cdn`引入相关资源又自定义样式的业务需求。\n\n\n## 实例\n\n#### `{ \"libraryName\": \"antd\" }`\n\n```javascript\nimport { Button } from 'antd';\nReactDOM.render(\u003cButton\u003exxxx\u003c/Button\u003e);\n\n      ↓ ↓ ↓ ↓ ↓ ↓\n\nimport Button from 'antd/lib/button';\nReactDOM.render(\u003cButton\u003exxxx\u003c/Button\u003e);\n```\n\n#### `{ \"libraryName\": \"antd\", style: true }`\n\n```javascript\nimport { Button } from 'antd';\nReactDOM.render(\u003cButton\u003exxxx\u003c/Button\u003e);\n\n      ↓ ↓ ↓ ↓ ↓ ↓\n\nimport 'antd/lib/button/style';\nimport Button from 'antd/lib/button';\nReactDOM.render(\u003cButton\u003exxxx\u003c/Button\u003e);\n```\n\n## 使用\n\n```bash\nnpm install babel-plugin-import-separation --save-dev\n```\n\n在`babelrc`或者`babel-loader`中写入配置\n\n```js\n{\n  \"plugins\": [[\"import-separation\", options]]\n}\n```\n\n### 支持多个库 \n\n#### `babel6` 及以下版本如下\n\n```js\n{\n  \"plugins\": [\n        [\"import-separation\", \n            [ {...option1}, {...option2} ]\n        ]\n      ]\n}\n```\n#### `babel7` Options 不能为数组，但是你可以添加多次用不同的名字\n\n```js\n{\n  \"plugins\": [\n        [\"import-separation\", options1, 'antd'],\n        [\"import-separation\", options2, 'lodash']\n      ]\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazyzh%2Fbabel-plugin-import-separation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhazyzh%2Fbabel-plugin-import-separation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhazyzh%2Fbabel-plugin-import-separation/lists"}