diff --git a/packages/metro-bundler/src/ModuleGraph/ModuleGraph.js b/packages/metro-bundler/src/ModuleGraph/ModuleGraph.js index 9d67a024..cce7ff8a 100644 --- a/packages/metro-bundler/src/ModuleGraph/ModuleGraph.js +++ b/packages/metro-bundler/src/ModuleGraph/ModuleGraph.js @@ -31,8 +31,8 @@ type BuildFn = ( ) => void; type BuildOptions = {| - optimize?: boolean, - platform?: string, + optimize: boolean, + platform: string, |}; exports.createBuildSetup = ( @@ -96,6 +96,7 @@ function* concat(...iterables: Array>): Iterable { function prelude(optimize) { return virtualModule( - `var __DEV__=${String(!optimize)},__BUNDLE_START_TIME__=Date.now();` + `var __DEV__=${String(!optimize)},` + + '__BUNDLE_START_TIME__=global.nativePerformanceNow?global.nativePerformanceNow():Date.now();' ); } diff --git a/packages/metro-bundler/src/ModuleGraph/__tests__/ModuleGraph-test.js b/packages/metro-bundler/src/ModuleGraph/__tests__/ModuleGraph-test.js index 4a3844c9..d9a9108a 100644 --- a/packages/metro-bundler/src/ModuleGraph/__tests__/ModuleGraph-test.js +++ b/packages/metro-bundler/src/ModuleGraph/__tests__/ModuleGraph-test.js @@ -28,7 +28,8 @@ describe('build setup', () => { expect(prelude).toEqual({ dependencies: [], file: { - code: 'var __DEV__=true,__BUNDLE_START_TIME__=Date.now();', + code: 'var __DEV__=true,__BUNDLE_START_TIME__=' + + 'global.nativePerformanceNow?global.nativePerformanceNow():Date.now();', path: '', type: 'script', }, @@ -41,7 +42,8 @@ describe('build setup', () => { buildSetup(noEntryPoints, {optimize: true}, (error, result) => { const [prelude] = result.modules; expect(prelude.file.code) - .toEqual('var __DEV__=false,__BUNDLE_START_TIME__=Date.now();'); + .toEqual('var __DEV__=false,__BUNDLE_START_TIME__=' + + 'global.nativePerformanceNow?global.nativePerformanceNow():Date.now();'); done(); }); }); diff --git a/packages/metro-bundler/src/ModuleGraph/worker.js b/packages/metro-bundler/src/ModuleGraph/worker.js index 96c00f6e..6a1a41f3 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker.js +++ b/packages/metro-bundler/src/ModuleGraph/worker.js @@ -10,15 +10,19 @@ */ 'use strict'; -const asyncify = require('async/asyncify'); +const asyncify: Asyncify = require('async/asyncify'); const optimizeModule = require('./worker/optimize-module'); const transformModule = require('./worker/transform-module'); const wrapWorkerFn = require('./worker/wrap-worker-fn'); +import type {Callback} from './types.flow'; import type {OptimizationOptions} from './worker/optimize-module'; import type {TransformOptions} from './worker/transform-module'; import type {WorkerFnWithIO} from './worker/wrap-worker-fn'; +type Asyncify = ((A, B) => C) => (A, B, Callback) => void; + + exports.optimizeModule = (wrapWorkerFn(asyncify(optimizeModule)): WorkerFnWithIO); exports.transformModule = diff --git a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/optimize-module-test.js b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/optimize-module-test.js index 6dadf689..37e58472 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/__tests__/optimize-module-test.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/__tests__/optimize-module-test.js @@ -36,23 +36,24 @@ describe('optimizing JS modules', () => { if (error) { throw error; } - transformResult = JSON.stringify(result.details); + transformResult = JSON.stringify({type: 'code', details: result.details}); done(); }); }); it('copies everything from the transformed file, except for transform results', () => { const result = optimizeModule(transformResult, optimizationOptions); - const expected = JSON.parse(transformResult); + const expected = JSON.parse(transformResult).details; delete expected.transformed; - expect(result).toEqual(objectContaining(expected)); + expect(result.type).toBe('code'); + expect(result.details).toEqual(objectContaining(expected)); }); describe('code optimization', () => { let dependencyMapName, injectedVars, optimized, requireName; beforeAll(() => { const result = optimizeModule(transformResult, optimizationOptions); - optimized = result.transformed.default; + optimized = result.details.transformed.default; injectedVars = optimized.code.match(/function\(([^)]*)/)[1].split(','); [, requireName,,, dependencyMapName] = injectedVars; }); @@ -79,10 +80,16 @@ describe('optimizing JS modules', () => { const result = optimizeModule( transformResult, {...optimizationOptions, isPolyfill: true}, - ); + ).details; expect(result.transformed.default.dependencies).toEqual([]); }); }); + + it('passes through non-code data unmodified', () => { + const data = {type: 'asset', details: {arbitrary: 'data'}}; + expect(optimizeModule(JSON.stringify(data), {dev: true, platform: ''})) + .toEqual(data); + }); }); function findLast(code, needle) { diff --git a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js index f80aa426..89885163 100644 --- a/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js +++ b/packages/metro-bundler/src/ModuleGraph/worker/optimize-module.js @@ -18,7 +18,7 @@ const inline = require('../../JSTransformer/worker/inline').plugin; const minify = require('../../JSTransformer/worker/minify'); const sourceMap = require('source-map'); -import type {TransformedCodeFile, TransformResult} from '../types.flow'; +import type {TransformedSourceFile, TransformResult} from '../types.flow'; export type OptimizationOptions = {| dev: boolean, @@ -27,21 +27,25 @@ export type OptimizationOptions = {| |}; function optimizeModule( - data: string | TransformedCodeFile, + content: Buffer, optimizationOptions: OptimizationOptions, -): TransformedCodeFile { - if (typeof data === 'string') { - data = JSON.parse(data); +): TransformedSourceFile { + const data: TransformedSourceFile = JSON.parse(content.toString('utf8')); + + if (data.type !== 'code') { + return data; } - const {code, file, transformed} = data; - const result = {...data, transformed: {}}; + + const {details} = data; + const {code, file, transformed} = details; + const result = {...details, transformed: {}}; //$FlowIssue #14545724 Object.entries(transformed).forEach(([k, t: TransformResult]: [*, TransformResult]) => { result.transformed[k] = optimize(t, file, code, optimizationOptions); }); - return result; + return {type: 'code', details: result}; } function optimize(transformed, file, originalCode, options): TransformResult {