Compiling dynamically required modules with Browserify

后端 未结 3 1381
广开言路
广开言路 2020-12-06 01:00

I am using Browserify to compile a large Node.js application into a single file (using options --bare and --ignore-missing [to avoid troubles with

相关标签:
3条回答
  • 2020-12-06 01:29

    There's also the bulkify transform, as documented here:

    https://github.com/chrisdavies/tech-thoughts/blob/master/browserify-include-directory.md

    Basically, you can do this in your app.js or whatever:

    var bulk = require('bulk-require');
    
    // Require all of the scripts in the controllers directory
    bulk(__dirname, ['controllers/**/*.js']);
    

    And my gulpfile has something like this in it:

    gulp.task('js', function () {
      return gulp.src('./src/js/init.js')
        .pipe(browserify({
          transform: ['bulkify']
        }))
        .pipe(rename('app.js'))
        .pipe(uglify())
        .pipe(gulp.dest('./dest/js'));
    });
    
    0 讨论(0)
  • 2020-12-06 01:42

    Browserify does not support dynamic requires - see GH issue 377.

    The only method for dynamically requiring a directory I am aware of: a build step to list the directory files and write the "static" index.js file.

    0 讨论(0)
  • 2020-12-06 01:44

    This plugin allows to require Glob patterns: require-globify

    Then, with a little hack you can add all the files on compilation and not executing them:

    // Hack to compile Glob files. Don´t call this function!
    function ಠ_ಠ() {
      require('views/**/*.js', { glob: true })
    }
    

    And, for example, you could require and execute a specific file when you need it :D

    var homePage = require('views/'+currentView)
    
    0 讨论(0)
提交回复
热议问题