Skip to content

Instantly share code, notes, and snippets.

@kristoferjoseph
Last active August 10, 2017 14:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kristoferjoseph/921160c70967e31dbcf45b4642e73b69 to your computer and use it in GitHub Desktop.
Save kristoferjoseph/921160c70967e31dbcf45b4642e73b69 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var path = require('path')
var parse = require('minimist')
var browserify = require('browserify')
var args = parse(process.argv.slice(2))
var entries = args._
var output = args.o || args.output
var main = args.m || args.main
var url = args.url || output
var options = {
main: main,
map: {},
output: output,
url: url
}
var temp
var pkgPath
var pkg
entries.forEach(f=> {
// We need to treat the entry file differently. You can't expose an alias for it.
if (f === main) {
console.info('require:', f)
console.info('output:', output+'/'+'entry.js')
console.info('script src:', url+'/'+'entry.js\n')
// Going to hard code entry.js because works.
options.map['entry.js'] = [f]
} else if (f.split('/').pop() === 'index.js') {
pkgPath = f.split('/')
pkgPath.pop()
pkgPath = pkgPath.join('/')
pkg = require(path.join('../', pkgPath, 'package.json'))
temp = pkg.name
console.info('require:', f)
console.info('output:', output+'/'+temp+'.js')
console.info('expose:', temp)
console.info('script src:', url+'/'+temp+'\n')
if (temp) {
options.map[temp+'.js'] = [{
require: f,
expose: temp
}]
}
} else {
temp = f.split('/').pop()
console.info('require:', f)
console.info('output:', output+'/'+temp)
console.info('expose:', temp.replace('.js', ''))
console.info('script src:', url+'/'+temp+'\n')
options.map[temp] = [{
require: f,
expose: temp.replace('.js', '')
}]
}
})
console.info('partitioning bundle with options:\n', options)
var b = browserify()
b.plugin('partition-bundle', options)
b.bundle()
@kristoferjoseph
Copy link
Author

kristoferjoseph commented Jul 31, 2017

In your package.json you can use them in the "scripts" section like so:

"local": "./scripts/build ./index.js ./screens/*.js -o=assets -m=./index.js"
"prod": "./scripts/build ./index.js ./screens/*.js -o=assets -m=./index.js --url='http://remote.url.like.s3'"

@kristoferjoseph
Copy link
Author

Added the ability to pass a folder of directories with package.json files included

Screens
|--Home
     |--index.js
     |--package.json
         |-- {  main: index.js, name: 'home' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment