[英]Using proxyquire in a browserify factor bundle
Stuck with this one. 卡在这一个。
I am using laravel elxir with tsify to generate my js. 我使用laravel elxir与tsify产生我的js。 I run the typescript through factor-bundle to split common js modules into a seperate files.
我通过factor-bundle运行打字稿,将常见的js模块拆分为单独的文件。 I don't think though that will be a problem in this case because everything is in a spec.js
我认为这种情况下不会有问题,因为所有内容都在spec.js中
spec.ts 规格
/// <reference path="../../../typings/index.d.ts" />
import "jasmine-jquery";
// @start widgets
import "./widgets/common/widget-factory/test";
factory-widget/index.ts factory-widget / index.ts
export class WidgetFactory {
.... this contains a require call to browser.service which i need to mock
}
factory-widget/test.ts 工厂小工具/test.ts
...
import {WidgetFactory} from "./index";
const proxyRequire = require("proxyquire");
it("should output the factory items", ()=> {
proxyRequire('./widgets/browser.service/index',{
"@global": true,
});
}
browser-service.ts 浏览器服务
...
export class BrowserService implements IBrowserService{
//details
}
Getting an error Uncaught TypeError: require.resolve is not a function
on line 262. 获取错误
Uncaught TypeError: require.resolve is not a function
第262行上Uncaught TypeError: require.resolve is not a function
。
Here is the code ( yeah it's over 20,000 lines ) how else are you supposed to debug this stuff . 这是代码(是的,超过20,000行)您还应该如何调试这些东西。 ¯_(ツ)_/¯
¯_(ツ)_ /¯
I've looked at Stubbing with proxyquire . 我用proxyquire看了Stubbing 。 I am not holding my breath getting an answer on this one.
我没有屏息地回答这个问题。
Edit: 06-09-2016 Proxquire is needed to overide the require call in the boot method of the WidgetFactory class 编辑:06-09-2016 Proxquire需要覆盖WidgetFactory类的启动方法中的require调用
In factory-widget/index.ts: 在factory-widget / index.ts中:
boot(output = true):any {
let required = {};
if (this._sorted.length) {
this._sorted.forEach((key)=> {
if (output) {
console.log(`${this._path}${key}/index`);
// this is where is need to overide the call to require.
required[key] = require(`${this._path}${key}/index`);
}
});
this._sorted.forEach((key)=> {
let dependencies = {},
module = this._factory[key];
if (module.hasOwnProperty(this.dependencyKey)) {
module[this.dependencyKey].map((key)=> {
dependencies[_.camelCase(key)] = this.isService(module) ? new required[key] : key;
});
}
if (this.isTag(module)) {
if (output) {
document.addEventListener("DOMContentLoaded", ()=> {
riot.mount(key, dependencies);
});
}
//console.log(key,dependencies);
}
else {
}
})
}
}
I've added a proxyquireify
example to the tsify
GitHub repo. 我在
tsify
GitHub存储库中添加了一个proxyquireify
示例。 It's based on the simple example in the proxyquireify
README.md
. 它基于
proxyquireify
README.md
的简单示例 。
The significant parts are the re-definition of require
to call proxyquire
in foo-spec.ts
: 该显著的部分是重新定义
require
调用proxyquire
在foo-spec.ts
:
const proxyquire = require('proxyquireify')(require);
require = function (name) {
const stubs = {
'./bar': {
kinder: function () { return 'schokolade'; },
wunder: function () { return 'wirklich wunderbar'; }
}
};
return proxyquire(name, stubs);
} as NodeRequire;
and the configuration of the proxyquire
plugin in build.js
: 和的配置
proxyquire
在插件build.js
:
browserify()
.plugin(tsify)
.plugin(proxyquire.plugin)
.require(require.resolve('./src/foo-spec.ts'), { entry: true })
.bundle()
.pipe(process.stdout);
If you build the bundle.js
and run it under Node.js, you should see that the message written to the console includes strings returned by the functions in the stubbed ./bar
module. 如果构建
bundle.js
并在Node.js下运行它,则应该看到写入控制台的消息包括存根的./bar
模块中的函数返回的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.