我正在使用 gruntjs、javascript、jade、bower 和 phonegap 来构建混合 Web 应用程序和 phonegap 移动应用程序。经过一番调查,我决定使用玉模板语言来创建我的 html 和 javascript 文件。这是我的设置。
//begin grunt module
module.exports = function(grunt) {
var $jadeConfigObject = {
compileDevelopment: {
options: {
pretty: true,
data: {
debug: true,
**phonegap : '<%= isPhonegapBuild %>'**
}
},
files: [{
expand: true,
cwd : "src",
src: "**/*.jade",
ext: ".html",
dest:"dist/development/www/"
}]
}
};
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
clean: [$dist_folder,"www"],
jade: $jadeConfigObject
// other gruntjs tasks
})
grunt.registerTask('setPhonegap', 'Set Phonegap related values.', function(n) {
**grunt.config.set('isPhonegapBuild', true);**
});
grunt.registerTask('buildPhonegap', ['setPhonegap','buildDevelopment'
,'copy:phonegap_www']);
}
现在解释
使用 gruntjs setPhonegap 任务来区分 phonegap 构建和普通 Web 应用程序构建。
任务 setPhonegap 设置 isPhonegapBuild 配置属性,该属性反过来被玉配置对象使用。然后我可以在翡翠文件中使用“if phonegap”语句。
我的翡翠布局文件中有以下行
include ../includes/mainJavascript.jade
mainJavascript.jade
if phonegap
script(type='text/javascript',src='#{mainFolder}/cordova.js')
script(type='text/javascript',src='#{mainFolder}/cordova_plugins.js')
script(type='text/javascript',src='#{mainFolder}/plugins/org.apache.cordova.device/www/device.js')
script(type='text/javascript',src='#{javascriptServicesFolder}/pushTokenService.js')
script(type='text/javascript',src='#{javascriptFolder}/plugins/com.phonegap.plugins.PushPlugin/www/PushNotification.js')
script(type='text/javascript',src='#{javascriptFolder}/phonegap/pushNotificationHelper.js')
这样我有以下功能
- 我的 Web 应用程序不包含任何与 phonegap 相关的脚本或 html
- 我可以使用翡翠布局(母版页)非常轻松地简化我的文件。
- 向每个页面添加新的 javascript 或更改应用程序中每个页面的每个 html 非常容易。
请注意,phonegap 需要 www 文件夹用于其构建脚本,因此我创建了以下目录结构。
src -- every code file here, jade, javascript
www -- only created for phonegap build
dist/development/www -- normal development www build
dist/production/www -- production www build
我在源代码管理中忽略了 www/* 和 dist/*。我只使用 src 玉和 javascript 文件。将此设置添加到 bower,我相信这是一个不错的选择。