blob: e99a0be9ae909e2f41026594f02dfbafe49208f6 [file] [log] [blame]
#!/usr/bin/env node
// Build a single module using a generic Rollup-based build script.
// Reads in a src/index.js, writes to a lib/index.js. Might write
// index-browser.js if it detects that it needs to support a "browser" version.
//
// You can use this on the CLI by doing:
// build-module.js path/to/module
'use strict';
var rollupPrepublish = require('rollup-prepublish');
var path = require('path');
var lie = require('lie');
if (typeof Promise === 'undefined') {
global.Promise = lie; // required for denodeify in node 0.10
}
var Promise = lie;
var denodeify = require('denodeify');
var mkdirp = denodeify(require('mkdirp'));
var rimraf = denodeify(require('rimraf'));
function buildModule(filepath) {
var pkg = require(path.resolve(filepath, 'package.json'));
if (pkg.browser && pkg.browser['./lib/index.js'] !== './lib/index-browser.js') {
return Promise.reject(new Error(pkg.name +
' is missing a "lib/index.js" entry in the browser field'));
}
// browser & node vs one single vanilla version
var versions = pkg.browser ? [false, true] : [false];
// special case for "pouchdb-browser" - there is only one index.js,
// and it's built in "browser mode"
var forceBrowser = pkg.name === 'pouchdb-browser';
return Promise.resolve().then(function () {
return rimraf(path.resolve(filepath, 'lib'));
}).then(function () {
return mkdirp(path.resolve(filepath, 'lib'));
}).then(function () {
return Promise.all(versions.map(function (isBrowser) {
var dest = isBrowser ? 'lib/index-browser.js' : 'lib/index.js';
return rollupPrepublish({
entry: path.resolve(filepath, './src/index.js'),
dest: path.resolve(filepath, dest),
browser: isBrowser || forceBrowser,
quiet: true
}).then(function () {
console.log(' \u2713' + ' wrote ' +
path.basename(filepath) + '/' + dest + ' in ' +
(isBrowser ? 'browser' :
versions.length > 1 ? 'node' : 'vanilla') +
' mode');
});
}));
});
}
if (require.main === module) {
buildModule(process.argv[process.argv.length - 1]).catch(function (err) {
console.error('build-module.js error');
console.error(err.stack);
process.exit(1);
});
} else {
module.exports = buildModule;
}