我使用Webpack来打包我的依赖项,我正在尝试加载方法--组合器。依赖性是这样的..。
// Generated by CoffeeScript 1.3.1
(function() {
var __slice = [].slice;
this.before = function(decoration) {
return function(base) {
return function() {
decoration.apply(this, arguments);
return base.apply(this, arguments);
};
};
};
...
}).call(this);然后我在我的TS文件中包括了.
require("method-combinators/lib/method-combinators.js");当我在内部函数上断点时,我会注意到this != window。如果我想加入..。
require.bind(window);我得到了
关键依赖项: 20:0-7要求函数的使用方式不能静态地提取依赖项。
怎样才是正确的方法?
更新
> this
Object {}
> window
Window {...}如果我做了
window['comb'] = require("method-combinators/lib/method-combinators.js");它看起来很有效,但是因为这是遗留代码,所以很难搜索after和search的所有实例。
更新2
这有点管用..。
var comb = require("method-combinators/lib/method-combinators.js");
window['after'] = comb.after;
window['before'] = comb.before;这与@tcooc提供的答案类似,但问题是,我必须列出每个函数。为了强调我有这样的东西..。
var s = require("underscore.string");
window['_'].mixin(s.exports());但这里的问题是梳没有exports
更新3
这是我的最后一次
var comb = require("method-combinators/lib/method-combinators.js");
window['_'].extend(window, comb);发布于 2016-09-23 19:41:32
根据设计,公共‘t模块不访问或修改全局范围(至少不应该)。代码中的this变量引用的是从该模块导出的值,而不是window。代码实际上被包装在类似的东西中(为用例简化):
// you want to load "method-combinator.js"
require('method-combinator.js');
// load "method-combinator.js" as "dependency()"
var exports = {};
dependency.call(exports);现在,每当您尝试加载“方法-组合器. is”时,都会返回exports。
使用依赖项的正确方法是:
var combinators = require('method-combinator.js');如果您希望将combinators的所有值添加到window中,假设您有下划线:
_.extend(window, combinators);
// or if _ is somehow not resolving properly (for some reason?)
window._.extend(window, combinators);https://stackoverflow.com/questions/39667964
复制相似问题