美文网首页工作生活
babel-plugin-root-import react

babel-plugin-root-import react

作者: JsLin_ | 来源:发表于2019-07-04 21:00 被阅读0次
//我们在引入相对路径的时候
import SomeExample from '../../../some/example.js';
const OtherExample = require('../../../other/example.js');
 
// 使用 babel-plugin-root-import 之后
import SomeExample from '~/some/example.js';
const OtherExample = require('~/other/example.js');

usage

npm install babel-plugin-root-import --save-dev # or yarn add babel-plugin-root-import --dev

For recent react-native versions, add it as a plugin in babel.config.js:

module.exports = (api) => {
  api.cache(true);
 
  return {
    plugins: [
      'babel-plugin-root-import',
    ],
  };
}

自定义根文件名


  "plugins": [
    [
      "babel-plugin-root-import",
      {
        "rootPathSuffix": "./",
        "rootPathPrefix": "~"
      }
    ]
  ],

安装配置完后
watchman watch-del-all
npm start -- --reset-cache

相关文章

网友评论

    本文标题:babel-plugin-root-import react

    本文链接:https://www.haomeiwen.com/subject/lksdhctx.html