Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster Source Maps #100

Merged
merged 1 commit into from
Aug 16, 2020
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
4 changes: 2 additions & 2 deletions lib/impl/PluginLifecycle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let fs = require('fs');
let path = require('path');
let { resolvePath, combineSourceMapChain } = require('./utils');
let { resolvePath, combineSourceMapChainFast } = require('./utils');
let ErrorHandling = require('./ErrorHandling');

async function _callAsyncHook (self, plugin, hook, args) {
Expand Down Expand Up @@ -208,7 +208,7 @@ module.exports = {
return { code: result.code, filePath: filePath, map: result.map };
}, { code, filePath, map: null });

map = combineSourceMapChain(mapChain, originalCode, filePath);
map = await combineSourceMapChainFast(mapChain, originalCode, filePath);

for (let i = 0; i < context.pluginsContext.length; i++) {
delete context.pluginsContext[i].__mapChain;
Expand Down
51 changes: 38 additions & 13 deletions lib/impl/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
let path = require('path');
let SourceMap = require('source-map');
let SourceMapFast = require('source-map-fast');

function resolvePath (target, current) {
if (path.isAbsolute(target)) {
Expand Down Expand Up @@ -43,9 +44,7 @@ function getNameFromFileName (file) {
return path.basename(file.replace(/\0/g, '_')).replace(path.extname(file), '')
}

function combineSourceMapChain (mapChain, original_code, filepath) {
// TODO: Proper handling of null maps.
let map;
function prepareSourceMapChain (mapChain, original_code, filepath) {
mapChain = mapChain.filter(o => o.map && o.map.mappings).reverse();

if (mapChain.length > 1) {
Expand All @@ -56,32 +55,57 @@ function combineSourceMapChain (mapChain, original_code, filepath) {
obj.map.sources = obj.map.sources.length === 1? [filepath + '_' + (index + 1)] : obj.map.sources;
obj.map.sourcesContent = obj.map.sourcesContent.length === 1? [mapChain[index + 1]? mapChain[index + 1].code : original_code] : obj.map.sourcesContent;
});
}

let mapGenerator = SourceMap.SourceMapGenerator.fromSourceMap(new SourceMap.SourceMapConsumer(mapChain[0].map));
return mapChain;
}

for (let i = 1; i < mapChain.length; i++) {
mapGenerator.applySourceMap(new SourceMap.SourceMapConsumer(mapChain[i].map))
}
function generateSourceMap (mapChain, mapGenerator, original_code, filepath) {
let map;

if (mapChain.length > 1) {
map = mapGenerator.toJSON();

// Remove irrelevant maps.
map.sources = map.sources && map.sources.map((s, i) => i !== map.sources.length - 1? '' : s);
map.sourcesContent = map.sourcesContent && map.sourcesContent.map((s, i) => i !== map.sourcesContent.length - 1? '' : s);
} else {
map = mapChain.length > 0? mapChain[0].map : undefined;
}
}

if (map) {
map.file = filepath;
map.sources = [filepath];
map.sourcesContent = [original_code];
}


return map;
}

function combineSourceMapChain (inputMapChain, original_code, filepath) {
let mapGenerator, mapChain = prepareSourceMapChain(inputMapChain, original_code, filepath);

if (mapChain.length > 1) {
mapGenerator = SourceMap.SourceMapGenerator.fromSourceMap(new SourceMap.SourceMapConsumer(mapChain[0].map));

for (let i = 1; i < mapChain.length; i++) {
mapGenerator.applySourceMap(new SourceMap.SourceMapConsumer(mapChain[i].map))
}
}

return generateSourceMap(mapChain, mapGenerator, original_code, filepath);
}

async function combineSourceMapChainFast (inputMapChain, original_code, filepath) {
let mapGenerator, mapChain = prepareSourceMapChain(inputMapChain, original_code, filepath);

if (mapChain.length > 1) {
mapGenerator = SourceMapFast.SourceMapGenerator.fromSourceMap(await new SourceMapFast.SourceMapConsumer(mapChain[0].map));

for (let i = 1; i < mapChain.length; i++) {
mapGenerator.applySourceMap(await new SourceMapFast.SourceMapConsumer(mapChain[i].map))
}
}

return generateSourceMap(mapChain, mapGenerator, original_code, filepath);
}

function applyOutputFileNames (context, bundle) {
let name_map = {};

Expand Down Expand Up @@ -136,6 +160,7 @@ module.exports = {
formatFileName,
getNameFromFileName,
combineSourceMapChain,
combineSourceMapChainFast,
applyOutputFileNames,
emitAssetToBundle
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"express-ws": "^4.0.0",
"magic-string": "^0.25.7",
"mime-types": "^2.1.24",
"source-map": "^0.5.6"
"source-map": "^0.5.6",
"source-map-fast": "npm:source-map@0.7.3"
},
"devDependencies": {
"chai": "^4.1.2",
Expand Down
12 changes: 8 additions & 4 deletions test/nollup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ let proxyquire = require('proxyquire');
let path = require('path');
let fs_impl = require('fs');
let expect = require('chai').expect;
let SourceMapFast = require('source-map-fast');

// Source map lib has a check for browser
window.fetch = undefined;

let fs = {
'@global': true,
Expand Down Expand Up @@ -32,19 +36,19 @@ let fs = {

readFile: function (file, encoding, callback) {
try {
let output = this.readFileSync(file);
callback(null, output);
let output = this.readFileSync(file, encoding);
callback(null, output);
} catch (e) {
callback(e);
}
},

readFileSync: function (file) {
readFileSync: function (file, encoding) {
if (this._stubs[file]) {
return this._stubs[file]();
}

return fs_impl.readFileSync(file, 'utf8');
return fs_impl.readFileSync(file, encoding);
},

reset: function () {
Expand Down