Last active
March 28, 2016 18:52
-
-
Save shamasis/fda8fdc4d8616683eadb to your computer and use it in GitHub Desktop.
Converts package.json dependencies to bundled dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/** | |
* Read a package.json file and then replace all dependencies and | |
* optionally dev dependencies into bundled dependencies. | |
*/ | |
var p = '', | |
writeFile = require('fs').writeFile, | |
resolve = require('path').resolve; | |
// get the package.json file reference | |
(process.argv[2]) && (p = process.argv[2]); | |
p = p.replace(/\/?package\.json$/, ''); | |
p || (p = '.'); | |
p += '/package.json'; | |
p = resolve(p); | |
var data = require(p); | |
// do a backup | |
writeFile(p + '.prebundle', JSON.stringify(data, null, 2), 'utf8', function (err) { | |
if (err) throw err; | |
}); | |
for (var dep in data.devDependencies) { | |
if (data.dependencies[dep]) { | |
throw "Overlapping dependencies and dev dependency: " + dep; | |
} | |
data.dependencies[dep] = data.devDependencies[dep]; | |
} | |
delete data.devDependencies; | |
delete data.bundleDependencies; | |
delete data.bundledDependencies; | |
data.bundledDependencies = Object.keys(data.dependencies || {}); | |
writeFile(p, JSON.stringify(data, null, 2), 'utf8', function (err) { | |
if (err) throw err; | |
console.log(' - set bundle of ' + data.bundledDependencies.length + ' dependencies.'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment