mirror of https://github.com/status-im/metro.git
Restore file header for RAM bundles
Summary: Restores the ability to write the expected header for indexed RAM bundles by avoiding to stringify buffers as utf8. Some minor cleanups included Reviewed By: javache Differential Revision: D5351839 fbshipit-source-id: 056661b064336ff74571b9f44c16d709fc59145b
This commit is contained in:
parent
b96d886982
commit
efa753c2e4
|
@ -49,14 +49,14 @@ class Bundle extends BundleBase {
|
|||
_sourceMap: string | null;
|
||||
_sourceMapFormat: SourceMapFormat;
|
||||
_sourceMapUrl: ?string;
|
||||
postProcessBundleSourcemap: ?PostProcessBundleSourcemap;
|
||||
postProcessBundleSourcemap: PostProcessBundleSourcemap;
|
||||
|
||||
constructor({sourceMapUrl, dev, minify, ramGroups, postProcessBundleSourcemap}: {
|
||||
sourceMapUrl: ?string,
|
||||
dev?: boolean,
|
||||
minify?: boolean,
|
||||
ramGroups?: Array<string>,
|
||||
postProcessBundleSourcemap?: PostProcessBundleSourcemap,
|
||||
postProcessBundleSourcemap: PostProcessBundleSourcemap,
|
||||
} = {}) {
|
||||
super();
|
||||
this._sourceMap = null;
|
||||
|
|
|
@ -41,7 +41,7 @@ const VERSION = require('../../package.json').version;
|
|||
import type AssetServer from '../AssetServer';
|
||||
import type Module, {HasteImpl} from '../node-haste/Module';
|
||||
import type ResolutionResponse from '../node-haste/DependencyGraph/ResolutionResponse';
|
||||
import type {MappingsMap} from '../lib/SourceMap';
|
||||
import type {MappingsMap, SourceMap} from '../lib/SourceMap';
|
||||
import type {Options as JSTransformerOptions} from '../JSTransformer/worker';
|
||||
import type {Reporter} from '../lib/reporting';
|
||||
import type {TransformCache} from '../lib/TransformCaching';
|
||||
|
@ -113,10 +113,10 @@ export type PostMinifyProcess = ({
|
|||
}) => {code: string, map: MappingsMap};
|
||||
|
||||
export type PostProcessBundleSourcemap = ({
|
||||
code: string,
|
||||
map: string,
|
||||
code: Buffer | string,
|
||||
map: SourceMap,
|
||||
outFileName: string,
|
||||
}) => {code: string, map: string};
|
||||
}) => {code: Buffer | string, map: SourceMap | string};
|
||||
|
||||
type Options = {|
|
||||
+allowBundleUpdates: boolean,
|
||||
|
@ -132,7 +132,7 @@ type Options = {|
|
|||
+platforms: Array<string>,
|
||||
+polyfillModuleNames: Array<string>,
|
||||
+postMinifyProcess: PostMinifyProcess,
|
||||
+postProcessBundleSourcemap?: PostProcessBundleSourcemap,
|
||||
+postProcessBundleSourcemap: PostProcessBundleSourcemap,
|
||||
+postProcessModules?: PostProcessModules,
|
||||
+projectRoots: $ReadOnlyArray<string>,
|
||||
+providesModuleNodeModules?: Array<string>,
|
||||
|
|
|
@ -29,14 +29,18 @@ function buildBundle(packagerClient: Server, requestOptions: RequestOptions) {
|
|||
});
|
||||
}
|
||||
|
||||
function createCodeWithMap(bundle: Bundle, dev: boolean, sourceMapSourcesRoot?: string): * {
|
||||
function createCodeWithMap(
|
||||
bundle: Bundle,
|
||||
dev: boolean,
|
||||
sourceMapSourcesRoot?: string,
|
||||
): {code: string, map: SourceMap} {
|
||||
const map = bundle.getSourceMap({dev});
|
||||
const sourceMap = relativizeSourceMap(
|
||||
typeof map === 'string' ? (JSON.parse(map): SourceMap) : map,
|
||||
sourceMapSourcesRoot);
|
||||
return {
|
||||
code: bundle.getSource({dev}),
|
||||
map: JSON.stringify(sourceMap),
|
||||
map: sourceMap,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -55,9 +59,10 @@ function saveBundleAndMap(
|
|||
|
||||
log('start');
|
||||
const origCodeWithMap = createCodeWithMap(bundle, !!dev, sourcemapSourcesRoot);
|
||||
const codeWithMap = bundle.postProcessBundleSourcemap ?
|
||||
bundle.postProcessBundleSourcemap({...origCodeWithMap, outFileName: bundleOutput}) :
|
||||
origCodeWithMap;
|
||||
const codeWithMap = bundle.postProcessBundleSourcemap({
|
||||
...origCodeWithMap,
|
||||
outFileName: bundleOutput,
|
||||
});
|
||||
log('finish');
|
||||
|
||||
log('Writing bundle output to:', bundleOutput);
|
||||
|
@ -73,7 +78,10 @@ function saveBundleAndMap(
|
|||
|
||||
if (sourcemapOutput) {
|
||||
log('Writing sourcemap output to:', sourcemapOutput);
|
||||
const writeMap = writeFile(sourcemapOutput, codeWithMap.map, null);
|
||||
const map = typeof codeWithMap.map !== 'string'
|
||||
? JSON.stringify(codeWithMap.map)
|
||||
: codeWithMap.map;
|
||||
const writeMap = writeFile(sourcemapOutput, map, null);
|
||||
writeMap.then(() => log('Done writing sourcemap output'));
|
||||
return Promise.all([writeBundle, writeMetadata, writeMap]);
|
||||
} else {
|
||||
|
|
|
@ -22,25 +22,33 @@ const constantFor = encoding =>
|
|||
/^(?:utf-?16(?:le)?|ucs-?2)$/.test(encoding) ? 3 : 0;
|
||||
|
||||
module.exports = function(
|
||||
code: string,
|
||||
code: Buffer | string,
|
||||
encoding: 'ascii' | 'utf8' | 'utf16le' = 'utf8',
|
||||
): Buffer {
|
||||
const buffer: Buffer = asBuffer(code, encoding);
|
||||
const hash = crypto.createHash('sha1');
|
||||
// remove `new Buffer` calls when RN drops support for Node 4
|
||||
hash.update(Buffer.from ? Buffer.from(code, encoding) : new Buffer(code, encoding));
|
||||
hash.update(buffer);
|
||||
const digest = hash.digest();
|
||||
const signature = Buffer.alloc ? Buffer.alloc(digest.length + 1) : new Buffer(digest.length + 1);
|
||||
digest.copy(signature);
|
||||
signature.writeUInt8(
|
||||
constantFor(tryAsciiPromotion(code, encoding)),
|
||||
constantFor(tryAsciiPromotion(buffer, encoding)),
|
||||
signature.length - 1);
|
||||
return signature;
|
||||
};
|
||||
|
||||
function tryAsciiPromotion(string, encoding) {
|
||||
function tryAsciiPromotion(buffer, encoding) {
|
||||
if (!isUTF8(encoding)) { return encoding; }
|
||||
for (let i = 0, n = string.length; i < n; i++) {
|
||||
if (string.charCodeAt(i) > 0x7f) { return encoding; }
|
||||
for (let i = 0, n = buffer.length; i < n; i++) {
|
||||
if (buffer[i] > 0x7f) { return encoding; }
|
||||
}
|
||||
return 'ascii';
|
||||
}
|
||||
|
||||
function asBuffer(x, encoding): Buffer {
|
||||
if (typeof x !== 'string') {
|
||||
return x;
|
||||
}
|
||||
// remove `new Buffer` calls when RN drops support for Node 4
|
||||
return Buffer.from ? Buffer.from(x, encoding) : new Buffer(x, encoding);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue