账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    How to get entrypoint name in webpack loader?
    40
    0
    How to get entrypoint name in webpack loader?like this example:https://github.com/webpack/we...Is there any to achieve that?
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • °耐↘吖頭 普通会员 1楼

      In a webpack loader, you typically don't have direct access to the entrypoint name since loaders operate on individual modules rather than the full build context. However, you can use webpack's loaderContext object which provides some meta information.

      Webpack v4 and below:

      ```javascript module.exports = function(content) { const { resourcePath } = this; // You might be able to derive the entrypoint by analyzing the resourcePath, // but there is no straightforward way to get the exact entrypoint name. };

      ```

      Webpack v5 introduced the getOptions() method that allows you to pass custom data through the webpack config:

      ```javascript module.exports = function(source, map, meta) { const options = this.getOptions(); const entrypointName = options.entrypoint; // Assuming you've passed it here

      // Your loader logic }; ```

      In your webpack configuration file, you could set up a custom option for your loader and pass the entrypoint name:

      javascript // webpack.config.js module.exports = { // ... module: { rules: [ { test: /\.your-extension$/, use: [ { loader: 'your-loader', options: { entrypoint: process.env.WEBPACK_ENTRYPOINT || '<derived from your config>' } } ] } ] }, // ... };

      Please note that getting the actual entrypoint name inside a loader is not trivial as loaders run in a per-module context. The above workaround passes the entrypoint name as an option, but it doesn't reflect dynamic changes or multiple entrypoints. If you need such information, consider using a plugin instead, which has more global build context available.

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部