Browserify to minify and concat files

I have a purely web-based app (ie no Node server) and I want to be able to bundle all my JS files and CSS files for each page of the web app.

I was using Gulp to simply minify and concat my JS and CSS files into one bundle, but found that the order in which I supplied the files was then spewed out randomly (unacceptable if you ask me...). I tried gulp-order and other stream ordering modules with more random results.

I want to try browserify but I am stuck on two things

  • Do I need to do module.exports when using browserify? Think of my web app as extremely basic.. It requires jQuery, bootstrap, and my own libraries. There is nothing past the complexity of having to include the files in the right order
  • Why when I try to use browserify with a build file, as opposed to the command line, does it seem to try and bundle every single thing it requires? I only want to bundle the things that my app.js has a require() to
  • Eg.

    var browserify = require('browserify');
    var fs = require('fs');
    var b = browserify();
    b.add('./app.js');
    b.transform({
        global: true
    }, 'uglifyify');
    
    b.bundle()
        .pipe(fs.createWriteStream('./bundle.js'));
    

    When I run browserify build.js it spews out maybe 100 files into stdout. I am trying to replicate what happens when I run browserify -g uglifyify app.js > bundle.js -> this takes all requires() from app.js and bundles it into a new file.


  • If you want to be able to require your modules / code in other parts of your application, you'll be required to use module.exports . Otherwise you don't need to export anything.

  • Browserify only bundles the dependencies that are required, but it also does this recursively, meaning if your application requires library a and library a requires library b, then browserify will bundle your code, library a and library b.

  • 链接地址: http://www.djcxy.com/p/32914.html

    上一篇: 强制bundle()在找不到模块时抛出错误

    下一篇: Browserify缩小和连接文件