[英]How to minify and factor-bundle files in the same browserify command?
[英]Using proxyquire in a browserify factor bundle
卡在这一个。
我使用laravel elxir与tsify产生我的js。 我通过factor-bundle运行打字稿,将常见的js模块拆分为单独的文件。 我认为这种情况下不会有问题,因为所有内容都在spec.js中
规格
/// <reference path="../../../typings/index.d.ts" />
import "jasmine-jquery";
// @start widgets
import "./widgets/common/widget-factory/test";
factory-widget / index.ts
export class WidgetFactory {
.... this contains a require call to browser.service which i need to mock
}
工厂小工具/test.ts
...
import {WidgetFactory} from "./index";
const proxyRequire = require("proxyquire");
it("should output the factory items", ()=> {
proxyRequire('./widgets/browser.service/index',{
"@global": true,
});
}
浏览器服务
...
export class BrowserService implements IBrowserService{
//details
}
获取错误Uncaught TypeError: require.resolve is not a function
第262行上Uncaught TypeError: require.resolve is not a function
。
这是代码(是的,超过20,000行)您还应该如何调试这些东西。 ¯_(ツ)_ /¯
我用proxyquire看了Stubbing 。 我没有屏息地回答这个问题。
编辑:06-09-2016 Proxquire需要覆盖WidgetFactory类的启动方法中的require调用
在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 {
}
})
}
}
我在tsify
GitHub存储库中添加了一个proxyquireify
示例。 它基于proxyquireify
README.md
的简单示例 。
该显著的部分是重新定义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;
和的配置proxyquire
在插件build.js
:
browserify()
.plugin(tsify)
.plugin(proxyquire.plugin)
.require(require.resolve('./src/foo-spec.ts'), { entry: true })
.bundle()
.pipe(process.stdout);
如果构建bundle.js
并在Node.js下运行它,则应该看到写入控制台的消息包括存根的./bar
模块中的函数返回的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.