10000 Add support for specifying a source map URL in the to the `-p`/`--sou… · lodash/lodash@3ed9e0e · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ed9e0e

Browse files
committed
Add support for specifying a source map URL in the to the -p/--source-map build options.
Former-commit-id: 2098da69d7902497e2e67210d778b8f99a5ff8f0
1 parent 45bec0c commit 3ed9e0e

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ The following options are also supported:
176176
* `-h`, `--help` ............. Display help information
177177
* `-m`, `--minify` ......... Write only the minified production output
178178
* `-o`, `--output` ......... Write output to a given path/filename
179-
* `-p`, `--source-map` .. Generate a source map for the minified output
179+
* `-p`, `--source-map` .. Generate a source map for the minified output, using an optional source map URL
180180
* `-s`, `--silent` ......... Skip status updates normally logged to the console
181181
* `-V`, `--version` ....... Output current version of Lo-Dash
182182

build.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@
594594
' -h, --help Display help information',
595595
' -m, --minify Write only the minified production output',
596596
' -o, --output Write output to a given path/filename',
597-
' -p, --source-map Generate a source map for the minified output',
597+
' -p, --source-map Generate a source map for the minified output, using an optional source map URL',
598598
' -s, --silent Skip status updates normally logged to the console',
599599
' -V, --version Output current version of Lo-Dash',
600600
''
@@ -1304,13 +1304,16 @@
13041304
// the debug version of `source`
13051305
var debugSource;
13061306

1307+
// used to specify the source map URL
1308+
var sourceMapURL;
1309+
13071310
// used to report invalid command-line arguments
13081311
var invalidArgs = _.reject(options.slice(options[0] == 'node' ? 2 : 0), function(value, index, options) {
13091312
if (/^(?:-o|--output)$/.test(options[index - 1]) ||
13101313
/^(?:category|exclude|exports|iife|include|moduleId|minus|plus|settings|template)=.*$/.test(value)) {
13111314
return true;
13121315
}
1313-
return [
1316+
var result = [
13141317
'backbone',
13151318
'csp',
13161319
'legacy',
@@ -1328,6 +1331,12 @@
13281331
'-s', '--silent',
13291332
'-V', '--version'
13301333
].indexOf(value) > -1;
1334+
1335+
if (!result && /^(?:-p|--source-map)$/.test(options[index - 1])) {
1336+
result = true;
1337+
sourceMapURL = value;
1338+
}
1339+
return result;
13311340
});
13321341

13331342
// report invalid arguments
@@ -2440,6 +2449,7 @@
24402449
'isTemplate': isTemplate,
24412450
'modes': isIIFE && ['simple', 'hybrid'],
24422451
'outputPath': outputPath,
2452+
'sourceMapURL': sourceMapURL,
24432453
'onComplete': function(data) {
24442454
if (isCustom) {
24452455
data.source = addCommandsToHeader(data.source, options);

build/minify.js

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
https = require('https'),
88
path = require('path'),
99
spawn = require('child_process').spawn,
10+
zlib = require('zlib'),
1011
tar = require('../vendor/tar/tar.js'),
11-
zlib = require('zlib');
12+
_ = require('../lodash.js');
1213

1314
/** Load other modules */
1415
var preprocess = require('./pre-compile.js'),
@@ -80,7 +81,12 @@
8081
* onComplete - The function called once minification has finished.
8182
*/
8283
function minify(source, options) {
84+
// used to specify the source map URL
85+
var sourceMapURL;
86+
87+
// used to specify the default minifer modes
8388
var modes = ['simple', 'advanced', 'hybrid'];
89+
8490
source || (source = '');
8591
options || (options = {});
8692

@@ -89,6 +95,35 @@
8995
// convert commands to an options object
9096
options = source;
9197

98+
// used to report invalid command-line arguments
99+
var invalidArgs = _.reject(options.slice(options[0] == 'node' ? 2 : 0), function(value, index, options) {
100+
if (/^(?:-o|--output)$/.test(options[index - 1]) ||
101+
/^modes=.*$/.test(value)) {
102+
return true;
103+
}
104+
var result = [
105+
'-o', '--output',
106+
'-p', '--source-map',
107+
'-s', '--silent',
108+
'-t', '--template'
109+
].indexOf(value) > -1;
110+
111+
if (!result && /^(?:-p|--source-map)$/.test(options[index - 1])) {
112+
result = true;
113+
sourceMapURL = value;
114+
}
115+
return result;
116+
});
117+
118+
// report invalid arguments
119+
if (invalidArgs.length) {
120+
console.log(
121+
'\n' +
122+
'Invalid argument' + (invalidArgs.length > 1 ? 's' : '') +
123+
' passed: ' + invalidArgs.join(', ')
124+
);
125+
return;
126+
}
92127
var filePath = options[options.length - 1],
93128
isMapped = options.indexOf('-p') > -1 || options.indexOf('--source-map') > -1,
94129
isSilent = options.indexOf('-s') > -1 || options.indexOf('--silent') > -1,
@@ -114,7 +149,8 @@
114149
'isSilent': isSilent,
115150
'isTemplate': isTemplate,
116151
'modes': modes,
117-
'outputPath': outputPath
152+
'outputPath': outputPath,
153+
'sourceMapURL': sourceMapURL
118154
};
119155

120156
source = fs.readFileSync(filePath, 'utf8');
@@ -186,6 +222,7 @@
186222
this.isSilent = !!options.isSilent;
187223
this.isTemplate = !!options.isTemplate;
188224
this.outputPath = options.outputPath;
225+
this.sourceMapURL = options.sourceMapURL;
189226

190227
var modes = this.modes = options.modes;
191228
source = this.source = preprocess(source, options);
@@ -304,10 +341,11 @@
304341
function closureCompile(source, mode, callback) {
305342
var filePath = this.filePath,
306343
isAdvanced = mode == 'advanced',
307-
outputPath = this.outputPath,
308344
isMapped = this.isMapped,
345+
options = closureOptions.slice(),
346+
outputPath = this.outputPath,
309347
mapPath = getMapPath(outputPath),
310-
options = closureOptions.slice();
348+
sourceMapURL = this.sourceMapURL || path.basename(mapPath);
311349

312350
// remove copyright header to make other modifications easier
313351
var license = (/^(?:\s*\/\/.*\s*|\s*\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/\s*)*/.exec(source) || [''])[0];
@@ -369,7 +407,7 @@
369407
if (isMapped) {
370408
var mapOutput = fs.readFileSync(mapPath, 'utf8');
371409
fs.unlinkSync(mapPath);
372-
output = output.replace(/[\s;]*$/, '\n/*\n//@ sourceMappingURL=' + path.basename(mapPath)) + '\n*/';
410+
output = output.replace(/[\s;]*$/, '\n/*\n//@ sourceMappingURL=' + sourceMapURL) + '\n*/';
373411

374412
mapOutput = JSON.parse(mapOutput);
375413
mapOutput.file = path.basename(outputPath);

test/test-build.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,9 @@
645645
(function() {
646646
var mapCommands = [
647647
'-p',
648-
'--source-map'
648+
'-p custom.map',
649+
'--source-map',
650+
'--source-map custom.map'
649651
];
650652

651653
var outputCommands = [
@@ -661,9 +663,10 @@
661663
var basename = path.basename(data.outputPath, '.js'),
662664
comment = (/(\s*\/\/.*\s*|\s*\/\*[^*]*\*+(?:[^\/][^*]*\*+)*\/\s*)$/.exec(data.source) || [])[0],
663665
sources = /foo.js/.test(outputCommand) ? ['foo.js'] : ['lodash' + (outputCommand.length ? '' : '.custom') + '.js'],
664-
sourceMap = JSON.parse(data.sourceMap);
666+
sourceMap = JSON.parse(data.sourceMap),
667+
sourceMapURL = (/\w+(?=\.map$)/.exec(mapCommand) || [basename])[0];
665668

666-
ok(RegExp('/\\*\\n//@ sourceMappingURL=' + basename + '.map\\n\\*/').test(comment), basename);
669+
ok(RegExp('/\\*\\n//@ sourceMappingURL=' + sourceMapURL + '.map\\n\\*/').test(comment), basename);
667670
equal(sourceMap.file, basename + '.js', basename);
668671
deepEqual(sourceMap.sources, sources, basename);
669672

@@ -674,7 +677,7 @@
674677
if (outputCommand.indexOf('-m') < 0) {
675678
callback = _.after(2, callback);
676679
}
677-
build(['-s', mapCommand].concat(outputCommand), callback);
680+
build(['-s'].concat(mapCommand.split(' '), outputCommand), callback);
678681
});
679682
});
680683
});

0 commit comments

Comments
 (0)
0