【问题标题】:Browserify + browserify-ngannotate + Tsify not workingBrowserify + browserify-ngannotate + Tsify 不工作
【发布时间】:2015-10-18 10:44:19
【问题描述】:

我正在使用带有 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;

【问题讨论】:

    标签: node.js gulp browserify ng-annotate


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

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

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

      【讨论】:

      • 如果答案对您有用,那么如果您接受它作为答案,那就太好了——这样其他用户就可以从知道答案有效的过程中受益。如果您有任何问题,请告诉我。
      猜你喜欢
      • 2015-04-20
      • 1970-01-01
      • 2016-02-22
      • 1970-01-01
      • 2017-03-02
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 2016-12-22
      相关资源
      最近更新 更多