Compiling dynamically required modules with Browserify

有些话、适合烂在心里 提交于 2019-11-27 22:10:48

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)

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.

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'));
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!