Permalink
Browse files

Fix release / minified bundle builds

Summary: Fixes building release builds with the new buck integration.

Reviewed By: jeanlauliac

Differential Revision: D5002441

fbshipit-source-id: e7716324c7c8dd0bfcaf5b39cf716ac589e948e8
  • Loading branch information...
davidaurelio authored and facebook-github-bot committed May 4, 2017
1 parent 11eef69 commit 1a0e78c4e40866bdca3f6c32b5fc5a4df90d28e2
@@ -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<T>(...iterables: Array<Iterable<T>>): Iterable<T> {
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();'
);
}
@@ -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();
});
});
@@ -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) => C) => (A, B, Callback<C>) => void;
exports.optimizeModule =
(wrapWorkerFn(asyncify(optimizeModule)): WorkerFnWithIO<OptimizationOptions>);
exports.transformModule =
@@ -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) {
@@ -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 {

0 comments on commit 1a0e78c

Please sign in to comment.