簡體   English   中英

在browserify因子束中使用proxyquire

[英]Using proxyquire in a browserify factor bundle

卡在這一個。

我使用laravel elxirtsify產生我的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調用proxyquirefoo-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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM