install `node_modules` into `buck-out`

Reviewed By: cpojer

Differential Revision: D4312220

fbshipit-source-id: 1449a194c557071bb5f0be6bc00dccef26c62b47
This commit is contained in:
David Aurelio 2016-12-13 08:59:45 -08:00 committed by Facebook Github Bot
parent 4843500e9e
commit 5da19878c4
5 changed files with 28 additions and 32 deletions

View File

@ -14,8 +14,8 @@ import type {SourceMap} from './output/source-map';
import type {Console} from 'console';
export type Callback<A = void, B = void>
= (Error => mixed)
& ((null | void, A, B) => mixed);
= (Error => void)
& ((null | void, A, B) => void);
type Dependency = {|
id: string,
@ -109,7 +109,6 @@ export type TransformFn = (
callback: Callback<TransformFnResult>
) => void;
export type TransformFnResult = {
ast: Object,
};
@ -121,6 +120,8 @@ export type TransformResult = {|
map: ?Object,
|};
export type TransformVariants = {[key: string]: Object};
export type TransformedFile = {
code: string,
file: string,

View File

@ -10,6 +10,7 @@
*/
'use strict';
const asyncify = require('async/asyncify');
const optimizeModule = require('./worker/optimize-module');
const transformModule = require('./worker/transform-module');
const wrapWorkerFn = require('./worker/wrap-worker-fn');
@ -18,8 +19,7 @@ import type {OptimizationOptions} from './worker/optimize-module';
import type {TransformOptions} from './worker/transform-module';
import type {WorkerFnWithIO} from './worker/wrap-worker-fn';
exports.optimizeModule =
(wrapWorkerFn(optimizeModule): WorkerFnWithIO<OptimizationOptions>);
(wrapWorkerFn(asyncify(optimizeModule)): WorkerFnWithIO<OptimizationOptions>);
exports.transformModule =
(wrapWorkerFn(transformModule): WorkerFnWithIO<TransformOptions>);

View File

@ -41,24 +41,20 @@ describe('optimizing JS modules', () => {
});
});
it('copies everything from the transformed file, except for transform results', done => {
optimizeModule(transformResult, optimizationOptions, (error, result) => {
const expected = JSON.parse(transformResult);
delete expected.transformed;
expect(result).toEqual(objectContaining(expected));
done();
});
it('copies everything from the transformed file, except for transform results', () => {
const result = optimizeModule(transformResult, optimizationOptions);
const expected = JSON.parse(transformResult);
delete expected.transformed;
expect(result).toEqual(objectContaining(expected));
});
describe('code optimization', () => {
let dependencyMapName, injectedVars, optimized, requireName;
beforeAll(done => {
optimizeModule(transformResult, optimizationOptions, (error, result) => {
optimized = result.transformed.default;
injectedVars = optimized.code.match(/function\(([^)]*)/)[1].split(',');
[requireName,,,, dependencyMapName] = injectedVars;
done();
});
beforeAll(() => {
const result = optimizeModule(transformResult, optimizationOptions);
optimized = result.transformed.default;
injectedVars = optimized.code.match(/function\(([^)]*)/)[1].split(',');
[requireName,,,, dependencyMapName] = injectedVars;
});
it('optimizes code', () => {
@ -79,15 +75,12 @@ describe('optimizing JS modules', () => {
.toEqual(objectContaining(loc));
});
it('does not extract dependencies for polyfills', done => {
optimizeModule(
it('does not extract dependencies for polyfills', () => {
const result = optimizeModule(
transformResult,
{...optimizationOptions, isPolyfill: true},
(error, result) => {
expect(result.transformed.default.dependencies).toEqual([]);
done();
},
);
expect(result.transformed.default.dependencies).toEqual([]);
});
});
});

View File

@ -18,7 +18,7 @@ const inline = require('../../JSTransformer/worker/inline').plugin;
const minify = require('../../JSTransformer/worker/minify');
const sourceMap = require('source-map');
import type {Callback, TransformedFile, TransformResult} from '../types.flow';
import type {TransformedFile, TransformResult} from '../types.flow';
export type OptimizationOptions = {|
dev: boolean,
@ -27,11 +27,12 @@ export type OptimizationOptions = {|
|};
function optimizeModule(
json: string,
data: string | TransformedFile,
optimizationOptions: OptimizationOptions,
callback: Callback<TransformedFile>,
): void {
const data: TransformedFile = JSON.parse(json);
): TransformedFile {
if (typeof data === 'string') {
data = JSON.parse(data);
}
const {code, file, transformed} = data;
const result = {...data, transformed: {}};
@ -39,7 +40,8 @@ function optimizeModule(
Object.entries(transformed).forEach(([k, t: TransformResult]: [*, TransformResult]) => {
result.transformed[k] = optimize(t, file, code, optimizationOptions);
});
callback(null, result);
return result;
}
function optimize(transformed, file, originalCode, options): TransformResult {

View File

@ -24,9 +24,9 @@ import type {
TransformFn,
TransformFnResult,
TransformResult,
TransformVariants,
} from '../types.flow';
type TransformVariants = {[key: string]: Object};
export type TransformOptions = {|
filename: string,
polyfill?: boolean,