简体   繁体   中英

How to use babel-plugin-module-resolver to set an alias for the root path and disable other root imports?

I would like to do imports like this import Foo from '~/components/Foo'

https://github.com/tleunen/babel-plugin-module-resolver

If I set my .babelrc to:

{
  "plugins": [
    ["module-resolver", {
      "root": "./",
      "alias": {
        "~/": "./"
      }
    }]
  ]
}

I have also tried (and lots of other combinations):

{
  "plugins": [
    ["module-resolver", {
      "root": ".",
      "alias": {
        "~": "."
      }
    }]
  ]
}

Then I can use it in scripts like so:

import Foo from 'components/Foo'
  1. It doesn't work if I do import Foo from '~/components/Foo' (I would expect it to)
  2. I want to force users to have to prepend the ~/ so there aren't multiple usage patterns.
{
  "plugins": [
    ["module-resolver", {
      "root": ["./",]
      "alias": {
        "~/": "./"
      }
    }]
  ]
}

try it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM