Gulp + Browserify捆绑依赖

时间:2015-12-22 04:30:17

标签: gulp bundle browserify

<{1>} libs/index.jsx 使用require调用所有库 恩。 require('jquery')...

它会生成一个包含jquery的包

<{1>} app/index.jsx 我在app.js上调用了同样的东西,但整个jquery代码仍在添加 到app.js上的包

任何人都可以帮助我如何将app.js依赖于libs.js输出文件?这是我的代码:

    'use strict';

    var gulp = require('gulp');
    var notify = require('gulp-notify');
    var rename = require('gulp-rename');
    var browserify = require('browserify');
    var babelify = require('babelify');
    var watchify = require('watchify');
    var source = require('vinyl-source-stream');
    var es = require('event-stream');

    gulp.task('default', function() {

        // we define our input files, which we want to have bundled:
        var files = [
          'build/js/app/index.jsx',
          'build/js/libs/index.jsx'
        ];
        var outputFiles = [
          'app.js',
          'libs.js'
        ];

        // map them to our stream function
        var tasks = files.map(function(entry, index) {

          // browserify instance
          var bundler = browserify({
            entries: [entry]
          });

          // Watchify to watch source file changes
          bundler.plugin(watchify, {
            // ignore this folders
            ignoreWatch: [
              '**/node_modules/**',
              '**/bower_components/**'
            ]
          });

          // Babel tranforms, transform .jsx files to native js
          bundler.transform(babelify, {
            presets: ['es2015', 'react']
          });

          // bundle the files
          bundler.bundle()
                 .pipe(source(outputFiles[index]))
                 // rename them to have "bundle as postfix"
                 .pipe(rename({
                     extname: '.bundle.js'
                 }))
                 .pipe(gulp.dest('dist/js'))
                 .pipe(notify({
                   message: 'Generated file: ',
                 }));

          return bundler;
        });
    });

在dist / js文件夹中创建的输出文件是libs.bundle.jsapp.bundle.js。我想要两个独立的文件,其中一个依赖于另一个,即app.bundle.js。有人可以帮帮我吗?

1 个答案:

答案 0 :(得分:1)

使用factor-bundle。看看this answer