我喜欢这样做的方式是为index.html 维护两个单独的版本。
index-development.html 用于开发环境,index-production.html 用于生产环境。
index-development.html 包括所有脚本和 css(非缩小和连接)和 index-production.html 作为缩小和连接脚本和 css 链接。
我从 gulp 脚本构造index.html。
默认情况下会部署index-development.html。
如果我为 gulp 脚本指定参数p,它将部署index-production.html
无需更新要在您的express router 中提供的文件的文件路径。
先做
npm install yargs
在 gulp 中,我包括
var argv = require('yargs').argv;
检查参数p (gulp -p) 是否通过
传递给 gulp(用于生产的 p)
var isProduction = argv.p;
然后,
if(isProduction){
taskSequence = ['combineControllers','combineServices','productionsIndex','startServer'];
} else{
taskSequence = ['developmentIndex','startServer'];
}
gulp.task('default', taskSequence);
gulp.task('startServer', function(){
exec('npm start', function (err, stdout, stderr) {
console.log(stdout);
console.log(stderr);
cb(err);
});
});
gulp.task('productionsIndex', function(done) {
return gulp.src('./www/index-productions.html')
.pipe(concat('index.html'))
.pipe(gulp.dest('./public/'));
});
gulp.task('developmentIndex', function(done) {
return gulp.src('./www/index-development.html')
.pipe(concat('index.html'))
.pipe(gulp.dest('./public/'));
});
这样,您的 index.html 文件将动态构建,而无需更改 express 中的代码,您可以像这样提供它
res.render('index');
如果您想在任何地方使用myPage.html,只需将上面代码中的index.html 和index 替换为myPage.html 和myPage。
编辑:
要在开发环境中启动您的应用程序,只需运行gulp
要在生产环境中启动您的应用程序,只需运行gulp -p
简单!