首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Browserify + browserify-ngannotate + Tsify不工作

Browserify + browserify-ngannotate + Tsify不工作
EN

Stack Overflow用户
提问于 2015-10-18 10:44:19
回答 2查看 1.2K关注 0票数 2

我用的是一口又一口的巧克力。这件事一直运作得很好。然后我决定使用browserify-ngannotate添加ng注释。

我已经添加了ng注释的browserify转换,但是如果tsify是作为插件添加的,则不会调用ng注释转换。

如果我删除tsify插件,则调用ng-annote。我已经玩过,并切换了插件/转换注册。我是不是在这里遗漏了什么,还是应该在browserify/tsify上记录一个问题?

代码语言:javascript
复制
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;
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-05-24 13:02:59

您可以通过在ng注释选项中指定扩展来解决这个问题。

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

票数 2
EN

Stack Overflow用户

发布于 2015-11-06 12:44:29

当我将升华添加到包转换以生成小型化的构建时,我也意识到了这个问题。

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

所以,对我起作用的解决方案是安装uglifyify

代码语言:javascript
复制
npm install --save-dev uglifyify

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

代码语言:javascript
复制
b.transform({
            global: true,
            mangle: false,
            comments: true,
            compress: {
                angular: true
            }
        }, 'uglifyify');

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

因此,总之,我没有一个只使用ng-annotate的解决方案,但我的解决方案将在代码缩小之前添加必要的$inject语句,这在大多数情况下都很重要。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33197007

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档