简体   繁体   English

如何在webpack模块中加载备用jquery?

[英]How do you load an alternate jquery in a webpack module?

I need to load alternate versions of jQuery other than the one in my node_modules. 我需要加载除了node_modules中的jQuery之外的其他版本的jQuery。 I have a local copy of the script but I am unable to get it to load in my webpack app.ts. 我有一个脚本的本地副本,但我无法在我的webpack app.ts中加载它。 I have removed the global jQuery and resolve path for node_modules in my webpack config since I do not want the version from node_modules. 我已经删除了全局jQuery并解析了我的webpack配置中node_modules的路径,因为我不想使用node_modules中的版本。 However I can't figure out how I can pass the loaded jquery to the scripts that require it. 但是我无法弄清楚如何将加载的jquery传递给需要它的脚本。

I have multiple entry points (app.ts) each which needs to be able to load a different jquery so I can not define jquery in the webpack config as I would like to use the same config for processing all my entry points. 我有多个入口点(app.ts),每个入口点都需要能够加载不同的jquery,所以我无法在webpack配置中定义jquery,因为我想使用相同的配置来处理我的所有入口点。

app.ts app.ts

import jquery from '../../../js/jquery-3.3.1.js';

// tried this but didn't help
// window.$ = jquery;
// window.jquery = jquery;

// these two require jquery but fail, it only works if I add back in
// the webpack config to find jquery in node_modules which is what I 
// don't want. popper.js has the same issue as jquery.
import '../../../js/popper-1.12.9.min.js';
import '../../../js/bootstrap-4.0.0.js';
import './js/jquery.matchHeight-min.js';

...

I think there are several approaches to this issue. 我认为这个问题有几种方法。

  • Add some scripting. 添加一些脚本。 It's possible to create a simple function that will set different resolve.alias values for different given entry points. 可以创建一个简单的函数,为不同的给定entry点设置不同的resolve.alias值。 and then use ProvidePlugin or even imports-loader in order to do shimming and set it globally. 然后使用ProvidePlugin甚至imports-loader来进行匀场并全局设置。 shimming docs 填补文档
  • Use expose-loader . 使用expose-loader with that you can require different scripts yet they all will be exposed with the same variable name. 有了它你可以要求不同的脚本,但它们都将使用相同的变量名称公开。

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

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