Skip to content

Commit 6709bb5

Browse files
BREAKING CHANGE - The "default" (non-minified) bundle is no longer created automatically, unless you don't specify ANY output options. But if you specify an output option such as --test or --minify, then ONLY the outputs that you explicitly specify are created. If you ALSO want a non-minified bundle created, then add --bundle.
1 parent ff96fbc commit 6709bb5

15 files changed

+257
-102
lines changed

Diff for: bin/simplifyify.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ function parseArguments() {
1818
.option('-o, --outfile <filespec>', 'The output file or directory. May include a filename pattern (e.g. "*.bundle.js")')
1919
.option('-u, --exclude <filespec>', 'File path or glob pattern to exclude')
2020
.option('-s, --standalone <name>', 'Export as a named UMD bundle')
21-
.option('-d, --debug', 'Output source maps for debugging (.map)')
22-
.option('-m, --minify', 'Output a minified copy of the bundle (.min.js)')
23-
.option('-v, --test', 'Output a bundle with code-coverage instrumentation for testing (.test.js)')
21+
.option('-b, --bundle', 'Create a non-minified bundle for development (.js)')
22+
.option('-d, --debug', 'Create a source map for debugging (.js.map)')
23+
.option('-m, --minify', 'Create a minified bundle for production (.min.js)')
24+
.option('-v, --test', 'Create a bundle with code-coverage instrumentation for testing (.test.js)')
2425
.option('-w, --watch', 'Watch source file(s) and rebuild the bundle(s) automatically')
2526
.on('--help', function() {
2627
console.log(
@@ -33,7 +34,7 @@ function parseArguments() {
3334
'\n' +
3435
' Examples:\n' +
3536
'\n' +
36-
' simplifyify src/index.js --outfile dist/bundle.js --debug --minify --test\n' +
37+
' simplifyify src/index.js --outfile dist/bundle.js --bundle --debug --minify --test\n' +
3738
'\n' +
3839
' Output Files: \n' +
3940
' dist/bundle.js\n' +
@@ -43,7 +44,7 @@ function parseArguments() {
4344
' dist/bundle.test.js\n' +
4445
'\n' +
4546
'\n' +
46-
' simplifyify "src/module-*.js" --outfile "dist/*.bundle.js" --minify\n' +
47+
' simplifyify "src/module-*.js" --outfile "dist/*.bundle.js" --bundle --minify\n' +
4748
'\n' +
4849
' Output Files: \n' +
4950
' dist/module-one.bundle.js\n' +

Diff for: lib/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = simplifyify;
1111
* outfile: string,
1212
* exclude: string,
1313
* standalone: string,
14+
* bundle: boolean,
1415
* debug: boolean,
1516
* minify: boolean,
1617
* test: boolean,

Diff for: lib/write-bundles.js

+110-69
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ var browserify = require('browserify'),
1212

1313
module.exports = writeBundles;
1414

15+
var uglifyifyOptions = {
16+
global: true,
17+
exts: ['.js', '.json'],
18+
output: {
19+
// Keep important comments when minifying
20+
comments: /^!|^\*!|@preserve|@license|@cc_on/
21+
}
22+
};
23+
24+
var istanbulOptions = {
25+
ignore: ['**/*.json', '**/*.html', '**/*.md', '**/*.txt'],
26+
defaultIgnore: false
27+
};
28+
1529
/**
1630
* Writes Browserify bundles for the given entry file.
1731
* At least one bundle is created (the outputFile), but additional ones may be
@@ -22,42 +36,23 @@ module.exports = writeBundles;
2236
* @param {Options} options - Bundling options
2337
*/
2438
function writeBundles(mainFiles, events, options) {
25-
var bundles = [];
26-
var bundleFiles = [];
39+
var bundlers = [];
2740

28-
// Create the main bundle
29-
var mainBundle = newify(mainFiles, events, options);
30-
bundles.push(mainBundle);
31-
bundleFiles.push(mainFiles);
41+
// If no output options are specified, then default to --bundle
42+
if (!options.bundle && !options.minify && !options.test) {
43+
options.bundle = true;
44+
}
3245

33-
if (options.minify) {
34-
// The files for the minified bundle
35-
var minifiedFiles = new FileSet();
36-
minifiedFiles.entryFile = mainFiles.entryFile;
37-
minifiedFiles.outputFile = util.appendToFileName(mainFiles.outputFile, '.min');
38-
if (options.debug) {
39-
minifiedFiles.mapFile = minifiedFiles.outputFile + '.map';
40-
}
41-
bundleFiles.push(minifiedFiles);
46+
if (options.bundle) {
47+
bundlers.push(createMainBundler(mainFiles, events, options));
48+
}
4249

43-
// Create the minified bundle
44-
var minifiedBundle = newify(minifiedFiles, events, options);
45-
addMinifyTransform(minifiedBundle);
46-
bundles.push(minifiedBundle);
50+
if (options.minify) {
51+
bundlers.push(createMinifiedBundler(mainFiles, events, options));
4752
}
4853

4954
if (options.test) {
50-
// The files for the test bundle
51-
var testFiles = new FileSet();
52-
testFiles.entryFile = mainFiles.entryFile;
53-
testFiles.outputFile = util.appendToFileName(mainFiles.outputFile, '.test');
54-
bundleFiles.push(testFiles);
55-
56-
// Create the test bundle
57-
var testBundle = newify(testFiles, events, options);
58-
addCoverageTransform(testBundle);
59-
addMinifyTransform(testBundle);
60-
bundles.push(testBundle);
55+
bundlers.push(createTestBundler(mainFiles, events, options));
6156
}
6257

6358
/**
@@ -67,12 +62,12 @@ function writeBundles(mainFiles, events, options) {
6762
* @param {number} index - The bundle to build (from the {@link bundles} array)
6863
**/
6964
function writeBundle(index) {
70-
if (bundles[index]) {
65+
if (bundlers[index]) {
7166
// Write this bundle
72-
bundle(bundles[index], bundleFiles[index]);
67+
bundle(bundlers[index]);
7368

7469
// Write the next bundle when this one finishes
75-
bundles[index].once('end', function() {
70+
bundlers[index].once('end', function() {
7671
writeBundle(index + 1);
7772
});
7873
}
@@ -81,6 +76,78 @@ function writeBundles(mainFiles, events, options) {
8176
writeBundle(0);
8277
}
8378

79+
/**
80+
* Creates a Browserify instance that outputs the main (non-minified) bundle for the given entry file.
81+
*
82+
* @param {FileSet} mainFiles - The input & output files
83+
* @param {EventEmitter} events - Browserify events will be propagated to this EventEmitter
84+
* @param {Options} options - Bundling options
85+
* @returns {Browserify}
86+
*/
87+
function createMainBundler(mainFiles, events, options) {
88+
return newify(mainFiles, events, options);
89+
}
90+
91+
/**
92+
* Creates a Browserify instance that outputs the minified bundle for the given entry file.
93+
*
94+
* @param {FileSet} mainFiles - The input & output files
95+
* @param {EventEmitter} events - Browserify events will be propagated to this EventEmitter
96+
* @param {Options} options - Bundling options
97+
* @returns {Browserify}
98+
*/
99+
function createMinifiedBundler(mainFiles, events, options) {
100+
var minifiedFiles = new FileSet();
101+
102+
if (options.bundle || options.test) {
103+
// We're creating multiple output files, so append ".min" to the minified file
104+
minifiedFiles.entryFile = mainFiles.entryFile;
105+
minifiedFiles.outputFile = util.appendToFileName(mainFiles.outputFile, '.min');
106+
if (options.debug) {
107+
minifiedFiles.mapFile = minifiedFiles.outputFile + '.map';
108+
}
109+
}
110+
else {
111+
// We're ONLY creating a minified file, so this is the main output file
112+
minifiedFiles = mainFiles;
113+
}
114+
115+
var bundler = newify(minifiedFiles, events, options);
116+
bundler.transform(uglifyify, uglifyifyOptions);
117+
return bundler;
118+
}
119+
120+
/**
121+
* Creates a Browserify instance that outputs the test bundle (with code-coverage instrumentation)
122+
* for the given entry file.
123+
*
124+
* @param {FileSet} mainFiles - The input & output files
125+
* @param {EventEmitter} events - Browserify events will be propagated to this EventEmitter
126+
* @param {Options} options - Bundling options
127+
* @returns {Browserify}
128+
*/
129+
function createTestBundler(mainFiles, events, options) {
130+
var testFiles = new FileSet();
131+
132+
if (options.bundle || options.minify) {
133+
// We're creating multiple output files, so append ".test" to the test file
134+
testFiles.entryFile = mainFiles.entryFile;
135+
testFiles.outputFile = util.appendToFileName(mainFiles.outputFile, '.test');
136+
}
137+
else {
138+
// We're ONLY creating a test file, so this is the main output file
139+
testFiles = mainFiles;
140+
141+
// Don't produce source maps for test files (Istanbul doesn't support source maps anyway)
142+
testFiles.mapFile = '';
143+
}
144+
145+
var bundler = newify(testFiles, events, options);
146+
bundler.transform(uglifyify, uglifyifyOptions);
147+
bundler.transform(istanbul(istanbulOptions));
148+
return bundler;
149+
}
150+
84151
/**
85152
* Creates a new Browserify or Watchify instance
86153
*
@@ -122,54 +189,28 @@ function newify(fileSet, events, options) {
122189
});
123190
}
124191

125-
return b;
126-
}
192+
// Remember the input/output files for this bundler
193+
b.files = fileSet;
127194

128-
/**
129-
* Uses the Uglifyify transform to minify the given Browserify bundle
130-
*
131-
* @param {Browserify} b - The Browserify object to transform
132-
*/
133-
function addMinifyTransform(b) {
134-
b.transform(uglifyify, {
135-
global: true,
136-
exts: ['.js', '.json'],
137-
output: {
138-
// Keep important comments when minifying
139-
comments: /^!|^\*!|@preserve|@license|@cc_on/
140-
}
141-
});
142-
}
143-
144-
/**
145-
* Uses the Istanbul transform to add code-coverage instrumentation the given Browserify bundle
146-
*
147-
* @param {Browserify} b - The Browserify object to transform
148-
*/
149-
function addCoverageTransform(b) {
150-
b.transform(istanbul({
151-
ignore: ['**/*.json', '**/*.html', '**/*.md', '**/*.txt'],
152-
defaultIgnore: false
153-
}));
195+
return b;
154196
}
155197

156198
/**
157199
* Writes the output file (and possibly its .map file) for the given Browserify object
158200
*
159201
* @param {Browserify} b - The Browserify object to bundle
160-
* @param {FileSet} fileSet - The input & output files for this bundle
161202
*/
162-
function bundle(b, fileSet) {
203+
function bundle(b) {
163204
var stream = b.bundle();
164205
stream.on('end', b.emit.bind(b, 'end'));
165206
stream.on('error', b.emit.bind(b, 'error'));
166207

167-
if (fileSet.mapFile) {
168-
util.ensureFileExists(fileSet.mapFile);
169-
var dirname = path.dirname(fileSet.mapFile);
170-
stream = stream.pipe(exorcist(fileSet.mapFile, null, null, dirname));
208+
if (b.files.mapFile) {
209+
util.ensureFileExists(b.files.mapFile);
210+
var dirname = path.dirname(b.files.mapFile);
211+
stream = stream.pipe(exorcist(b.files.mapFile, null, null, dirname));
171212
}
172213

173-
util.ensureFileExists(fileSet.outputFile);
174-
stream.pipe(fs.createWriteStream(fileSet.outputFile));
214+
util.ensureFileExists(b.files.outputFile);
215+
stream.pipe(fs.createWriteStream(b.files.outputFile));
175216
}

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simplifyify",
3-
"version": "1.6.0",
3+
"version": "2.0.0-beta.0",
44
"description": "A simplified Browserify and Watchify CLI",
55
"keywords": [
66
"browserify",
@@ -37,6 +37,7 @@
3737
"url": "https://github.com/BigstickCarpet/simplifyify.git"
3838
},
3939
"devDependencies": {
40+
"babelify": "^7.2.0",
4041
"chai": "^3.4.1",
4142
"coveralls": "^2.11.6",
4243
"del": "^2.2.0",

Diff for: tests/specs/debug.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ describe('simplifyify --debug', function() {
9090
});
9191

9292
it('should create source maps for multiple minified files', function(done) {
93-
cli.run('es5/lib/**/index.js --debug --minify --outfile es5/dist/',
93+
cli.run('es5/lib/**/index.js --bundle --debug --minify --outfile es5/dist/',
9494
function(err, stdout) {
9595
if (err) {
9696
return done(err);
@@ -146,7 +146,7 @@ describe('simplifyify --debug', function() {
146146
});
147147

148148
it('should append ".map" when renaming output files', function(done) {
149-
cli.run('es5/lib/**/*.js --debug --minify --outfile es5/dist/*.bundle.es',
149+
cli.run('es5/lib/**/*.js --bundle --debug --minify --outfile es5/dist/*.bundle.es',
150150
function(err, stdout) {
151151
if (err) {
152152
return done(err);

Diff for: tests/specs/minify.spec.js

+49-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ var cli = require('../fixtures/cli'),
77
describe('simplifyify --minify', function() {
88
it('should minify a single file', function(done) {
99
cli.run('es5/lib/index.js --minify --outfile es5/dist/',
10+
function(err, stdout) {
11+
if (err) {
12+
return done(err);
13+
}
14+
15+
expect(stdout).to.contain('es5/lib/index.js --> es5/dist/index.js');
16+
17+
assert.directoryContents('es5/dist', 'index.js');
18+
19+
assert.fileContents('es5/dist/index.js', function(contents) {
20+
assert.hasPreamble(contents);
21+
assert.isMinified(contents);
22+
assert.noSourceMap(contents);
23+
assert.noCoverage(contents);
24+
});
25+
done();
26+
});
27+
});
28+
29+
it('should create a minified and non-minified file', function(done) {
30+
cli.run('es5/lib/index.js --bundle --minify --outfile es5/dist/',
1031
function(err, stdout) {
1132
if (err) {
1233
return done(err);
@@ -44,24 +65,14 @@ describe('simplifyify --minify', function() {
4465
}
4566

4667
expect(stdout).to.contain('es5/lib/index.js --> es5/dist/index.js');
47-
expect(stdout).to.contain('es5/lib/index.js --> es5/dist/index.min.js');
4868
expect(stdout).to.contain('es5/lib/say/index.js --> es5/dist/say/index.js');
49-
expect(stdout).to.contain('es5/lib/say/index.js --> es5/dist/say/index.min.js');
5069

5170
assert.directoryContents('es5/dist', [
5271
'index.js',
53-
'index.min.js',
5472
'say/index.js',
55-
'say/index.min.js'
5673
]);
5774

5875
assert.fileContents('es5/dist', ['index.js', 'say/index.js'], function(contents) {
59-
assert.hasPreamble(contents);
60-
assert.notMinified(contents);
61-
assert.noSourceMap(contents);
62-
assert.noCoverage(contents);
63-
});
64-
assert.fileContents('es5/dist', ['index.min.js', 'say/index.min.js'], function(contents) {
6576
assert.hasPreamble(contents);
6677
assert.isMinified(contents);
6778
assert.noSourceMap(contents);
@@ -71,8 +82,35 @@ describe('simplifyify --minify', function() {
7182
});
7283
});
7384

74-
it('should append ".min" when renaming output files', function(done) {
85+
it('should NOT append ".min" when renaming output files', function(done) {
7586
cli.run('es5/lib/**/*.js --minify --outfile es5/dist/*.foo.es5',
87+
function(err, stdout) {
88+
if (err) {
89+
return done(err);
90+
}
91+
92+
expect(stdout).to.contain('es5/lib/index.js --> es5/dist/index.foo.es5');
93+
expect(stdout).to.contain('es5/lib/hello-world.js --> es5/dist/hello-world.foo.es5');
94+
expect(stdout).to.contain('es5/lib/say/index.js --> es5/dist/say/index.foo.es5');
95+
96+
assert.directoryContents('es5/dist', [
97+
'index.foo.es5',
98+
'hello-world.foo.es5',
99+
'say/index.foo.es5',
100+
]);
101+
102+
assert.fileContents('es5/dist', ['index.foo.es5', 'hello-world.foo.es5', 'say/index.foo.es5'], function(contents) {
103+
assert.hasPreamble(contents);
104+
assert.isMinified(contents);
105+
assert.noSourceMap(contents);
106+
assert.noCoverage(contents);
107+
});
108+
done();
109+
});
110+
});
111+
112+
it('should append ".min" when renaming output files and producing multiple bundles', function(done) {
113+
cli.run('es5/lib/**/*.js --bundle --minify --outfile es5/dist/*.foo.es5',
76114
function(err, stdout) {
77115
if (err) {
78116
return done(err);

0 commit comments

Comments
 (0)