【问题标题】:Can browserify require a vinyl file generated in gulp?browserify 可以需要在 gulp 中生成的乙烯基文件吗?
【发布时间】:2015-03-12 15:24:00
【问题描述】:

我刚刚开始使用gulpbrowserify,我需要一些帮助来解决这个问题:

我正在使用一个名为ng-autobootstrap 的库来生成一个browserify 兼容文件,稍后在主脚本中需要该文件。这是我的autobootstrap 任务:

gulp.task "autobootstrap", ->
  gulp.src("source/**/*.{coffee,js}",
    read: false
    base: "source"
  )
  .pipe(ngAutoBootstrap(
    moduleTypes:
      animation:
        path: "**/animations/*.{coffee,js}",
      constant:
        path: "**/constants/*.{coffee,js}",
      controller:
        path: "**/controllers/*.{coffee,js}",
      directive:
        path: "**/directives/*.{coffee,js}",
      factory:
        path: "**/factories/*.{coffee,js}",
      filter:
        path: "**/filters/*.{coffee,js}",
      provider:
        path: "**/providers/*.{coffee,js}",
      service:
        path: "**/services/*.{coffee,js}",
      value:
        path: "**/values/*.{coffee,js}",
      # config modules are pulled in like this:
      # app.config(require("./path/to-config"))
      config:
        path: "**/*-config.{coffee,js}"
  ))

如果我添加.pipe(gulp.dest("./source/")),它将在source 目录中创建一个bootstrap.js 文件,但这不是我想要的,我宁愿保持该目录干净。据我了解,到目前为止,我的内存中有一个黑胶文件,内容如下:

'use strict';

module.exports = function(app) {
    // Controllers
    app.controller('AppController', require('./controllers/app-controller'));
    app.controller('UsersController', require('./controllers/users-controller'));
    // ... and so on
};

假设source/js/main.js 文件如下所示:

app = angular.module("app");
require("./bootstrap")(app); // this is the file generated by ng-autobootstrap

还有一个简单的browserify 任务,它创建了build/bundle.js 文件:

browserify = require('browserify')
gulp = require('gulp')
source = require('vinyl-source-stream')

gulp.task 'browserify', ->
  browserify('./source/main.js')
    .bundle()
    .pipe(source('bundle.js'))
    .pipe(gulp.dest('./build/'))

现在,如果我修改autobootstrap 以先将文件写入光盘,然后运行browserify,这一切都很好,./bootstrap 文件将在那里。但是有没有办法避免写入光盘?类似于将乙烯基文件添加到browserify 的搜索树?

【问题讨论】:

    标签: javascript angularjs gulp browserify commonjs


    【解决方案1】:

    使用b.require 使文件可从捆绑包外部使用。为了使用 Gulp 使用/发射的乙烯基流,与 Browserify;我们使用through2.obj 进行了一个小的内联转换,只发出内容。

    关于使用b.require 的注意事项是您需要b.exclude 以便module-deps 不会尝试在node_modules 目录等中解决它。请参阅问题#908 和@987654325 @了解更多信息。

    var gulp = require('gulp');
    var through = require('through2');
    var source = require('vinyl-source-stream');
    var browserify = require('browserify');
    
    gulp.task('browserify-bundle', function() {
        return browserify('./test/main.js')
            // We need to exclude the required module so `module-deps` doesn't try to resolve it in the `node_modules` directories, etc
            // Suggested here: https://github.com/substack/node-browserify/issues/908#issuecomment-74909062
            //      See also: https://github.com/substack/node-browserify/issues/1106
            .exclude('some-func')
            // Then add in the module to the bundle
            .require(
                gulp.src('./test/hidden-some-func.js')
                    // Any plugins here....
                    // .pipe(ngAutoBootstrap(..)
                    //
                    // Then convert it to a object stream with only the `contents` for browserify to consume
                    .pipe(through.obj(function(chunk, env, cb) {
                        if (chunk.isStream()) {
                            self.emit('error', new gutil.PluginError(PLUGIN_NAME, 'Cannot operate on stream'));
                        }
                        else if (chunk.isBuffer()) {
                            this.push(chunk.contents);
                        }
    
                        // "call callback when the transform operation is complete."
                        cb();
                    })),
                {
                    // Dependency name to inject
                    expose: 'some-func',
                    // So that relative requires will be resolvable
                    basedir: './test/'
                }
            )
            .bundle()
            .pipe(source('bundle.js'))
            .pipe(gulp.dest('dest'));
    });
    

    【讨论】:

    • 似乎是我一直在寻找的答案,但我放弃了使用 browserify,因此我无法对其进行测试。但我将此标记为正确答案:-) 谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-26
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多