mirror of https://github.com/status-im/metro.git
Sourcemaps support for RAM
Summary:This rev adds support for production sourcemaps on RAM. When we inject a module into JSC we use the original `sourceURL` and specify the `startingLineNumber` of the module relative to a "regular" bundle. By doing so, when an error is thrown, JSC will include the provided `sourceURL` as the filename and will use the indicated `startingLineNumber` to figure out on which line the error actually occurred. To make things a bit simpler and avoid having to deal with columns, we tweak the generated bundle so that each module starts on a new line. Since we cannot assure that each module's code will be on a single line as the minifier might break it on multiple (UglifyJS does so due to a bug on old versions of Chrome), we include on the index the line number that should be used when invoking `JSEvaluateScript`. Since the module length was not being used we replaced the placeholder we have there for the line number. Reviewed By: javache Differential Revision: D2997520 fb-gh-sync-id: 3243a489cbb5b48a963f4ccdd98ba63b30f53f3f shipit-source-id: 3243a489cbb5b48a963f4ccdd98ba63b30f53f3f
This commit is contained in:
parent
c46d6cc239
commit
44f7df4432
|
@ -16,9 +16,6 @@ const crypto = require('crypto');
|
||||||
|
|
||||||
const SOURCEMAPPING_URL = '\n\/\/# sourceMappingURL=';
|
const SOURCEMAPPING_URL = '\n\/\/# sourceMappingURL=';
|
||||||
|
|
||||||
const getCode = x => x.code;
|
|
||||||
const getNameAndCode = ({name, code}) => ({name, code});
|
|
||||||
|
|
||||||
class Bundle extends BundleBase {
|
class Bundle extends BundleBase {
|
||||||
constructor({sourceMapUrl, minify} = {}) {
|
constructor({sourceMapUrl, minify} = {}) {
|
||||||
super();
|
super();
|
||||||
|
@ -40,6 +37,7 @@ class Bundle extends BundleBase {
|
||||||
map: moduleTransport.map,
|
map: moduleTransport.map,
|
||||||
meta: moduleTransport.meta,
|
meta: moduleTransport.meta,
|
||||||
minify: this._minify,
|
minify: this._minify,
|
||||||
|
polyfill: module.isPolyfill(),
|
||||||
}).then(({code, map}) => {
|
}).then(({code, map}) => {
|
||||||
// If we get a map from the transformer we'll switch to a mode
|
// If we get a map from the transformer we'll switch to a mode
|
||||||
// were we're combining the source maps as opposed to
|
// were we're combining the source maps as opposed to
|
||||||
|
@ -113,20 +111,21 @@ class Bundle extends BundleBase {
|
||||||
const modules =
|
const modules =
|
||||||
allModules
|
allModules
|
||||||
.splice(prependedModules, allModules.length - requireCalls - prependedModules);
|
.splice(prependedModules, allModules.length - requireCalls - prependedModules);
|
||||||
const startupCode = allModules.map(getCode).join('\n');
|
const startupCode = allModules.map(({code}) => code).join('\n');
|
||||||
|
|
||||||
return {
|
return {
|
||||||
startupCode,
|
startupCode,
|
||||||
modules: modules.map(getNameAndCode)
|
modules: modules.map(({name, code, polyfill}) =>
|
||||||
|
({name, code, polyfill})
|
||||||
|
),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* I found a neat trick in the sourcemap spec that makes it easy
|
* Combine each of the sourcemaps multiple modules have into a single big
|
||||||
* to concat sourcemaps. The `sections` field allows us to combine
|
* one. This works well thanks to a neat trick defined on the sourcemap spec
|
||||||
* the sourcemap easily by adding an offset. Tested on chrome.
|
* that makes use of of the `sections` field to combine sourcemaps by adding
|
||||||
* Seems like it's not yet in Firefox but that should be fine for
|
* an offset. This is supported only by Chrome for now.
|
||||||
* now.
|
|
||||||
*/
|
*/
|
||||||
_getCombinedSourceMaps(options) {
|
_getCombinedSourceMaps(options) {
|
||||||
const result = {
|
const result = {
|
||||||
|
|
|
@ -123,7 +123,7 @@ describe('Bundle', () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
const promise = Promise.all(
|
const promise = Promise.all(
|
||||||
moduleTransports.map(m => bundle.addModule(resolver, null, null, m)))
|
moduleTransports.map(m => bundle.addModule(resolver, null, {isPolyfill: () => false}, m)))
|
||||||
.then(() => {
|
.then(() => {
|
||||||
expect(bundle.getModules())
|
expect(bundle.getModules())
|
||||||
.toEqual(moduleTransports);
|
.toEqual(moduleTransports);
|
||||||
|
@ -375,12 +375,19 @@ function resolverFor(code, map) {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function addModule({bundle, code, sourceCode, sourcePath, map, virtual}) {
|
function addModule({bundle, code, sourceCode, sourcePath, map, virtual, polyfill}) {
|
||||||
return bundle.addModule(
|
return bundle.addModule(
|
||||||
resolverFor(code, map),
|
resolverFor(code, map),
|
||||||
null,
|
null,
|
||||||
null,
|
{isPolyfill: () => polyfill},
|
||||||
createModuleTransport({code, sourceCode, sourcePath, map, virtual})
|
createModuleTransport({
|
||||||
|
code,
|
||||||
|
sourceCode,
|
||||||
|
sourcePath,
|
||||||
|
map,
|
||||||
|
virtual,
|
||||||
|
polyfill,
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -204,6 +204,7 @@ describe('Bundler', function() {
|
||||||
transform: {
|
transform: {
|
||||||
dev: true,
|
dev: true,
|
||||||
hot: false,
|
hot: false,
|
||||||
|
generateSourceMaps: false,
|
||||||
projectRoots,
|
projectRoots,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -230,8 +230,9 @@ class Bundler {
|
||||||
platform,
|
platform,
|
||||||
moduleSystemDeps = [],
|
moduleSystemDeps = [],
|
||||||
hot,
|
hot,
|
||||||
|
unbundle,
|
||||||
entryModuleOnly,
|
entryModuleOnly,
|
||||||
resolutionResponse
|
resolutionResponse,
|
||||||
}) {
|
}) {
|
||||||
const onResolutionResponse = response => {
|
const onResolutionResponse = response => {
|
||||||
bundle.setMainModuleId(response.mainModuleId);
|
bundle.setMainModuleId(response.mainModuleId);
|
||||||
|
@ -265,6 +266,7 @@ class Bundler {
|
||||||
platform,
|
platform,
|
||||||
bundle,
|
bundle,
|
||||||
hot,
|
hot,
|
||||||
|
unbundle,
|
||||||
resolutionResponse,
|
resolutionResponse,
|
||||||
onResolutionResponse,
|
onResolutionResponse,
|
||||||
finalizeBundle,
|
finalizeBundle,
|
||||||
|
@ -316,6 +318,7 @@ class Bundler {
|
||||||
platform,
|
platform,
|
||||||
bundle,
|
bundle,
|
||||||
hot,
|
hot,
|
||||||
|
unbundle,
|
||||||
resolutionResponse,
|
resolutionResponse,
|
||||||
onResolutionResponse = noop,
|
onResolutionResponse = noop,
|
||||||
onModuleTransformed = noop,
|
onModuleTransformed = noop,
|
||||||
|
@ -336,8 +339,15 @@ class Bundler {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
resolutionResponse = this.getDependencies(
|
resolutionResponse = this.getDependencies({
|
||||||
{entryFile, dev, platform, hot, onProgess, minify});
|
entryFile,
|
||||||
|
dev,
|
||||||
|
platform,
|
||||||
|
hot,
|
||||||
|
onProgess,
|
||||||
|
minify,
|
||||||
|
generateSourceMaps: unbundle,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.resolve(resolutionResponse).then(response => {
|
return Promise.resolve(resolutionResponse).then(response => {
|
||||||
|
@ -391,10 +401,18 @@ class Bundler {
|
||||||
minify = !dev,
|
minify = !dev,
|
||||||
hot = false,
|
hot = false,
|
||||||
recursive = true,
|
recursive = true,
|
||||||
|
generateSourceMaps = false,
|
||||||
onProgess,
|
onProgess,
|
||||||
}) {
|
}) {
|
||||||
return this.getTransformOptions(
|
return this.getTransformOptions(
|
||||||
entryFile, {dev, platform, hot, projectRoots: this._projectRoots}
|
entryFile,
|
||||||
|
{
|
||||||
|
dev,
|
||||||
|
platform,
|
||||||
|
hot,
|
||||||
|
generateSourceMaps,
|
||||||
|
projectRoots: this._projectRoots,
|
||||||
|
},
|
||||||
).then(transformSpecificOptions => {
|
).then(transformSpecificOptions => {
|
||||||
const transformOptions = {
|
const transformOptions = {
|
||||||
minify,
|
minify,
|
||||||
|
|
|
@ -261,6 +261,10 @@ class Resolver {
|
||||||
: result;
|
: result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
minifyModule({path, code, map}) {
|
||||||
|
return this._minifyCode(path, code, map);
|
||||||
|
}
|
||||||
|
|
||||||
getDebugInfo() {
|
getDebugInfo() {
|
||||||
return this._depGraph.getDebugInfo();
|
return this._depGraph.getDebugInfo();
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ function ModuleTransport(data) {
|
||||||
|
|
||||||
this.virtual = data.virtual;
|
this.virtual = data.virtual;
|
||||||
this.meta = data.meta;
|
this.meta = data.meta;
|
||||||
|
this.polyfill = data.polyfill;
|
||||||
this.map = data.map;
|
this.map = data.map;
|
||||||
|
|
||||||
Object.freeze(this);
|
Object.freeze(this);
|
||||||
|
|
Loading…
Reference in New Issue