【发布时间】:2015-06-25 20:09:46
【问题描述】:
我设法使用一个名为 gulp-insert 的 gulp 插件完成了我的任务,如下所示:
gulp.task('compile-js', function () {
// Minify and bundle client scripts.
var scripts = gulp.src([
srcDir + '/routes/**/*.js',
srcDir + '/shared/js/**/*.js'
])
// Sort angular files so the module definition appears
// first in the bundle.
.pipe(gulpAngularFilesort())
// Add angular dependency injection annotations before
// minifying the bundle.
.pipe(gulpNgAnnotate())
// Begin building source maps for easy debugging of the
// bundled code.
.pipe(gulpSourcemaps.init())
.pipe(gulpConcat('bundle.js'))
// Buffer the bundle.js file and replace the appConfig
// placeholder string with a stringified config object.
.pipe(gulpInsert.transform(function (contents) {
return contents.replace("'{{{appConfigObj}}}'", JSON.stringify(config));
}))
.pipe(gulpUglify())
// Finish off sourcemap tracking and write the map to the
// bottom of the bundle file.
.pipe(gulpSourcemaps.write())
.pipe(gulp.dest(buildDir + '/shared/js'));
return scripts.pipe(gulpLivereload());
});
我正在做的是读取我们应用程序的配置文件,该文件由 npm 上的 config 模块管理。使用var config = require('config'); 从服务器端代码获取我们的配置文件很容易,但我们是一个单页应用程序,经常需要访问客户端的配置设置。为此,我将配置对象填充到 Angular 服务中。
这是 gulp 构建之前的 Angular 服务。
angular.module('app')
.factory('appConfig', function () {
return '{{{appConfigObj}}}';
});
占位符位于字符串中,因此对于其他一些首先处理文件的 gulp 插件来说,它是有效的 JavaScript。 gulpInsert 实用程序让我可以像这样插入配置。
.pipe(gulpInsert.transform(function (contents) {
return contents.replace("'{{{appConfigObj}}}'", JSON.stringify(config));
}))
这可行,但感觉有点 hacky。更不用说它必须缓冲整个捆绑文件,以便我可以执行操作。有没有更优雅的方式来完成同样的事情?最好是一种允许流保持平稳流动而不在最后缓冲整个捆绑包的方法?谢谢!
【问题讨论】:
-
对此我采取了一些不同的方法。我为每个环境创建了一个 json 文件。 dev.config.js、prod.config.js & 在 gulp dev/prod 构建期间,我确实用适当的 config.js 替换了我的 env.config.js,并且我的 index.html 包括 env.config.js。这感觉比在文件中搜索和替换要干净一些
-
抱歉,使用缓冲区有什么问题?流式传输也是一种缓冲,但体积更小。但是,我真的怀疑你的整个 bundle.js 是否超过了 1 Mb,这在当今几乎没有。也许,您对构建的整体执行时间不满意。在这种情况下,您应该考虑使用
watch功能。 -
这是一个大型企业应用Dimitry。它还不是很大,但肯定会很大。我们使用 gulp 是为了提高性能,这使得构建时间可能比使用 grunt 的相同设置要短得多。我正在注意尽早正确地做事,以便在未来的每一秒都减少。
-
@entre 我确实喜欢这种方法,但我选择了下面 tomtastico 的回答。请随时分享您的解决方案,因为它可以使其他人受益。
标签: javascript angularjs node.js stream gulp