//我们在引入相对路径的时候
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
网友评论