简体   繁体   English

如何在 babel 的“模块解析器”中参数化别名路径?

[英]How to parameterize aliased paths in babel's `Module Resolver`?

Using babel-plugin-module-resolver I would like to alias paths using more than one paramters.使用babel-plugin-module-resolver我想使用多个参数对路径进行别名。

I have the following paths:我有以下路径:

/foo/components/filename.jsx /foo/components/filename.jsx

/bar/components/filename.jsx /bar/components/filename.jsx

I would like to alias these paths with a single regex in such a way that:我想用一个正则表达式给这些路径起别名,这样:

import * from '@foo/filename' ---> import * from '/foo/components/filename'导入 * 从 '@foo/filename' ---> 导入 * 从 '/foo/components/filename'

Namely, @{foldername}/{filename} becomes /{foldername}/components/{filename}即, @{foldername}/{filename}变为/{foldername}/components/{filename}

Thank you谢谢

You can make configuration in.babelrc file可以在.babelrc 文件中进行配置

{ "plugins": [ [ "module-resolver", { "cwd": "babelrc", "root": ["./"], "alias": { "@foo": "./foo/components", "@bar": "./bar/components", } } ] ] }

Just make sure you fill '"root":' where./foo and./bar placed只需确保填写 '"root":' where ./foo 和 ./bar 放置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “babel-plugin-module-resolver”和“tsconfig-paths”有什么不同? - what is different between 'babel-plugin-module-resolver' and 'tsconfig-paths'? Babel 模块解析器不适用于 react-native - Babel module resolver not working with react-native Babel 模块解析器无法按预期工作 - Babel module resolver doesn't work as expected Babel 模块解析器不适用于 Mocha 测试 - Babel module-resolver not working with Mocha tests 如何使用 babel-plugin-module-resolver 为根路径设置别名并禁用其他根导入? - How to use babel-plugin-module-resolver to set an alias for the root path and disable other root imports? React Native-未定义的babel-plugin-module-resolver'1' - React native - babel-plugin-module-resolver '1' of undefined babel-plugin-module-resolver 添加额外的../ 到路径 - babel-plugin-module-resolver adding extra ../ to path PhpStorm-b​​abel插件模块解析器-babel插件模块别名 - PhpStorm - babel-plugin-module-resolver - babel-plugin-module-alias 如何将 TypeScript 中的别名路径转换为 JavaScript? - How do I transpile aliased paths in TypeScript to JavaScript? React Native Typescript babel-plugin-module-resolver 错误:找不到模块或其对应的类型声明 - React Native Typescript babel-plugin-module-resolver error: Cannot find module or its corresponding type declerations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM