esbuild 是否提供类似于 webpack 中的resolve.alias 选项的功能?

发布于 2025-01-20 17:21:31 字数 589 浏览 5 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

开始看清了 2025-01-27 17:21:32

Esbuild现在确实提供了配置中的“别名”,您可以在其中设置这些别名引用。

在此处查看 esbuild别名

Esbuild does now provide an "alias" in configuration, where you can setup these alias references.

See it here Esbuild alias

眼眸里的快感 2025-01-27 17:21:31

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 in tsconfig.json.
https://github.com/evanw/esbuild/issues/2191

始终不够爱げ你 2025-01-27 17:21:31

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

夏至、离别 2025-01-27 17:21:31

是的,您可以,但您必须确保启用 bundle 选项。

如果您使用的是 JavaScript,请创建 jsconfig.json 文件;如果您使用的是 TypeScript,请创建 tsconfig.json 文件。

并使用 "paths" 编译器选项,如下所示:

{
  "compilerOptions": {
    "target": "ES2016",
    "module": "ESNext",
    "moduleResolution": "Bundler", // << you should enable this
    "esModuleInterop": true,
    "rootDir": "./src",
    "outDir": "./dist",
    "paths": {
      "@/*": ["./src/*"] // << enable this (or your own)
    }
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules/**/*"]
}

然后,在运行 esbuild 时,确保添加 --bundle 标志:

// package.json
"scripts": {
  "build": "esbuild --bundle ./index.ts # or for js, index.js"
}
$ npm run build

这将解析如下导入:

import log from '@/tools'

致:

import log from '../../../tools'

请注意,仅当您使用 --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:

{
  "compilerOptions": {
    "target": "ES2016",
    "module": "ESNext",
    "moduleResolution": "Bundler", // << you should enable this
    "esModuleInterop": true,
    "rootDir": "./src",
    "outDir": "./dist",
    "paths": {
      "@/*": ["./src/*"] // << enable this (or your own)
    }
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules/**/*"]
}

Then, when running esbuild, make sure to add the --bundle flag:

// package.json
"scripts": {
  "build": "esbuild --bundle ./index.ts # or for js, index.js"
}
$ npm run build

This will resolve imports like this:

import log from '@/tools'

To:

import log from '../../../tools'

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文