Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/build/amodro-trace/read/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const transformSync = require('@babel/core').transformSync;
* @param {string} fileName
* @param {string} fileContents
* @param {{} | boolean} inputSourceMap
* @returns {{ code: string, map: string, ast: any }}
* @returns {babel.BabelFileResult}
*/
module.exports = function es(fileName, fileContents, inputSourceMap) {
return transformSync(fileContents, {
Expand Down
9 changes: 6 additions & 3 deletions lib/build/bundled-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ exports.BundledSource = class {
this.includedIn = null;
this.includedBy = null;
this._contents = null;
this._transformedSourceMap = undefined;
this.requiresTransform = true;
}

get sourceMap() {
return this.file.sourceMap;
return this._transformedSourceMap || this.file.sourceMap;
}

get path() {
Expand Down Expand Up @@ -215,6 +216,7 @@ exports.BundledSource = class {

if (cache) {
this.contents = cache.contents;
this._transformedSourceMap = cache.transformedSourceMap;
deps = cache.deps;
} else {
let contents;
Expand All @@ -239,7 +241,7 @@ exports.BundledSource = class {

contents = result.code;
if (result.map) {
this.file.sourceMap = result.map; // save updated source map
this._transformedSourceMap = result.map;
}
} catch (e) {
logger.error('Could not convert to AMD module, skipping ' + modulePath);
Expand Down Expand Up @@ -279,7 +281,8 @@ exports.BundledSource = class {
if (useCache && hash) {
Utils.setCache(hash, {
deps: deps,
contents: this.contents
contents: this.contents,
transformedSourceMap: !this._transformedSourceMap ? undefined : this._transformedSourceMap // avoid serializing `null`
});
}
}
Expand Down
95 changes: 95 additions & 0 deletions spec/lib/build/bundled-source.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -881,5 +881,100 @@ module.exports = require('./bar.js');
expect(Utils.getCache).toHaveBeenCalled();
expect(Utils.setCache).not.toHaveBeenCalled();
});

it('transform saves transformed source map to cache', () => {
let file = {
path: path.resolve(cwd, 'node_modules/foo/bar/lo.js'),
contents: "export {default as t} from './t.js';",
sourceMap: {"version":3,"file":"lo.js","sourceRoot":"","sources":["lo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,CAAC,EAAC,MAAM,QAAQ,CAAA"}
};

Utils.getCache = jasmine.createSpy('getCache').and.returnValue(undefined);
Utils.setCache = jasmine.createSpy('setCache');

let bs = new BundledSource(bundler, file);
bs._getProjectRoot = () => 'src';
bs.includedBy = {
includedBy: {
description: {
name: 'foo',
mainId: 'foo/index',
loaderConfig: {
name: 'foo',
path: '../node_modules/foo',
main: 'index'
},
browserReplacement: () => undefined
}
}
};
bs._getLoaderPlugins = () => [];
bs._getLoaderConfig = () => ({paths: {}});
bs._getUseCache = () => true;

let deps = bs.transform();
let contents = "define('foo/bar/lo',[\"exports\", './t'], function (_exports, _t) { \"use strict\"; _exports.__esModule = true; _exports.t = void 0; _t = _interopRequireDefault(_t); _exports.t = _t.default; function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }});";
let transformedSourceMap = {version:3,file:undefined,names:['_t', '_interopRequireDefault', '_exports', 't', 'default', 'e', '__esModule'],sourceRoot:undefined,sources:['lo.ts'],sourcesContent:[undefined],mappings:";;;;;EAAAA,EAAA,GAAAC,sBAAA,CAAAD,EAAA;EAA2BE,QAAA,CAAAC,CAAA,GAAAH,EAAA,CAAAI,OAAA;EAAA,SAAAH,uBAAAI,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAD,OAAA,EAAAC,CAAA;AAAA",ignoreList:[]};

expect(deps).toEqual(['foo/bar/t']);
expect(bs.requiresTransform).toBe(false);
expect(bs.contents.replace(/\r|\n/g, ''))
.toBe(contents);
expect(bs.sourceMap).toEqual(transformedSourceMap)

expect(Utils.getCache).toHaveBeenCalled();
expect(Utils.setCache).toHaveBeenCalled();
expect(Utils.setCache.calls.argsFor(0)[1].deps).toEqual(['./t']);
expect(Utils.setCache.calls.argsFor(0)[1].contents.replace(/\r|\n/g, '')).toBe(contents);
expect(Utils.setCache.calls.argsFor(0)[1].transformedSourceMap).toEqual(transformedSourceMap);
});

it('transform uses cache for transformed source map', () => {
let file = {
path: path.resolve(cwd, 'node_modules/foo/bar/lo.js'),
contents: "export {default as t} from './t.js';",
sourceMap: {"version":3,"file":"lo.js","sourceRoot":"","sources":["lo.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,OAAO,IAAI,CAAC,EAAC,MAAM,QAAQ,CAAA"}
};

let contents = "define('foo/bar/lo',[\"exports\", './t'], function (_exports, _t) { \"use strict\"; _exports.__esModule = true; _exports.t = void 0; _t = _interopRequireDefault(_t); _exports.t = _t.default; function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }});";
let transformedSourceMap = {version:3,file:undefined,names:['_t', '_interopRequireDefault', '_exports', 't', 'default', 'e', '__esModule'],sourceRoot:undefined,sources:['lo.ts'],sourcesContent:[undefined],mappings:";;;;;EAAAA,EAAA,GAAAC,sBAAA,CAAAD,EAAA;EAA2BE,QAAA,CAAAC,CAAA,GAAAH,EAAA,CAAAI,OAAA;EAAA,SAAAH,uBAAAI,CAAA,WAAAA,CAAA,IAAAA,CAAA,CAAAC,UAAA,GAAAD,CAAA,KAAAD,OAAA,EAAAC,CAAA;AAAA",ignoreList:[]};

Utils.getCache = jasmine.createSpy('getCache').and.returnValue({
deps: ['./t'],
contents,
transformedSourceMap
});
Utils.setCache = jasmine.createSpy('setCache');

let bs = new BundledSource(bundler, file);
bs._getProjectRoot = () => 'src';
bs.includedBy = {
includedBy: {
description: {
name: 'foo',
mainId: 'foo/index',
loaderConfig: {
name: 'foo',
path: '../node_modules/foo',
main: 'index'
},
browserReplacement: () => undefined
}
}
};
bs._getLoaderPlugins = () => [];
bs._getLoaderConfig = () => ({paths: {}});
bs._getUseCache = () => true;

let deps = bs.transform();
expect(deps).toEqual(['foo/bar/t']);
expect(bs.requiresTransform).toBe(false);
expect(bs.contents.replace(/\r|\n/g, ''))
.toBe(contents);
expect(bs.sourceMap).toEqual(transformedSourceMap);

expect(Utils.getCache).toHaveBeenCalled();
expect(Utils.setCache).not.toHaveBeenCalled();
});
});
});
Loading