Skip to content

Commit d5a6c91

Browse files
Merge pull request #24 from taye/master
Allow '*' pattern in standalone option
2 parents b68c46f + 8b03a0b commit d5a6c91

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

lib/expand-globs.js

+35
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ function expandGlobs (globs, options) {
5353
fileSet.mapFile = fileSet.outputFile + '.map';
5454
}
5555

56+
if (options.standalone) {
57+
fileSet.standalone = getStandalone(entryFile, pattern, options.standalone);
58+
}
59+
5660
matches.push(fileSet);
5761
});
5862
});
@@ -132,3 +136,34 @@ function rename (file, baseDir, namePattern) {
132136
let outputDir = path.join(patternDir, relativeDir); // dest/subdir
133137
return path.join(outputDir, fileBaseName + fileExtName); // dest/subdir/my-file.min.js
134138
}
139+
140+
/**
141+
* Returns the output file name, based on the the entry file name, the base directory,
142+
* and the output path/pattern.
143+
*
144+
* @param {string} file - The source file path and name (e.g. "my-module/sub-module.js")
145+
* @param {string} filePattern - The pattern that got the file (e.g. "my-module/*.js")
146+
* @param {string} namePattern - The standalone option pattern (e.g. "my-module-*")
147+
* @returns {string} - The UMD standalone module name (e.g. "my-module-sub-module")
148+
*/
149+
function getStandalone (file, filePattern, standalonePattern) {
150+
let starIndex = filePattern.indexOf('*');
151+
152+
if (starIndex === -1) {
153+
return standalonePattern;
154+
}
155+
156+
let regExpString = Array.from(filePattern)
157+
.map((character, index) =>
158+
index === starIndex
159+
// replace the star with a capture
160+
? '(.*)'
161+
// replace the character with a unicode escape
162+
: `\\u${`000${character.charCodeAt(0).toString(16)}`.substr(-4)}`)
163+
.join('');
164+
let globRegExp = new RegExp(regExpString);
165+
let match = file.match(globRegExp);
166+
let standaloneName = standalonePattern.replace('*', match ? match[1] : '');
167+
168+
return standaloneName;
169+
}

lib/file-set.js

+6
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@ function FileSet () {
2525
* @type {string}
2626
*/
2727
this.mapFile = '';
28+
29+
/**
30+
* The UMD standalone module name for a Browserify bundle
31+
* @type {string}
32+
*/
33+
this.standalone = '';
2834
}

lib/write-bundles.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ function createMinifiedBundle (mainFiles, transforms, events, options) {
9696
// We're creating multiple output files, so append ".min" to the minified file
9797
minifiedFiles.entryFile = mainFiles.entryFile;
9898
minifiedFiles.outputFile = util.appendToFileName(mainFiles.outputFile, '.min');
99+
minifiedFiles.standalone = mainFiles.standalone;
99100
if (options.debug) {
100101
minifiedFiles.mapFile = minifiedFiles.outputFile + '.map';
101102
}
@@ -127,6 +128,7 @@ function createTestBundle (mainFiles, transforms, events, options) {
127128
// We're creating multiple output files, so append ".test" to the test file
128129
testFiles.entryFile = mainFiles.entryFile;
129130
testFiles.outputFile = util.appendToFileName(mainFiles.outputFile, '.test');
131+
testFiles.standalone = mainFiles.standalone;
130132
}
131133
else {
132134
// We're ONLY creating a test file, so this is the main output file
@@ -154,7 +156,7 @@ function newify (fileSet, transforms, events, options) {
154156
// We always set these options
155157
let browserifyOptions = {
156158
entries: fileSet.entryFile,
157-
standalone: options.standalone || undefined,
159+
standalone: fileSet.standalone || undefined,
158160
debug: !!fileSet.mapFile,
159161
};
160162

test/specs/standalone.spec.js

+38
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,44 @@ describe('simplifyify --standalone', () => {
151151
});
152152
});
153153

154+
it('should create UMD modules with names derived from pattern', function (done) {
155+
cli.run('es5/lib/*.js --standalone Fizz.* --outfile es5/dist/',
156+
function (err, stdout) {
157+
if (err) {
158+
return done(err);
159+
}
160+
161+
expect(stdout).to.contain('es5/lib/index.js --> es5/dist/index.js');
162+
expect(stdout).to.contain('es5/lib/hello-world.js --> es5/dist/hello-world.js');
163+
164+
assert.directoryContents('es5/dist', [
165+
'index.js',
166+
'hello-world.js',
167+
]);
168+
169+
170+
assert.fileContents('es5/dist/index.js', function (contents) {
171+
assert.noBanner(contents);
172+
assert.hasUmdPreamble(contents);
173+
assert.notMinified(contents);
174+
assert.noSourceMap(contents);
175+
assert.noCoverage(contents);
176+
expect(contents).to.match(/\.Fizz = /);
177+
expect(contents).to.match(/\.index = /);
178+
});
179+
assert.fileContents('es5/dist/hello-world.js', function (contents) {
180+
assert.noBanner(contents);
181+
assert.hasUmdPreamble(contents);
182+
assert.notMinified(contents);
183+
assert.noSourceMap(contents);
184+
assert.noCoverage(contents);
185+
expect(contents).to.match(/\.Fizz = /);
186+
expect(contents).to.match(/\.helloWorld = /);
187+
});
188+
done();
189+
});
190+
});
191+
154192
it('should create a bundle and sourcemap for a universal library', (done) => {
155193
cli.run('universal-lib/lib/browser.js --outfile universal-lib/dist/universal-lib.js --standalone universal --bundle --debug --minify',
156194
function (err, stdout) {

0 commit comments

Comments
 (0)