2013-05-11 67 views
2

我试图编写一个自定义脚本/插件以包含在requireJS r.js优化器(用于指纹静态文件和一个用于捆绑JSON资源)。如何将插件添加到requirejs r.js优化器,该优化器仅在构建期间运行?

我没有想到的是如何在构建过程中触发我的自定义优化器。我可以写脚本,但是如何在优化期间触发bundle-build.js模块。

感谢的提示!

编辑:
我知道我可以使用“onBuildRead /写”,但这是不添加优化插件的地方。类似require-css的东西比较接近,但是是一个文件前缀,它会触发该插件,该插件指定在构建期间指定使用pluginBuilder。不是我正在寻找的东西。

回答

3

优化器应该获取指定为依赖关系的所有文件。如果您需要包括额外的事情,那么你可以添加使用onBuildRead或onBuildWrite回调,在这里你可以添加额外的逻辑/删除多余的东西:

({ 
    name: 'main', 
    baseUrl: '../', 
    // optimize: 'none', 
    optimize: 'uglify2', 
    exclude: ['jquery'], 
    mainConfigFile: '../main.js', 
    out: '../main.min.js', 
    // A function that if defined will be called for every file read in the 
    // build that is done to trace JS dependencies. 
    // Remove references to console.log(...) 
    onBuildRead: function (moduleName, path, contents) { 
     return contents; 
     // return contents.replace(/console.log(.*);/g, ''); 
    }, 
    onBuildWrite: function (moduleName, path, contents) { 
     // Add extra stufff; 
     return contents; 
    } 
}) 
+0

我一直在看这些,太,以及使用[前缀!/ pluginbuilder](http://requirejs.org/docs/plugins.html#apipluginbuilder)插件以及[内部API](https://github.com/jrburke/requirejs/wiki/Internal-API:- onResourceLoad)和[this](https://groups.google.com/forum/#!topic/requirejs/wOSAa25d6xY)页面。我仍在玩'onBuildRead/Write',但我希望找到一些hook/etc,它允许在处理过程中使用优化器,所以我可以尝试编写一个“优化器插件”,而不必添加所有的逻辑'onBuildRead/write'。仍然。谢谢! – frequent 2013-05-13 07:03:35

+0

仍然没有找到一个很好的钩子,所以我会检查现在,因为信息是正确的。感谢您的帮助! – frequent 2013-05-21 09:04:18