如何在Parcel(bundler)中使用jQuery和jQuery-ui?

我通过npm安装了jquery(3.2.1)和jquery-ui-dist(1.12.1)。 (它们不包含在html中的脚本标记中)

在客户端脚本中我使用:

window.$ = require('jquery');// plain jQuery stuff works fine import 'jquery-ui-dist'; // breaks whole jQuery, with Error (missing module 8) 

今天我遇到了类似问题的angularjs应用程序和parcel-bundler。 看来parcel不能很好地处理外部模块中引入的全局变量(现在?)。 还有其他问题。

一种方法; 您可以使用普通需求而不是像这样的导入:

 var jquery = require("jquery"); window.$ = window.jQuery = jquery; // notice the definition of global variables here require("jquery-ui-dist/jquery-ui.js"); $(function() { $("#datepicker").datepicker(); }); 

如果您坚持使用导入,则应创建一个单独的文件,例如使用以下内容调用import-jquery.js

 import jquery from "jquery"; export default (window.$ = window.jQuery = jquery); 

并将其导入主文件中:

 import "./import-jquery"; import "jquery-ui-dist/jquery-ui.js"; $(function() { $("#datepicker").datepicker(); }); 

我希望我们能在不久的将来得到更好的支持。