简体   繁体   English

如何正确暴露 jQuery 和 Webpack 5?

[英]How to correctly expose jQuery with Webpack 5?

Is there any specific way to expose jQuery with Webpack 5?有什么具体的方法可以将 jQuery 与 Webpack 5 公开吗?

It used to work on Webpack 4 OK, with the config bellow, but it shows the Uncaught Reference Error: jQuery is not defined error now with 5.它曾经在 Webpack 上工作 4 OK,配置如下,但它显示未捕获的Uncaught Reference Error: jQuery is not defined错误现在与 5。

module: {
    rules: [
      {
        test: require.resolve('jquery'),
        loader: 'expose-loader',
        options: {
          exposes: ['$', 'jQuery'],
        },
      },

Confirming @Sam Chen 's comment above, Webpack 5 (as of 5.75) does not require expose-loader to make $ available as an import.确认@Sam Chen 上面的评论,Webpack 5(截至 5.75)不需要 expose-loader 来使$作为导入可用。 It was simplest to uninstall the loader, remove it from webpack.config.js, and add卸载加载器最简单,将其从 webpack.config.js 中删除,然后添加

import $ from 'jquery';

to all files that used it.所有使用它的文件。 This can get messy in the case of TypeScript projects with @types/jquery, as pages where the import is missing may not warn about it.对于带有 @types/jquery 的 TypeScript 项目,这可能会变得混乱,因为缺少导入的页面可能不会对此发出警告。

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

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