Browserify + browserify-ngannotate + Tsify无法正常工作

时间:2015-10-18 10:44:19

标签: node.js gulp browserify ng-annotate

我正在使用浏览器和tsify。这一直很有效。然后我决定使用browserify-ngannotate添加ng-annotate。

我添加了ng-annotate browserify转换,但似乎如果将tsify添加为插件,则永远不会调用ng-annotate转换。

如果删除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;

2 个答案:

答案 0 :(得分:2)

我意识到我也遇到了这个问题,当我将uglifyify添加到捆绑转换中以生成缩小版本时。

我的解决方案的一个重要方面是,ng-annotate应该插入的缺失的显式$inject语句在代码实际缩小。幸运的是,在uglifyify中进行实际缩小的UglifyJS2得到了支持,可以在版本2。4。9中处理ng-annotate ngInject条评论(2014年1月) )。

所以,对我有用的解决方案是安装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语句,在大多数情况下是重要的。

答案 1 :(得分:2)

您可以通过在ng-annotate选项中指定扩展名来解决它。

bundler.transform(ngAnnotate, { ext: ['.ts', '.js'] });