Babel plugin (babel-plugin-module-alias) with React Native

I want to use babel-plugin-module-alias to convert module paths from ../../../../components/footo app/components/fooin my React Native project.

I tried changing the babel configuration in two places regardless, it doesn’t work.

One, at the root of my project, next to index.ios.js:

{
  "plugins": [
    ["babel-plugin-module-alias", [
      { "src": "./app", "expose": "app" }
    ]]
  ]
}

And two, in /node_modules/react-native/packager/react-packager/.babelrc:

{
  "retainLines": true,
  "compact": true,
  "comments": false,
  "plugins": [
    ["babel-plugin-module-alias", [
      { "src": "./app", "expose": "app" }
    ]],
    "syntax-async-functions",
    "syntax-class-properties",
    "syntax-trailing-function-commas",
    "transform-class-properties",
    "transform-es2015-arrow-functions",
    "transform-es2015-block-scoping",
    "transform-es2015-classes",
    "transform-es2015-computed-properties",
    "transform-es2015-constants",
    "transform-es2015-destructuring",
    ["transform-es2015-modules-commonjs", {"strict": false, "allowTopLevelThis": true}],
    "transform-es2015-parameters",
    "transform-es2015-shorthand-properties",
    "transform-es2015-spread",
    "transform-es2015-template-literals",
    "transform-flow-strip-types",
    "transform-object-assign",
    "transform-object-rest-spread",
    "transform-react-display-name",
    "transform-react-jsx",
    "transform-regenerator",
    "transform-es2015-for-of"
  ],
  "sourceMaps": false
}

It does not cause any errors, just trying to require the module as such to import MyComponent from 'app/components/MyComponent';throw an error:

Unable to resolve module app/components/MyComponent from .... Invalid directory /Users/node_modules/app/components/MyComponent.

I tried to use every cache I know about:

watchman watch-del-all
rm -rf $TMPDIR/react-*
./node_modules/react-native/packager/packager.sh start --resetCache

We will be very grateful for any further ideas!

+4
source share
1 answer

npm/ node/response-native/ :

react-native: ^0.29.0
react: 15.2.1
npm: 3.10.3
node: 6.3.0

:

watchman watch-del-all
npm start -- --reset-cache

.

: https://github.com/facebook/react-native/issues/4968 https://github.com/tleunen/babel-plugin-module-alias/issues/29

+3

Source: https://habr.com/ru/post/1629922/


All Articles