mirror of https://github.com/status-im/metro.git
install `node_modules` into `buck-out`
Reviewed By: cpojer Differential Revision: D4312220 fbshipit-source-id: 1449a194c557071bb5f0be6bc00dccef26c62b47
This commit is contained in:
parent
4843500e9e
commit
5da19878c4
|
@ -14,8 +14,8 @@ import type {SourceMap} from './output/source-map';
|
||||||
import type {Console} from 'console';
|
import type {Console} from 'console';
|
||||||
|
|
||||||
export type Callback<A = void, B = void>
|
export type Callback<A = void, B = void>
|
||||||
= (Error => mixed)
|
= (Error => void)
|
||||||
& ((null | void, A, B) => mixed);
|
& ((null | void, A, B) => void);
|
||||||
|
|
||||||
type Dependency = {|
|
type Dependency = {|
|
||||||
id: string,
|
id: string,
|
||||||
|
@ -109,7 +109,6 @@ export type TransformFn = (
|
||||||
callback: Callback<TransformFnResult>
|
callback: Callback<TransformFnResult>
|
||||||
) => void;
|
) => void;
|
||||||
|
|
||||||
|
|
||||||
export type TransformFnResult = {
|
export type TransformFnResult = {
|
||||||
ast: Object,
|
ast: Object,
|
||||||
};
|
};
|
||||||
|
@ -121,6 +120,8 @@ export type TransformResult = {|
|
||||||
map: ?Object,
|
map: ?Object,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
|
export type TransformVariants = {[key: string]: Object};
|
||||||
|
|
||||||
export type TransformedFile = {
|
export type TransformedFile = {
|
||||||
code: string,
|
code: string,
|
||||||
file: string,
|
file: string,
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const asyncify = require('async/asyncify');
|
||||||
const optimizeModule = require('./worker/optimize-module');
|
const optimizeModule = require('./worker/optimize-module');
|
||||||
const transformModule = require('./worker/transform-module');
|
const transformModule = require('./worker/transform-module');
|
||||||
const wrapWorkerFn = require('./worker/wrap-worker-fn');
|
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 {TransformOptions} from './worker/transform-module';
|
||||||
import type {WorkerFnWithIO} from './worker/wrap-worker-fn';
|
import type {WorkerFnWithIO} from './worker/wrap-worker-fn';
|
||||||
|
|
||||||
|
|
||||||
exports.optimizeModule =
|
exports.optimizeModule =
|
||||||
(wrapWorkerFn(optimizeModule): WorkerFnWithIO<OptimizationOptions>);
|
(wrapWorkerFn(asyncify(optimizeModule)): WorkerFnWithIO<OptimizationOptions>);
|
||||||
exports.transformModule =
|
exports.transformModule =
|
||||||
(wrapWorkerFn(transformModule): WorkerFnWithIO<TransformOptions>);
|
(wrapWorkerFn(transformModule): WorkerFnWithIO<TransformOptions>);
|
||||||
|
|
|
@ -41,24 +41,20 @@ describe('optimizing JS modules', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('copies everything from the transformed file, except for transform results', done => {
|
it('copies everything from the transformed file, except for transform results', () => {
|
||||||
optimizeModule(transformResult, optimizationOptions, (error, result) => {
|
const result = optimizeModule(transformResult, optimizationOptions);
|
||||||
const expected = JSON.parse(transformResult);
|
const expected = JSON.parse(transformResult);
|
||||||
delete expected.transformed;
|
delete expected.transformed;
|
||||||
expect(result).toEqual(objectContaining(expected));
|
expect(result).toEqual(objectContaining(expected));
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('code optimization', () => {
|
describe('code optimization', () => {
|
||||||
let dependencyMapName, injectedVars, optimized, requireName;
|
let dependencyMapName, injectedVars, optimized, requireName;
|
||||||
beforeAll(done => {
|
beforeAll(() => {
|
||||||
optimizeModule(transformResult, optimizationOptions, (error, result) => {
|
const result = optimizeModule(transformResult, optimizationOptions);
|
||||||
optimized = result.transformed.default;
|
optimized = result.transformed.default;
|
||||||
injectedVars = optimized.code.match(/function\(([^)]*)/)[1].split(',');
|
injectedVars = optimized.code.match(/function\(([^)]*)/)[1].split(',');
|
||||||
[requireName,,,, dependencyMapName] = injectedVars;
|
[requireName,,,, dependencyMapName] = injectedVars;
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('optimizes code', () => {
|
it('optimizes code', () => {
|
||||||
|
@ -79,15 +75,12 @@ describe('optimizing JS modules', () => {
|
||||||
.toEqual(objectContaining(loc));
|
.toEqual(objectContaining(loc));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('does not extract dependencies for polyfills', done => {
|
it('does not extract dependencies for polyfills', () => {
|
||||||
optimizeModule(
|
const result = optimizeModule(
|
||||||
transformResult,
|
transformResult,
|
||||||
{...optimizationOptions, isPolyfill: true},
|
{...optimizationOptions, isPolyfill: true},
|
||||||
(error, result) => {
|
|
||||||
expect(result.transformed.default.dependencies).toEqual([]);
|
|
||||||
done();
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
|
expect(result.transformed.default.dependencies).toEqual([]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -18,7 +18,7 @@ const inline = require('../../JSTransformer/worker/inline').plugin;
|
||||||
const minify = require('../../JSTransformer/worker/minify');
|
const minify = require('../../JSTransformer/worker/minify');
|
||||||
const sourceMap = require('source-map');
|
const sourceMap = require('source-map');
|
||||||
|
|
||||||
import type {Callback, TransformedFile, TransformResult} from '../types.flow';
|
import type {TransformedFile, TransformResult} from '../types.flow';
|
||||||
|
|
||||||
export type OptimizationOptions = {|
|
export type OptimizationOptions = {|
|
||||||
dev: boolean,
|
dev: boolean,
|
||||||
|
@ -27,11 +27,12 @@ export type OptimizationOptions = {|
|
||||||
|};
|
|};
|
||||||
|
|
||||||
function optimizeModule(
|
function optimizeModule(
|
||||||
json: string,
|
data: string | TransformedFile,
|
||||||
optimizationOptions: OptimizationOptions,
|
optimizationOptions: OptimizationOptions,
|
||||||
callback: Callback<TransformedFile>,
|
): TransformedFile {
|
||||||
): void {
|
if (typeof data === 'string') {
|
||||||
const data: TransformedFile = JSON.parse(json);
|
data = JSON.parse(data);
|
||||||
|
}
|
||||||
const {code, file, transformed} = data;
|
const {code, file, transformed} = data;
|
||||||
const result = {...data, transformed: {}};
|
const result = {...data, transformed: {}};
|
||||||
|
|
||||||
|
@ -39,7 +40,8 @@ function optimizeModule(
|
||||||
Object.entries(transformed).forEach(([k, t: TransformResult]: [*, TransformResult]) => {
|
Object.entries(transformed).forEach(([k, t: TransformResult]: [*, TransformResult]) => {
|
||||||
result.transformed[k] = optimize(t, file, code, optimizationOptions);
|
result.transformed[k] = optimize(t, file, code, optimizationOptions);
|
||||||
});
|
});
|
||||||
callback(null, result);
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function optimize(transformed, file, originalCode, options): TransformResult {
|
function optimize(transformed, file, originalCode, options): TransformResult {
|
||||||
|
|
|
@ -24,9 +24,9 @@ import type {
|
||||||
TransformFn,
|
TransformFn,
|
||||||
TransformFnResult,
|
TransformFnResult,
|
||||||
TransformResult,
|
TransformResult,
|
||||||
|
TransformVariants,
|
||||||
} from '../types.flow';
|
} from '../types.flow';
|
||||||
|
|
||||||
type TransformVariants = {[key: string]: Object};
|
|
||||||
export type TransformOptions = {|
|
export type TransformOptions = {|
|
||||||
filename: string,
|
filename: string,
|
||||||
polyfill?: boolean,
|
polyfill?: boolean,
|
||||||
|
|
Loading…
Reference in New Issue