diff --git a/packages/metro-bundler/src/Bundler/Bundle.js b/packages/metro-bundler/src/Bundler/Bundle.js index e9a58550..a53d2b18 100644 --- a/packages/metro-bundler/src/Bundler/Bundle.js +++ b/packages/metro-bundler/src/Bundler/Bundle.js @@ -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, - postProcessBundleSourcemap?: PostProcessBundleSourcemap, + postProcessBundleSourcemap: PostProcessBundleSourcemap, } = {}) { super(); this._sourceMap = null; diff --git a/packages/metro-bundler/src/Bundler/index.js b/packages/metro-bundler/src/Bundler/index.js index efd0baaa..612e973f 100644 --- a/packages/metro-bundler/src/Bundler/index.js +++ b/packages/metro-bundler/src/Bundler/index.js @@ -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, +polyfillModuleNames: Array, +postMinifyProcess: PostMinifyProcess, - +postProcessBundleSourcemap?: PostProcessBundleSourcemap, + +postProcessBundleSourcemap: PostProcessBundleSourcemap, +postProcessModules?: PostProcessModules, +projectRoots: $ReadOnlyArray, +providesModuleNodeModules?: Array, diff --git a/packages/metro-bundler/src/shared/output/bundle.js b/packages/metro-bundler/src/shared/output/bundle.js index 81ac4c1b..ed72c1a8 100644 --- a/packages/metro-bundler/src/shared/output/bundle.js +++ b/packages/metro-bundler/src/shared/output/bundle.js @@ -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 { diff --git a/packages/metro-bundler/src/shared/output/meta.js b/packages/metro-bundler/src/shared/output/meta.js index 4c110ada..b636b423 100644 --- a/packages/metro-bundler/src/shared/output/meta.js +++ b/packages/metro-bundler/src/shared/output/meta.js @@ -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); +}