browserify / factor-bundle

factor browser-pack bundles into common shared bundles
Other
400 stars 27 forks source link

Bundle external dependencies #16

Open nickdima opened 10 years ago

nickdima commented 10 years ago

In my project I'm using both npm modules and bower components (with debowerify) which I want to bundle together as vendor.js while keeping my own code in an app.js bundle.

What I did so far is created the app.js bundle by setting external to the npm modules and bower components. To figure out what modules to set as external I read the dependencies from package.json and bower.json.

Now, in a similar fashion I would like to parse my codebase, see which requires are external modules and bundle them together. How could I do this?

mykone commented 8 years ago

Hello, anybody have a solution to the question above? I know it's very old, but I am now starting to use factor-bundle, and I am facing the same situation.

casr commented 7 years ago

Factor Bundle has the ability to run a custom filter via the threshold option. So you could do:

#!/usr/bin/env node
var browserify = require('browserify')
var fs = require('fs')
var path = require('path')

var entries = [
  path.join(__dirname, 'index.js')
]

var outputFiles = [
  path.join(__dirname, 'dist-main.js')
]

var b = browserify(entries)

b.plugin('factor-bundle', {
  output: outputFiles,
  threshold: function (row, groups) {
    // Get a relative directory to where we are building
    var rPath = path.relative(__dirname, row.file)

    // Assuming our node_modules folder is in the same directory that we are
    // building in then just declare vendor files as those presiding in it
    return !!rPath.match(/^node_modules\//)
  }
})

b.bundle().pipe(fs.createWriteStream(path.join(__dirname, 'dist-vendor.js')))

I've made a simple repository showing it all together.