按照Addy Osmani(O'Reilly)关于Grunt-BBB (Backbone Boilerplate Buddy)的“开发Backbone.js Apllication ”的章节之一,我无法创建构建配置文件。
以下是用于此目的的文件系统树:
/builds
/closure
/rhino
/config
/build.js
build.sh
/development
/* Grunt-BBB files after init */
/app
/styles
index.css
app.js
config.js
main.js
router.js
/test
/* Not important files used for testing */
/vendor
/h5bp
/css
main.css
normalize.css
/jam
/backbone
backbone.js
package.json
/bakbone.layoutmanager
bakbone.layoutmanager.js
package.json
/jquery
jquery.js
package.json
/lodash
lodash.js
lodash.min.js
lodash.underscore.min.js
package.json
require.config.js
require.js
/js
/libs
almond.js
require.js
/distribution
/* Empty dist directory where the optimized / minified / concatenated files should go */
以下是我在/development
目录中执行的步骤:
1)安装Grunt-BBB(npm install -g bbb
)
2)下载r.js,这是Require.js项目的一部分(git clone https://github.com/backbone-boilerplate/grunt-bbb
)
3)初始化样板文件(bbb init
)
这是我用来为Google Closure编译器配置r.js AMD加载器的build.js文件:
({
appDir: '../../development',
baseUrl: 'app',
dir: '../../distribution',
optimize: 'closure', // 'uglify2'
paths: {
backbone: '../vendor/jam/backbone/backbone',
'backbone.layoutmanager': '../vendor/jam/backbone.layoutmanager/backbone.layoutmanager',
jquery: '../vendor/jam/jquery/jquery',
lodash: '../vendor/jam/lodash/backbone.min'
},
modules: [
{
name: 'main'
}
],
onBuildRead: function(moduleNames, path, contents) {
return contents;
//return contents.replace(/console\.log\(([^\)]+)\);/g, '')
// .replace(/debugger;/, '');
}
})
这是我使用的build.sh文件:
#!/usr/bin/env bash
# r.js directory
RJSDIR="r.js"
RJS="$RJSDIR/dist/r.js"
# Rhino directory
RHINODIR="rhino"
RHINO="$RHINODIR/js.jar"
# Google Closure Compiler directory
CLOSUREDIR="closure"
CLOSURE="$CLOSUREDIR/compiler.jar"
# Build config directory
CONFIGDIR="config"
CONFIG="$CONFIGDIR/build.js"
# Launch compillation
java -Xms256m -Xmx256m -classpath "$RHINO":"$CLOSURE" org.mozilla.javascript.tools.shell.Main "$RJS" -o "$CONFIG" $@
我的目标是优化,缩小,连接所有JavaScrit文件,包括库和模板(我还没有,我只使用样板文件),还有CSS文件。
运行./build.sh
得到的结果是每个文件都被正确地最小化(除了CSS规则内联,但这不是重点)和连接但是由Jam加载和管理的资源(包管理器,结合NPM和Require.js)没有连接。
由于它们已经由Jam加载/管理,因此它们不会在AMD样式的JavaScript文件中重新声明。
总之,我的问题如下:
如何重写我的build.js
配置文件,以便Jam加载的资源也包含在release / dist文件中并连接在一起?
如何才能使连锁资源不会复制到realse / dist目录中?是否可以在build.js
文件中对此进行配置,还是应该在我的build.sh
文件中进行配置?
修改:新的build.js文件:
({
appDir: '../../development',
baseUrl: 'app',
dir: '../../distribution',
optimize: 'closure', // 'uglify2'
paths: {
requirejs : '../vendor/jam/require',
backbone: '../vendor/jam/backbone/backbone',
'backbone.layoutmanager': '../vendor/jam/backbone.layoutmanager/backbone.layoutmanager',
jquery: '../vendor/jam/jquery/jquery',
lodash: '../vendor/jam/lodash/backbone.min'
},
name: 'main',
include: ['requirejs'],
onBuildRead: function(moduleNames, path, contents) {
return contents;
//return contents.replace(/console\.log\(([^\)]+)\);/g, '')
// .replace(/debugger;/, '');
}
})
这是错误:
file:///vendor/js/libs/require.jsFailed to load resource: The requested URL was not found on this server.
file:///app/styles/index.cssFailed to load resource: The requested URL was not found on this server.
答案 0 :(得分:0)
除非您指示它执行此操作,否则r.js永远不会包含require.js。看到这个链接:
http://requirejs.org/docs/optimization.html#onejs
链接指的是命令行选项,但构建文件选项大致相同:
您需要在路径中为require.js定义一个虚拟模块:
paths: {
requireLib : '/path/to/require.js'
backbone: '../vendor/jam/backbone/backbone',
'backbone.layoutmanager': '../vendor/jam/backbone.layoutmanager/backbone.layoutmanager',
jquery: '../vendor/jam/jquery/jquery',
lodash: '../vendor/jam/lodash/backbone.min'
},
并包含它:
name "main",
include: ["requireLib"],
您可以通过设置:
确保解决所有依赖关系findNestedDependencies: true,
您可以使用' out'来配置输出路径。在您的构建文件中
out: "path/to/my/builtfile-1.0.0.js",
抱歉,我对果酱的了解不足以说明果酱是否会覆盖此设置
_Pez