mirror of https://github.com/status-im/metro.git
Unify source map approach for RA bundles on iOS/Android
Reviewed By: javache Differential Revision: D3229780 fb-gh-sync-id: a3148d7626b32a2e6803ae8c35ac75025a992a32 fbshipit-source-id: a3148d7626b32a2e6803ae8c35ac75025a992a32
This commit is contained in:
parent
cfdd04f2d2
commit
2a77da76ae
|
@ -107,59 +107,21 @@ class Bundle extends BundleBase {
|
|||
}
|
||||
|
||||
getUnbundle(type) {
|
||||
if (this._ramBundle) {
|
||||
return this._ramBundle;
|
||||
}
|
||||
|
||||
switch (type) {
|
||||
case 'INDEX':
|
||||
this._ramBundle = this._getAsIndexedFileUnbundle();
|
||||
break;
|
||||
case 'ASSETS':
|
||||
this._ramBundle = this._getAsAssetsUnbundle();
|
||||
break;
|
||||
default:
|
||||
throw new Error('Unkown RAM Bundle type:', type);
|
||||
}
|
||||
|
||||
return this._ramBundle;
|
||||
}
|
||||
|
||||
_getAsIndexedFileUnbundle() {
|
||||
const modules = this.getModules();
|
||||
this.assertFinalized();
|
||||
if (!this._ramBundle) {
|
||||
const modules = this.getModules().slice();
|
||||
|
||||
// separate modules we need to preload from the ones we don't
|
||||
const shouldPreload = (module) => module.meta && module.meta.preloaded;
|
||||
const preloaded = modules.filter(module => shouldPreload(module));
|
||||
const notPreloaded = modules.filter(module => !shouldPreload(module));
|
||||
const [startupModules, lazyModules] = partition(modules, shouldPreload);
|
||||
|
||||
// code that will be executed on bridge start up
|
||||
const startupCode = preloaded.map(({code}) => code).join('\n');
|
||||
|
||||
return {
|
||||
// include copy of all modules on the order they're writen on the bundle:
|
||||
// polyfills, preloaded, additional requires, non preloaded
|
||||
allModules: preloaded.concat(notPreloaded),
|
||||
startupCode, // no entries on the index for these modules, only the code
|
||||
modules: notPreloaded, // we include both the code and entries on the index
|
||||
this._ramBundle = {
|
||||
startupModules,
|
||||
lazyModules,
|
||||
allModules: modules,
|
||||
};
|
||||
}
|
||||
|
||||
_getAsAssetsUnbundle() {
|
||||
const allModules = this.getModules().slice();
|
||||
const prependedModules = this._numPrependedModules;
|
||||
const requireCalls = this._numRequireCalls;
|
||||
|
||||
const modules =
|
||||
allModules
|
||||
.splice(prependedModules, allModules.length - requireCalls - prependedModules);
|
||||
const startupCode = allModules.map(({code}) => code).join('\n');
|
||||
|
||||
return {
|
||||
startupCode,
|
||||
startupModules: allModules,
|
||||
modules,
|
||||
};
|
||||
return this._ramBundle;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -351,4 +313,15 @@ function generateSourceMapForVirtualModule(module) {
|
|||
};
|
||||
}
|
||||
|
||||
function shouldPreload({meta}) {
|
||||
return meta && meta.preloaded;
|
||||
}
|
||||
|
||||
function partition(array, predicate) {
|
||||
const included = [];
|
||||
const excluded = [];
|
||||
array.forEach(item => (predicate(item) ? included : excluded).push(item));
|
||||
return [included, excluded];
|
||||
}
|
||||
|
||||
module.exports = Bundle;
|
||||
|
|
|
@ -556,12 +556,11 @@ class Bundler {
|
|||
]).then((
|
||||
[name, {code, dependencies, dependencyOffsets, map, source}]
|
||||
) => {
|
||||
const {preloadedModules} = transformOptions.transform;
|
||||
const preloaded =
|
||||
module.path === entryFilePath ||
|
||||
module.isPolyfill() || (
|
||||
transformOptions.transform.preloadedModules &&
|
||||
transformOptions.transform.preloadedModules.hasOwnProperty(module.path)
|
||||
);
|
||||
module.isPolyfill() ||
|
||||
preloadedModules && preloadedModules.hasOwnProperty(module.path);
|
||||
|
||||
return new ModuleTransport({
|
||||
name,
|
||||
|
@ -571,7 +570,7 @@ class Bundler {
|
|||
meta: {dependencies, dependencyOffsets, preloaded},
|
||||
sourceCode: source,
|
||||
sourcePath: module.path
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue