esbuild 是否提供类似于 webpack 中的resolve.alias 选项的功能?
Esbuild是否在WebPack中提供了诸如Resolve.alias选项之类的功能?
const path = require('path');
module.exports = {
//...
resolve: {
alias: {
Utilities: path.resolve(__dirname, 'src/utilities/'),
Templates: path.resolve(__dirname, 'src/templates/'),
},
},
};
现在,导入时,而不是使用相对路径:
import Utility from '../../utilities/utility';
您可以使用别名:
import Utility from 'Utilities/utility';
Does esbuild provide a feature like the resolve.alias option in webpack?
const path = require('path');
module.exports = {
//...
resolve: {
alias: {
Utilities: path.resolve(__dirname, 'src/utilities/'),
Templates: path.resolve(__dirname, 'src/templates/'),
},
},
};
Now, instead of using relative paths when importing like so:
import Utility from '../../utilities/utility';
you can use the alias:
import Utility from 'Utilities/utility';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Esbuild现在确实提供了配置中的“别名”,您可以在其中设置这些别名引用。
在此处查看 esbuild别名
Esbuild does now provide an "alias" in configuration, where you can setup these alias references.
See it here Esbuild alias
esbuild 目前不提供解析别名的专用方法,但支持 tsconfig.json 中的路径。
https://github.com/evanw/esbuild/issues/2191
esbuild does currently not offer a dedicated way to resolve aliases but does support
paths
intsconfig.json
.https://github.com/evanw/esbuild/issues/2191
Esbuild在构建选项中没有类似的选项,但它提供了控制通过插件解决路径的方法。您可以写简单自己的插件或使用 esbuild-plugin-alias
esbuild have no similar option in build options but it provides hancy way to control paths resolving via plugins. You can either write simple own plugin or use existing one such as esbuild-plugin-alias
是的,您可以,但您必须确保启用
bundle
选项。如果您使用的是 JavaScript,请创建 jsconfig.json 文件;如果您使用的是 TypeScript,请创建 tsconfig.json 文件。
并使用
"paths"
编译器选项,如下所示:然后,在运行 esbuild 时,确保添加
--bundle
标志:这将解析如下导入:
致:
请注意,仅当您使用
--bundle
标志时才支持此功能,否则它将不会解析任何别名,并且在构建过程中不会向您显示任何警告或错误。如果您只为后端编写代码,并且您不打算捆绑您的代码,因为您根本不在乎,那么,更好的解决方案是使用外部插件,或者第三方 npm 包,例如 ts-alias ,这非常酷& ;便于使用。
如果您正在为前端执行此操作,并且没有使用其他捆绑器(例如 webpack 或 SWC),并且使用 esbuild 的主要目的是捆绑代码,那么恭喜您已准备好开始。
Yes you can, but you have to make sure that the
bundle
option is enabled.If you're using JavaScript, create jsconfig.json file, and if you're using TypeScript, create a tsconfig.json file.
and use the
"paths"
compiler option as bellow for example:Then, when running esbuild, make sure to add the
--bundle
flag:This will resolve imports like this:
To:
Please note that this is only supported if you're using the
--bundle
flag, otherwise it will not resolve any alias and it will not show you any warning or an error during build.If you're only writing code for the back-end, and you're for example not going to bundle your code, because simply you don't care, then, a better solution to you would be using an external plugin, or a third-party npm package for that, such as ts-alias which is pretty cool & easy to use.
If you're doing that for the front-end and you're not using other bundler like webpack or swc, and your main purpose of using esbuild is to bundle your code, then congratulations you're ready to go.