Skip to content

Commit 1f3cfb8

Browse files
committed
preExisting still rewrite urls but do not add extra newline
1 parent a83c5fa commit 1f3cfb8

File tree

7 files changed

+40
-33
lines changed

7 files changed

+40
-33
lines changed

.jshintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"node": true,
33
"strict": true,
4-
"unused": true,
4+
"unused": "vars",
55
"undef": true
66
}

src/init/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ function init(options) {
3636
});
3737

3838
var fileContent = file.contents.toString();
39-
var sourceMap;
40-
var preExisting = utils.getPreExisting(fileContent);
39+
var sourceMap, preExistingComment;
4140

4241
if (options.loadMaps) {
4342
var result = loadMaps({file:file, fileContent:fileContent}, options);
4443
sourceMap = result.map;
4544
fileContent = result.content;
45+
preExistingComment = result.preExistingComment;
4646
}
4747

4848
if (!sourceMap && options.identityMap) {
@@ -124,8 +124,8 @@ function init(options) {
124124
sourcesContent: [fileContent]
125125
};
126126
}
127-
else if(preExisting !== null && typeof preExisting !== 'undefined')
128-
sourceMap.preExisting = preExisting;
127+
else if(preExistingComment !== null && typeof preExistingComment !== 'undefined')
128+
sourceMap.preExistingComment = preExistingComment;
129129

130130
sourceMap.file = unixStylePath(file.relative);
131131
file.sourceMap = sourceMap;

src/init/loadMaps.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module.exports = function loadMaps (lOpts, options) {
1616
var fileContent = lOpts.fileContent;
1717
var file = lOpts.file;
1818

19-
var sources = { path: '', map: null , content: fileContent};
19+
var sources = { path: '', map: null , content: fileContent, preExistingComment: null};
2020

2121
_getInlineSources({sources:sources, file:file, options:options});
2222
if (!sources.map) // ahh not inline, so try file
@@ -82,6 +82,8 @@ function _getInlineSources(args) {
8282
var sources = args.sources,
8383
file = args.file,
8484
options = args.options;
85+
86+
sources.preExistingComment = utils.getInlinePreExisting(sources.content);
8587
// Try to read inline source map
8688
sources.map = convert.fromSource(sources.content, options.largeFile);
8789

@@ -92,6 +94,7 @@ function _getInlineSources(args) {
9294
// sources in map are relative to the source file
9395
sources.path = path.dirname(file.path);
9496
if (!options.largeFile) {
97+
debug('comment REMOVED');
9598
sources.content = convert.removeComments(sources.content);
9699
}
97100
}
@@ -104,7 +107,8 @@ function _getFileSources(args) {
104107

105108
var mapFile;
106109
if (mapComment) {
107-
mapFile = path.resolve(path.dirname(file.path), mapComment[1] || mapComment[2]);
110+
sources.preExistingComment = mapComment[1] || mapComment[2];
111+
mapFile = path.resolve(path.dirname(file.path), sources.preExistingComment);
108112
sources.content = convert.removeMapFileComments(sources.content);
109113
// if no comment try map file with same name as source file
110114
} else {

src/utils.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ See http://stackoverflow.com/questions/10229144/bug-with-regexp-in-javascript-wh
1818
1919
So we either need to use a new instance of a regex everywhere.
2020
*/
21-
var sourceMapUrlRegEx = function(){ return /\/\/\# sourceMappingURL\=.*/g;}
21+
var sourceMapUrlRegEx = function(){ return /\/\/\# sourceMappingURL\=.*/g;};
2222

2323

2424
var getCommentFormatter = function (file) {
@@ -29,12 +29,12 @@ var getCommentFormatter = function (file) {
2929
return '';
3030
};
3131

32-
if (file.sourceMap.preExisting){
33-
debug('preExisting commentFormatter');
32+
if (file.sourceMap.preExistingComment){
33+
debug('preExistingComment commentFormatter');
3434
commentFormatter = function(url) {
35-
return file.sourceMap.preExisting;
35+
return "//# sourceMappingURL=" + url + newline;
3636
};
37-
return commentFormatter
37+
return commentFormatter;
3838
}
3939

4040
switch (extension) {
@@ -51,24 +51,24 @@ var getCommentFormatter = function (file) {
5151
};
5252
break;
5353
default:
54-
debug('unknown commentFormatter')
54+
debug('unknown commentFormatter');
5555
}
5656

5757
return commentFormatter;
58-
}
58+
};
5959

60-
var getPreExisting = function(fileContent){
60+
var getInlinePreExisting = function(fileContent){
6161
if(sourceMapUrlRegEx().test(fileContent)){
6262
debug('has preExisting');
6363
return fileContent.match(sourceMapUrlRegEx())[0];
6464
}
65-
}
65+
};
6666

6767
module.exports = {
6868
unixStylePath: unixStylePath,
6969
PLUGIN_NAME: PLUGIN_NAME,
7070
urlRegex: urlRegex,
7171
sourceMapUrlRegEx: sourceMapUrlRegEx,
7272
getCommentFormatter: getCommentFormatter,
73-
getPreExisting: getPreExisting
73+
getInlinePreExisting: getInlinePreExisting
7474
};

src/write.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ var utils = require('./utils'),
55
through = require('through2'),
66
fs = require('graceful-fs'),
77
path = require('path'),
8-
File = require('vinyl'),
98
stripBom = require('strip-bom');
109

1110
/**
@@ -97,7 +96,7 @@ function write(destPath, options) {
9796
if (destPath === undefined || destPath === null) {
9897
// encode source map into comment
9998
var base64Map = new Buffer(JSON.stringify(sourceMap)).toString('base64');
100-
debug("basic comment")
99+
debug("basic comment");
101100
comment = commentFormatter('data:application/json;charset=' + options.charset + ';base64,' + base64Map);
102101
} else {
103102
var mapFile = path.join(destPath, file.relative) + '.map';
@@ -152,18 +151,17 @@ function write(destPath, options) {
152151
}
153152
sourceMapPathRelative = prefix + path.join('/', sourceMapPathRelative);
154153
}
155-
debug("destPath comment")
154+
debug("destPath comment");
156155
comment = commentFormatter(unixStylePath(sourceMapPathRelative));
157156

158157
if (options.sourceMappingURL && typeof options.sourceMappingURL === 'function') {
159-
debug("options.sourceMappingURL comment")
158+
debug("options.sourceMappingURL comment");
160159
comment = commentFormatter(options.sourceMappingURL(file));
161160
}
162161
}
163162

164-
var preExisting = options.preExisting && utils.getPreExisting(String(file.contents));
165163
// append source map comment
166-
if (options.addComment && !preExisting){
164+
if (options.addComment){
167165
file.contents = Buffer.concat([file.contents, new Buffer(comment)]);
168166
}
169167

test/integration.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ var fs = require('fs');
1010
var sourceContent = fs.readFileSync(join(__dirname, 'assets/helloworld.js')).toString();
1111

1212

13+
function base64JSON(object) {
14+
return 'data:application/json;charset=utf8;base64,' + new Buffer(JSON.stringify(object)).toString('base64');
15+
}
16+
1317
function moveHtml(dest, t){
1418
return gulp.src(join(__dirname, './assets/*.html'))
1519
.pipe(gulp.dest('tmp/' + dest))
@@ -18,7 +22,7 @@ function moveHtml(dest, t){
1822

1923
debug('running');
2024

21-
test('creates inline mapping', function(t) {
25+
test('combined: creates inline mapping', function(t) {
2226

2327
gulp.src(join(__dirname, './assets/helloworld.js'))
2428
.pipe(sourcemaps.init())
@@ -42,17 +46,17 @@ test('creates inline mapping', function(t) {
4246
});
4347
});
4448

45-
test('creates re-uses existing mapping', function(t) {
49+
test('combined: creates preExistingComment , no new previous line', function(t) {
4650
gulp.src(join(__dirname, './assets/helloworld.map.js'))
4751
.pipe(sourcemaps.init({loadMaps:true}))
4852
.pipe(sourcemaps.write())
4953
// .pipe(gulp.dest('tmp'))
5054
.on('data', function(data) {
5155
t.ok(data.sourceMap, 'should add a source map object');
52-
t.ok(!!data.sourceMap.preExisting, 'should know the sourcemap pre-existed');
56+
t.ok(!!data.sourceMap.preExistingComment, 'should know the sourcemap pre-existed');
5357
t.deepEqual(
5458
data.contents.toString(),
55-
sourceContent + "\n//# sourceMappingURL=data:application/json;charset=utf8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIiwic291cmNlcyI6WyJoZWxsb3dvcmxkLmpzIl0sInNvdXJjZXNDb250ZW50IjpbIid1c2Ugc3RyaWN0JztcblxuZnVuY3Rpb24gaGVsbG9Xb3JsZCgpIHtcbiAgICBjb25zb2xlLmxvZygnSGVsbG8gd29ybGQhJyk7XG59XG4iXSwiZmlsZSI6ImhlbGxvd29ybGQuanMifQ==",
59+
sourceContent + '\n//# sourceMappingURL=' + base64JSON(data.sourceMap) + '\n',
5660
'file should be sourcemapped'
5761
);
5862
t.end();

test/write.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var hookStd = require('hook-std');
1010
var debug = require('debug-fabulous')();
1111
var assign = require('object-assign');
1212
var utils = require('../src/utils');
13+
var convert = require('convert-source-map');
1314

1415
var sourceContent = fs.readFileSync(path.join(__dirname, 'assets/helloworld.js')).toString();
1516
var mappedContent = fs.readFileSync(path.join(__dirname, 'assets/helloworld.map.js')).toString();
@@ -52,7 +53,7 @@ function makeMappedFile() {
5253
path: path.join(__dirname, 'assets', 'helloworld.map.js'),
5354
contents: new Buffer(mappedContent)
5455
});
55-
file.sourceMap = makeSourceMap({preExisting:utils.getPreExisting(mappedContent)});
56+
file.sourceMap = makeSourceMap({preExistingComment:utils.getInlinePreExisting(mappedContent)});
5657
return file;
5758
}
5859

@@ -169,17 +170,17 @@ test('write: should detect whether a file uses \\n or \\r\\n and follow the exis
169170
}).write(file);
170171
});
171172

172-
test('write: preExisting', function(t) {
173+
test('write: preExistingComment', function(t) {
173174
var file = makeMappedFile();
174-
file.contents = new Buffer(file.contents.toString());
175+
file.contents = new Buffer(convert.removeComments(file.contents.toString()));
175176

176-
sourcemaps.write({preExisting:true})
177+
sourcemaps.write({preExistingComment:true})
177178
.on('data', function(data) {
178179
t.ok(data, 'should pass something through');
179-
t.ok(!!data.sourceMap.preExisting, 'should mark as preExisting');
180+
t.ok(!!data.sourceMap.preExistingComment, 'should mark as preExistingComment');
180181
t.equal(
181182
String(data.contents),
182-
mappedContent,'should add source map as comment');
183+
sourceContent + '\n//# sourceMappingURL=' + base64JSON(data.sourceMap) + '\n' ,'should add source map as comment');
183184
t.end();
184185
})
185186
.on('error', function() {

0 commit comments

Comments
 (0)