在gulp任务中加载require.js模块

时间:2015-12-22 22:36:53

标签: requirejs gulp

我有一个require.js模块,它在我的app模块上运行一些基准测试。通常,这是通过benchmarksRunner.html手动打开的。

require.config({
    paths: {
        'someDependencies': ['someDependenciesPath'],
    },
});

require([
    'someModulePath'
], function (
    someModule
) {
    doSomeStuff
    return someResults (or dispatch event...)
});

有时我想使用gulp将这些结果保存在json文件中。我想出了如何将json保存到我想要存储结果的确切位置,但我一直坚持如何将基准测试结果检索到gulp中。

gulp.task('performance', function () {

    // Obviously this does not work...
    require('./src/libs/require/require.min.js');
    require('./tests/benchmarksMain.js');

    // I want to run the benchmarks as they are already 
    // configured in the benchmarksRunner.html using require.js

    // Somehow gulp should be able to start the require.js scripts
    // and wait for an arbitrary event, then run the code from bellow
    // (which works just fine)

    performanceResults = {'someResults': results};

    var fs = require('fs'),
    var json = JSON.parse(fs.readFileSync('./tests/performance/performance.json'));
    json[dateFormat()] = performanceResults;
    json = JSON.stringify(json);

    return stringSrc('performance.json', json)
       .pipe(gulp.dest('tests/performance'))
}); 

function stringSrc (filename, string) {
    var src = require('stream').Readable({ objectMode: true });
    src._read = function () {
        this.push(new gutil.File({ cwd: "", base: "", path: filename, contents: new Buffer(string) }));
        this.push(null)
    };
    return src
}

谢谢!

0 个答案:

没有答案