【问题标题】:Should I `browserify` all files then `concat` them for my gulp pipeline? Or the inverse?我应该“浏览”所有文件,然后为我的 gulp 管道“连接”它们吗?还是相反?
【发布时间】:2015-11-05 20:29:57
【问题描述】:

我开始在 index.js 中分解一个 Javascript 客户端库,并在顶部有一个额外的文件,我现在正在为它做一个 require

...
require("./other_file")
...

那么我的gulpfile.js 看起来像这样:

function compile(watch) {
  var bundler = watchify(browserify({
    entries: ['./src/index.js'],
    debug: true,
    sourceType: module,
  })
  .transform(babelify));

function rebundle() {
    bundler.bundle()
      .on('error', function(err) { console.error(err); this.emit('end'); })
      .pipe(source('build.js'))
      .pipe(buffer())
      .pipe(sourcemaps.init({ loadMaps: true }))
      .pipe(sourcemaps.write('./'))
      .pipe(gulp.dest('./dist'));
  }

  if (watch) {
    bundler.on('update', function() {
      console.log('-> bundling...');
      rebundle();
    });
  }

  rebundle();
}

我不确定我是否应该 concat 我将需要的所有文件,然后浏览较大的 concatted 文件或只是 browserify 主文件和 require 将只是工作?

(我关注了the gulpfile example here

【问题讨论】:

  • 无需连接。 Browserify 将跟踪您需要的所有模块并构建一个单独的包。
  • 太棒了,谢谢。请随意回答,我会将其标记为正确。

标签: javascript gulp browserify babeljs


【解决方案1】:

没有必要连接。 Browserify 将跟踪您需要的所有模块并构建一个单独的包。

browserify 将递归地分析应用程序中的所有 require() 调用,以便构建一个可以在单个标记中提供给浏览器的捆绑包。

source

【讨论】:

  • 这在 99% 的情况下都是正确的。然而,我遇到过一些情况,其中一个库,尤其是那些涉及 Emscripten 的库,包含一些检测环境的魔法,然后做一些让 Browserify 感到困惑的事情。但这是一个极端的情况,我最终只使用 gulp-concat。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 1970-01-01
相关资源
最近更新 更多