browserify-global-pack
Advanced tools
Comparing version 1.0.1 to 1.1.0
114
index.js
@@ -6,4 +6,4 @@ 'use strict'; | ||
fs = require('fs'), | ||
highland = require('highland'), | ||
writeFile = highland.wrapCallback(fs.writeFile); | ||
_ = require('highland'), | ||
writeFile = _.wrapCallback(fs.writeFile); | ||
@@ -19,11 +19,10 @@ /** | ||
* @param {string} [opts.verbose] log file writes | ||
* @param {function} [opts.getOutfile] Customize filenames of modules | ||
* @returns {Stream} | ||
*/ | ||
function packAndWrite(writeToDir, {scope, verbose} = {}) { | ||
const deps = []; | ||
return highland.pipeline( | ||
highland.doto(({file, id}) => deps.push({file, id})), | ||
globalPack({scope}), | ||
getFiles(deps, writeToDir), | ||
writeFiles(verbose) | ||
function packAndWrite(writeToDir, {scope, verbose, getOutfile} = {}) { | ||
return _.pipeline( | ||
globalPack({scope, objectMode: true}), | ||
assignOutfiles(writeToDir, getOutfile), | ||
writeDeps(verbose) | ||
); | ||
@@ -33,71 +32,62 @@ }; | ||
/** | ||
* Returns a stream that determines where each module in a global-pack | ||
* bundle should export, taking in global-pack strings and outputting | ||
* {id: string, outpath: string, content: string} objects. | ||
* @param {string[]} deps Array of module IDs | ||
* @param {string[]} writeToDir | ||
* @return {Stream} | ||
* Returns a function that returns the absolute filepath | ||
* to which a dep's content should be written. | ||
* @param {string} writeToDir | ||
* @return {string} | ||
*/ | ||
function getFiles(deps, writeToDir) { | ||
let i = 0; | ||
function outpathGenerator(writeToDir) { | ||
return (dep) => { | ||
const id = dep.expose || dep.id; | ||
return highland.map((str) => { | ||
const content = str + '\n', | ||
dep = deps[i - 1]; | ||
let id, sourceFile; | ||
if (i === 0) { | ||
id = 'prelude'; | ||
sourceFile = '(prelude)'; | ||
} else if (dep) { | ||
id = dep.id; | ||
sourceFile = dep.file; | ||
} else { | ||
id = 'postlude'; | ||
sourceFile = '(postlude)'; | ||
if (pathUtil.isAbsolute(id)) { | ||
return pathUtil.join(writeToDir, pathUtil.parse(id).name + '.js'); | ||
} | ||
i++; | ||
return { | ||
id, | ||
sourceFile, | ||
outpath: getOutpath(id, writeToDir), | ||
content | ||
}; | ||
}); | ||
return pathUtil.join(writeToDir, id + '.js'); | ||
}; | ||
} | ||
/** | ||
* For a given module ID and write destination, return the absolute filepath | ||
* to which the module should be written. | ||
* @param {string} id | ||
* Returns a stream that adds an "outfile" property to each dep | ||
* @param {string} writeToDir | ||
* @return {string} | ||
* @param {function} getOutfile | ||
* @return {Stream} | ||
*/ | ||
function getOutpath(id, writeToDir) { | ||
if (pathUtil.isAbsolute(id)) { | ||
return pathUtil.join(writeToDir, pathUtil.parse(id).name + '.js'); | ||
} | ||
return pathUtil.join(writeToDir, id + '.js'); | ||
function assignOutfiles(writeToDir, getOutfile = outpathGenerator(writeToDir)) { | ||
return _.map(dep => Object.assign({}, dep, {outfile: getOutfile(dep)})); | ||
} | ||
/** | ||
* Returns a stream that takes in {outpath: string, content: string} objects | ||
* and writes files. | ||
* @param {boolean} verbose | ||
* Write deps, combining deps with the same outfile | ||
* @param {boolean} verbose Log writes | ||
* @return {Stream} | ||
*/ | ||
function writeFiles(verbose) { | ||
return highland.flatMap(file => writeFile(file.outpath, file.content) | ||
.doto(() => verbose && console.log(`${file.sourceFile} -> ${file.outpath}`)) | ||
); | ||
function writeDeps(verbose) { | ||
return (deps) => _(deps) | ||
.group('outfile') | ||
.flatMap(_.pairs) | ||
.flatMap(writeDepGroup(verbose)); | ||
} | ||
/** | ||
* Write groups of deps | ||
* @param {boolean} verbose Log writes | ||
* @return {Stream} | ||
*/ | ||
function writeDepGroup(verbose) { | ||
return ([outfile, deps]) => { | ||
return _(deps) | ||
.reduce('', (prev, dep) => prev += dep.content + '\n') | ||
.flatMap(allContent => writeFile(outfile, allContent)) | ||
.doto(() => verbose && deps.forEach(dep => console.log(`${dep.sourceFile || dep.id} -> ${outfile}`))); | ||
}; | ||
} | ||
module.exports = function browserifyGlobalPack(b, opts) { | ||
if (typeof opts.writeToDir !== 'string') { | ||
throw new Error('Browserify-global-pack requires the "writeToDir" option to be set'); | ||
if (!(opts.writeToDir || opts.getOutfile)) { | ||
throw new Error('Browserify-global-pack requires "writeToDir" or "getOutfile" to be set'); | ||
} | ||
b.pipeline.get('pack').splice(0, 1, packAndWrite(opts.writeToDir)); | ||
if (opts.writeToDir && opts.getOutfile) { | ||
throw new Error('Overspecified: Browserify-global-pack does not allow "writeToDir" or "getOutfile" to be set together'); | ||
} | ||
b.pipeline.get('pack').splice(0, 1, packAndWrite(opts.writeToDir, opts)); | ||
}; | ||
{ | ||
"name": "browserify-global-pack", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -31,2 +31,6 @@ const expect = require('chai').expect, | ||
it ('throws error if both writeToDir and getOutfile are specified', function () { | ||
expect(()=>browserifyGlobalPack({writeToDir: 'foo', getOutfile: ()=>{}})).to.throw(Error); | ||
}); | ||
it('writes global-pack chunks to files in the directory specified by writeToDir', function (done) { | ||
@@ -40,8 +44,10 @@ const bundler = browserify({fullPaths: true}) | ||
bundlePromise.then(() => { | ||
EXPECTED_FILES.forEach((filename, index) => { | ||
assertFileContent(path.join(__dirname, './bundle', filename), expectedOutput[index]); | ||
}); | ||
done(); | ||
}); | ||
bundlePromise | ||
.then(() => { | ||
EXPECTED_FILES.forEach((filename, index) => { | ||
assertFileContent(path.join(__dirname, './bundle', filename), expectedOutput[index]); | ||
}); | ||
}) | ||
.then(() => done()) | ||
.catch(err => done(err)); | ||
}); | ||
@@ -58,14 +64,15 @@ | ||
bundlePromise.then(()=>{ | ||
EXPECTED_FILES.forEach((filename, index)=> { | ||
assertFileContent(path.join(__dirname, 'bundle', filename), expectedOutput[index].replace(/window.scope/g, 'foo.bar')); | ||
}); | ||
done(); | ||
}); | ||
bundlePromise | ||
.then(()=>{ | ||
EXPECTED_FILES.forEach((filename, index)=> { | ||
assertFileContent(path.join(__dirname, 'bundle', filename), expectedOutput[index].replace(/window.modules/g, 'foo.bar')); | ||
}); | ||
}) | ||
.then(() => done()) | ||
.catch(done); | ||
}); | ||
it ('works with expose', function () { | ||
it ('sets filenames to expose expose values', function (done) { | ||
const bundler = browserify({fullPaths: true}) | ||
.require({ | ||
file: path.join(__dirname, 'input', './a.js'), | ||
.require(path.join(__dirname, 'input', './a.js'), { | ||
expose: 'foo' | ||
@@ -78,7 +85,46 @@ }) | ||
bundlePromise.then(() => { | ||
assertFileContent(path.join(__dirname, 'bundle', 'foo.js'), expectedOutput[1]); | ||
done(); | ||
}); | ||
bundlePromise | ||
.then(() => { | ||
expect(fs.pathExistsSync(path.join(__dirname, 'bundle', 'foo.js'))).to.be.true; | ||
}) | ||
.then(() => done()) | ||
.catch(done); | ||
}); | ||
it('sets file paths with custom function if getOutpath', function (done) { | ||
const bundler = browserify({fullPaths: true}) | ||
.require(path.join(__dirname, 'input', './a.js')) | ||
.plugin(browserifyGlobalPack, { | ||
getOutfile: () => path.join(__dirname, 'bundle', 'out-' + (i++) + '.js') | ||
}), | ||
bundlePromise = _(bundler.bundle()).toPromise(Promise); | ||
let i = 0; | ||
bundlePromise | ||
.then(() => { | ||
EXPECTED_FILES.forEach((filename, index) => { | ||
assertFileContent(path.join(__dirname, 'bundle', 'out-' + index + '.js'), expectedOutput[index]); | ||
}); | ||
}) | ||
.then(() => done()) | ||
.catch(done); | ||
}); | ||
it ('puts modules associated with the same filename in the same file', function (done) { | ||
const bundler = browserify({fullPaths: true}) | ||
.require(path.join(__dirname, 'input', './a.js')) | ||
.plugin(browserifyGlobalPack, { | ||
getOutfile: () => path.join(__dirname, 'bundle', 'out-' + (i++ <= 1 ? 0 : 1) + '.js') | ||
}), | ||
bundlePromise = _(bundler.bundle()).toPromise(Promise); | ||
let i = 0; | ||
bundlePromise | ||
.then(() => { | ||
assertFileContent(path.join(__dirname, 'bundle', 'out-0.js'), expectedOutput[0] + expectedOutput[1]); | ||
assertFileContent(path.join(__dirname, 'bundle', 'out-1.js'), expectedOutput[2] + expectedOutput[3]); | ||
}) | ||
.then(() => done()) | ||
.catch(done); | ||
}); | ||
}); | ||
@@ -85,0 +131,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11875
204