Skip to content

Commit abeba3d

Browse files
committed
feat(source-maps): bundling improvements
multiple fixes to source maps: - fixes offset problems caused by: - prepended scripts; - duplicate dependency main module; - remove sourece map file comments; - add EOL separator for the anonymous module wrapper; closes #659, related to #624
1 parent 2b2d3ce commit abeba3d

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

lib/build/amodro-trace/write/packages.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function packages(options) {
2929
filePath, contents, options);
3030

3131
if (packageName && !hasPackageName) {
32-
contents += ';define(\'' + packageName + '\', [\'' + moduleName +
32+
contents += '\n;define(\'' + packageName + '\', [\'' + moduleName +
3333
'\'], function (main) { return main; });\n';
3434
}
3535

lib/build/bundle.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,14 @@ exports.Bundle = class {
112112
}
113113

114114
getBundledFiles() {
115-
return this.includes.reduce((a, b) => a.concat(b.getAllFiles()), []);
115+
return uniqueBy(this.includes.reduce((a, b) => a.concat(b.getAllFiles()), []), (file) => file.path);
116116
}
117117

118118
write(platform) {
119119
if (!this.requiresBuild) {
120120
return Promise.resolve();
121121
}
122122

123-
124123
let work = Promise.resolve();
125124
let loaderOptions = this.bundler.loaderOptions;
126125
let buildOptions = this.buildOptions;
@@ -191,8 +190,8 @@ exports.Bundle = class {
191190
: null;
192191

193192
if (sourceMap !== null) {
194-
// path.posix.relative(from, to) does not work here
195-
sourceMap.sourceRoot = parsedPath.dir.substring(process.cwd().length);
193+
let sourceRoot = parsedPath.dir.substring(process.cwd().length);
194+
sourceMap.sourceRoot = sourceRoot.replace(/\\/g, '\/');
196195
}
197196

198197
return sourceMap;
@@ -202,11 +201,17 @@ exports.Bundle = class {
202201
sourceMap = acquireSourceMapForDependency(currentFile);
203202
}
204203

204+
let content;
205+
205206
if (sourceMap) {
206207
needsSourceMap = true;
208+
content = Convert.removeMapFileComments(currentFile.contents);
209+
}
210+
else {
211+
content = currentFile.contents;
207212
}
208213

209-
concat.add(currentFile.path, currentFile.contents, sourceMap ? JSON.stringify(sourceMap) : undefined);
214+
concat.add(currentFile.path, content, sourceMap ? JSON.stringify(sourceMap) : undefined);
210215
}
211216

212217
let mapContents;
@@ -386,3 +391,11 @@ function unique(collection) {
386391

387392
return a;
388393
}
394+
395+
function uniqueBy(collection, key) {
396+
var seen = {};
397+
return collection.filter((item) => {
398+
var k = key(item);
399+
return seen.hasOwnProperty(k) ? false : (seen[k] = true);
400+
})
401+
}

lib/build/concat-with-sourcemaps/index.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ Concat.prototype.add = function(filePath, content, sourceMap) {
4545
}
4646
this.contentParts.push(content);
4747

48+
var contentString = content.toString();
49+
var lines = contentString.split('\n').length;
50+
4851
if (sourceMap && this.sourceMapping) {
49-
var contentString = content.toString();
50-
var lines = contentString.split('\n').length;
5152

5253
if (Object.prototype.toString.call(sourceMap) === '[object String]')
5354
sourceMap = JSON.parse(sourceMap);
@@ -97,13 +98,16 @@ Concat.prototype.add = function(filePath, content, sourceMap) {
9798
this._sourceMap.setSourceContent(filePath, sourceMap.sourcesContent[0]);
9899
}
99100
}
100-
if (lines > 1)
101-
this.columnOffset = 0;
102-
if (this.separatorLineOffset === 0)
103-
this.columnOffset += contentString.length - Math.max(0, contentString.lastIndexOf('\n')+1);
104-
this.columnOffset += this.separatorColumnOffset;
105-
this.lineOffset += lines - 1 + this.separatorLineOffset;
106101
}
102+
103+
if (lines > 1)
104+
this.columnOffset = 0;
105+
106+
if (this.separatorLineOffset === 0)
107+
this.columnOffset += contentString.length - Math.max(0, contentString.lastIndexOf('\n')+1);
108+
109+
this.columnOffset += this.separatorColumnOffset;
110+
this.lineOffset += lines - 1 + this.separatorLineOffset;
107111
};
108112

109113
Object.defineProperty(Concat.prototype, 'content', {

spec/lib/build/bundle.spec.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,31 @@ describe('the Bundle module', () => {
203203
});
204204

205205
it('getBundledFiles returns all files of all includes', () => {
206+
let aFile = {path:'a.js'};
207+
let bFile = {path:'b.js'};
208+
let cFile = {path:'c.js'};
209+
210+
sut.includes = [{
211+
getAllFiles: () => [aFile, bFile]
212+
}, {
213+
getAllFiles: () => [cFile]
214+
}];
215+
216+
expect(sut.getBundledFiles()).toEqual([aFile, bFile, cFile]);
217+
});
218+
219+
it('getBundledFiles returns unique files of all includes', () => {
220+
let aFile = {path:'a.js'};
221+
let bFile = {path:'b.js'};
222+
let cFile = {path:'c.js'};
223+
206224
sut.includes = [{
207-
getAllFiles: () => ['a.js', 'b.js']
225+
getAllFiles: () => [aFile, bFile]
208226
}, {
209-
getAllFiles: () => ['c.js']
227+
getAllFiles: () => [cFile, cFile]
210228
}];
211229

212-
expect(sut.getBundledFiles()).toEqual(['a.js', 'b.js', 'c.js']);
230+
expect(sut.getBundledFiles()).toEqual([aFile, bFile, cFile]);
213231
});
214232

215233
it('configures dependencies in the same order as they were entered to prevent a wrong module load order', done => {

0 commit comments

Comments
 (0)