如何解决如何从 jQuery 插件中隐藏“需要”功能
我正在使用 twism jQuery plugin,它在内部使用了“require”函数。这与 webpack 冲突,后者定义了自己的“require”,导致以下错误:
Uncaught TypeError: __webpack_require__(...).toUrl is not a function
有问题的代码是这一行:
b = require.toUrl("twism").replace("jquery.twism","maps/" + u);
如何在不修改的情况下将全局 webpack 功能与此代码隔离?
解决方法
解决方案是在该行之前添加特定于 webpack 的处理程序:
if (typeof(__webpack_require__) === 'function') {
// hack for webpack - ask it to copy the map being used to the build location
const buildUrl = require('./maps/' + file);
url = buildUrl.default;
// the following line is for debug and demonstration purpose
console.log('Moved file to url: ',file,url);
}
并用 else if 子句替换原来的 if :
原文:
//hack for RequireJS/Bower
if (typeof(require) !== 'undefined' && settings.map != "custom") {
b = require.toUrl("twism").replace("jquery.twism","maps/" + u);
替换为:
else if (typeof(require) !== 'undefined' && settings.map != "custom") {
//hack for RequireJS/Bower
b = require.toUrl("twism").replace("jquery.twism","maps/" + u);
我将此作为拉取请求发布到了 twism 作者的 Github。
,你能完全限定 require 调用吗? 例如
b = twism.require.toUrl("twism").replace("jquery.twism","maps/" + u);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。