2

我正在使用带有 browserify 和 tsify 的 gulp。这一直运作良好。然后我决定使用 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;
4

2 回答 2

2

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

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

于 2016-05-24T13:02:59.443 回答
2

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

我的解决方案的一个重要方面是ng-annotate$inject应该插入的缺失的显式语句在代码实际被缩小之前并不重要。幸运的是,在 2.4.9 版本(2014 年 1 月)中进行实际缩小的UglifyJS2获得了对处理评论的支持。uglifyifyng-annotatengInject

因此,对我有用的解决方案是安装uglifyify

npm install --save-dev uglifyify

并将以下uglifyify转换添加到 Browserify 包:

b.transform({
            global: true,
            mangle: false,
            comments: true,
            compress: {
                angular: true
            }
        }, 'uglifyify');

这将在代码缩小之前UglifyJS2将适当的语句插入到您的代码中。$inject

所以,总而言之,我没有只使用 using 的解决方案,但我的解决方案会在代码缩小ng-annotate之前添加必要的语句,这在大多数情况下很重要。$inject

于 2015-11-06T12:44:29.490 回答