Javascript 如何隐藏&x27;要求';来自jQuery插件的函数
我正在使用,它在内部使用'require'函数。这与webpack冲突,webpack定义了自己的“require”,导致以下错误:Javascript 如何隐藏&x27;要求';来自jQuery插件的函数,javascript,symfony,webpack,jquery-plugins,Javascript,Symfony,Webpack,Jquery Plugins,我正在使用,它在内部使用'require'函数。这与webpack冲突,webpack定义了自己的“require”,导致以下错误: Uncaught TypeError: __webpack_require__(...).toUrl is not a function 有问题的代码如下所示: b = require.toUrl("twism").replace("jquery.twism", "maps/" + u); 如何在不
Uncaught TypeError: __webpack_require__(...).toUrl is not a function
有问题的代码如下所示:
b = require.toUrl("twism").replace("jquery.twism", "maps/" + u);
如何在不修改代码的情况下将全局webpack函数与此代码隔离?您能完全限定require调用吗? e、 g
解决方案是在该行之前添加特定于网页包的处理程序:
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);
}
并将原始if替换为else 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。这不起作用。Twism实际上试图调用require,它属于另一个包,但与webpack不兼容。请参阅我的答案,了解我如何解决它的详细信息。
else if (typeof(require) !== 'undefined' && settings.map != "custom") {
//hack for RequireJS/Bower
b = require.toUrl("twism").replace("jquery.twism", "maps/" + u);