我用的是一口又一口的巧克力。这件事一直运作得很好。然后我决定使用browserify-ngannotate添加ng注释。
我已经添加了ng注释的browserify转换,但是如果tsify是作为插件添加的,则不会调用ng注释转换。
如果我删除tsify插件,则调用ng-annote。我已经玩过,并切换了插件/转换注册。我是不是在这里遗漏了什么,还是应该在browserify/tsify上记录一个问题?
var browserify = require('browserify');
var browserSyncConfig = require('../config').browserSync;
var browserSync = require('browser-sync').get(browserSyncConfig.instance);
var watchify = require('watchify');
var tsify = require('tsify');
var ngAnnotate = require('browserify-ngannotate');
var mergeStream = require('merge-stream');
var bundleLogger = require('../util/bundleLogger');
var gulp = require('gulp');
var handleErrors = require('../util/handleErrors');
var source = require('vinyl-source-stream');
var config = require('../config').browserify;
var _ = require('lodash');
var browserifyTask = function (devMode) {
var browserifyThis = function (bundleConfig) {
if (devMode) {
// Add watchify args and debug (sourcemaps) option
_.extend(bundleConfig, watchify.args, {debug: true});
// A watchify require/external bug that prevents proper recompiling,
// so (for now) we'll ignore these options during development. Running
// `gulp browserify` directly will properly require and externalize.
bundleConfig = _.omit(bundleConfig, ['external', 'require']);
}
var b = browserify(bundleConfig);
if (bundleConfig.tsify) {
b = b.plugin(tsify, {
noImplicitAny: false,
target: 'ES5',
noExternalResolve: false,
module: 'commonjs',
removeComments: false
});
}
if (bundleConfig.ngAnnotate) {
b = b.transform(ngAnnotate);
}
var bundle = function () {
// Log when bundling starts
bundleLogger.start(bundleConfig.outputName);
return b
.bundle()
// Report compile errors
.on('error', handleErrors)
// Use vinyl-source-stream to make the
// stream gulp compatible. Specify the
// desired output filename here.
.pipe(source(bundleConfig.outputName))
// Specify the output destination
.pipe(gulp.dest(bundleConfig.dest))
.pipe(browserSync.stream());
};
if (devMode) {
// Wrap with watchify and rebundle on changes
b = watchify(b, {
poll: true
});
// Rebundle on update
b.on('update', bundle);
bundleLogger.watch(bundleConfig.outputName);
} else {
// Sort out shared dependencies.
// b.require exposes modules externally
if (bundleConfig.require) b.require(bundleConfig.require);
// b.external excludes modules from the bundle, and expects
// they'll be available externally
if (bundleConfig.external) b.external(bundleConfig.external);
}
return bundle();
};
// Start bundling with Browserify for each bundleConfig specified
return mergeStream.apply(gulp, _.map(config.bundleConfigs, browserifyThis));
};
gulp.task('browserify', function () {
return browserifyTask()
});
// Exporting the task so we can call it directly in our watch task, with the 'devMode' option
module.exports = browserifyTask;发布于 2016-05-24 13:02:59
您可以通过在ng注释选项中指定扩展来解决这个问题。
bundler.transform(ngAnnotate, { ext: ['.ts', '.js'] });
发布于 2015-11-06 12:44:29
当我将升华添加到包转换以生成小型化的构建时,我也意识到了这个问题。
我的解决方案的一个重要方面是,缺少的、显式的$inject语句-- ng-注释应该插入的语句--在代码实际缩小之前并不重要。幸运的是,UglifyJS2在uglifyify中进行了实际的小型化,它在2.4.9版(2014年1月)中获得了处理ng-annotate的ngInject注释的支持。
所以,对我起作用的解决方案是安装uglifyify
npm install --save-dev uglifyify并将以下uglifyify转换添加到Browserify包中:
b.transform({
global: true,
mangle: false,
comments: true,
compress: {
angular: true
}
}, 'uglifyify');这将使UglifyJS2在缩小代码之前将适当的$inject语句插入到代码中。
因此,总之,我没有一个只使用ng-annotate的解决方案,但我的解决方案将在代码缩小之前添加必要的$inject语句,这在大多数情况下都很重要。
https://stackoverflow.com/questions/33197007
复制相似问题