简体   繁体   English

如何使用 webpack 全局公开 $.ui (jquery-ui)

[英]How to expose $.ui (jquery-ui) globally with webpack

I'm using npm, TypeScript and Webpack.我正在使用 npm、TypeScript 和 Webpack。

I can use Expose Loader to expose jQuery globally:我可以使用Expose Loader来全局公开 jQuery:

import "expose-loader?$!jquery"
import "expose-loader?jQuery!jquery"

Then I can import bootstrap:然后我可以导入引导程序:

import "bootstrap"

And this allows $ , jQuery , and $().popover (a boostrap function in the jQuery namespace) to be visible globally to external js files or the browser console.这允许$jQuery$().popover popover (jQuery 命名空间中的一个 boostrap 函数)对外部 js 文件或浏览器控制台全局可见。

However, I can't find a way to expose $.ui in the same manner.但是,我找不到以相同方式公开$.ui的方法。

I've tried:我试过:

import "jquery-ui" //needs building, from 1.12 on they support importing indivisual modules
import "jquery-ui/ui"
import "jquery-ui-dist/jquery-ui.js"
import "jquery-ui-bundle" //1.12.1 and 1.11.4
import "jqueryui";

All in an effort to get import "jquery-ui-touch-punch" to eventually import without throwing errors...所有的努力都是为了让import "jquery-ui-touch-punch"最终导入而不会抛出错误......

Examples above in the question work properly in the context of a create-react-app .问题中的上述示例在create-react-app的上下文中正常工作。 But in the context of a .Net application using it on the frontend it wasn't behaving.但是在前端使用它的 .Net 应用程序的上下文中,它没有表现。 I'm not sure why, but I did get things working like this:我不知道为什么,但我确实让事情像这样工作:

import $ from "jquery";
var jQuery = $;
(<any> window).$ = (<any>window).jQuery = $;
//use require so the assignments above happen, imports get hoisted
require("jquery-ui") 
require("jquery-ui-touch-punch");
require("bootstrap");

And for jQuery-ui-touch-punch I had to prepend var jQuery = require('jquery');对于jQuery-ui-touch-punch,我必须在前面加上var jQuery = require('jquery'); in the start of the file to make it play nicely with webpack.在文件的开头,使其与 webpack 很好地配合。 I forked it and am using a git based npm dependency to handle this.我分叉了它,并使用基于 git 的 npm 依赖来处理这个问题。

I have this working now this way:我现在以这种方式工作:

(window as any).$ = require('jquery');
(window as any).jQuery = $;

// use require so the imports aren't hoisted above the global variable assignments.
require('jquery-ui-dist/jquery-ui');
require('imports-loader?this=>window!jquery-ui-touch-punch');

using使用

"jquery": "3.1.1",
"jquery-ui-dist": "1.12.1",
"jquery-ui-touch-punch": "0.2.3",

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

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