mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-07 09:27:29 +08:00
Remove baseTransformer
Reviewed By: jeanlauliac Differential Revision: D4506124 fbshipit-source-id: 642f06dffe4ea710113e8e8426915bf1b40d4611
This commit is contained in:
committed by
Facebook Github Bot
parent
57990b7970
commit
a4d7a7835f
@@ -11,6 +11,7 @@
|
||||
'use strict';
|
||||
|
||||
import type {SourceMap} from './output/source-map';
|
||||
import type {Ast} from 'babel-core';
|
||||
import type {Console} from 'console';
|
||||
|
||||
export type Callback<A = void, B = void>
|
||||
@@ -99,18 +100,19 @@ type ResolveOptions = {
|
||||
log?: Console,
|
||||
};
|
||||
|
||||
export type TransformFn = (
|
||||
data: {|
|
||||
filename: string,
|
||||
options?: Object,
|
||||
plugins?: Array<string | Object | [string | Object, any]>,
|
||||
sourceCode: string,
|
||||
|},
|
||||
callback: Callback<TransformFnResult>
|
||||
) => void;
|
||||
export type TransformerResult = {
|
||||
ast: ?Ast,
|
||||
code: string,
|
||||
map: ?SourceMap,
|
||||
};
|
||||
|
||||
export type TransformFnResult = {
|
||||
ast: Object,
|
||||
export type Transformer = {
|
||||
transform: (
|
||||
sourceCode: string,
|
||||
filename: string,
|
||||
options: ?{},
|
||||
plugins?: Array<string | Object | [string | Object, any]>,
|
||||
) => {ast: ?Ast, code: string, map: ?SourceMap}
|
||||
};
|
||||
|
||||
export type TransformResult = {|
|
||||
|
||||
@@ -12,7 +12,7 @@ jest.disableAutomock();
|
||||
|
||||
const optimizeModule = require('../optimize-module');
|
||||
const transformModule = require('../transform-module');
|
||||
const transform = require('../../../../transformer.js');
|
||||
const transformer = require('../../../../transformer.js');
|
||||
const {SourceMapConsumer} = require('source-map');
|
||||
|
||||
const {objectContaining} = jasmine;
|
||||
@@ -32,7 +32,7 @@ describe('optimizing JS modules', () => {
|
||||
|
||||
let transformResult;
|
||||
beforeAll(done => {
|
||||
transformModule(originalCode, {filename, transform}, (error, result) => {
|
||||
transformModule(originalCode, {filename, transformer}, (error, result) => {
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
|
||||
@@ -24,18 +24,20 @@ const {any, objectContaining} = jasmine;
|
||||
describe('transforming JS modules:', () => {
|
||||
const filename = 'arbitrary';
|
||||
|
||||
let transform;
|
||||
let transformer;
|
||||
|
||||
beforeEach(() => {
|
||||
transform = fn();
|
||||
transform.stub.yields(null, transformResult());
|
||||
transformer = {
|
||||
transform: fn(),
|
||||
};
|
||||
transformer.transform.stub.returns(transformResult());
|
||||
});
|
||||
|
||||
const {bodyAst, sourceCode, transformedCode} = createTestData();
|
||||
|
||||
const options = variants => ({
|
||||
filename,
|
||||
transform,
|
||||
transformer,
|
||||
variants,
|
||||
});
|
||||
|
||||
@@ -80,17 +82,17 @@ describe('transforming JS modules:', () => {
|
||||
const variants = {dev: {dev: true}, prod: {dev: false}};
|
||||
|
||||
transformModule(sourceCode, options(variants), () => {
|
||||
expect(transform)
|
||||
.toBeCalledWith({filename, sourceCode, options: variants.dev}, any(Function));
|
||||
expect(transform)
|
||||
.toBeCalledWith({filename, sourceCode, options: variants.prod}, any(Function));
|
||||
expect(transformer.transform)
|
||||
.toBeCalledWith(sourceCode, filename, variants.dev);
|
||||
expect(transformer.transform)
|
||||
.toBeCalledWith(sourceCode, filename, variants.prod);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('calls back with any error yielded by the transform function', done => {
|
||||
const error = new Error();
|
||||
transform.stub.yields(error);
|
||||
transformer.transform.stub.throws(error);
|
||||
|
||||
transformModule(sourceCode, options(), e => {
|
||||
expect(e).toBe(error);
|
||||
@@ -138,7 +140,7 @@ describe('transforming JS modules:', () => {
|
||||
const dep1 = 'foo', dep2 = 'bar';
|
||||
const code = `require('${dep1}'),require('${dep2}')`;
|
||||
const {body} = parse(code).program;
|
||||
transform.stub.yields(null, transformResult(body));
|
||||
transformer.transform.stub.returns(transformResult(body));
|
||||
|
||||
transformModule(code, options(), (error, result) => {
|
||||
expect(result.transformed.default)
|
||||
@@ -149,11 +151,11 @@ describe('transforming JS modules:', () => {
|
||||
|
||||
it('transforms for all variants', done => {
|
||||
const variants = {dev: {dev: true}, prod: {dev: false}};
|
||||
transform.stub
|
||||
transformer.transform.stub
|
||||
.withArgs(filename, sourceCode, variants.dev)
|
||||
.yields(null, transformResult(bodyAst))
|
||||
.returns(transformResult(bodyAst))
|
||||
.withArgs(filename, sourceCode, variants.prod)
|
||||
.yields(null, transformResult([]));
|
||||
.returns(transformResult([]));
|
||||
|
||||
transformModule(sourceCode, options(variants), (error, result) => {
|
||||
const {dev, prod} = result.transformed;
|
||||
|
||||
@@ -21,8 +21,8 @@ const {basename} = require('path');
|
||||
import type {
|
||||
Callback,
|
||||
TransformedFile,
|
||||
TransformFn,
|
||||
TransformFnResult,
|
||||
Transformer,
|
||||
TransformerResult,
|
||||
TransformResult,
|
||||
TransformVariants,
|
||||
} from '../types.flow';
|
||||
@@ -30,7 +30,7 @@ import type {
|
||||
export type TransformOptions = {|
|
||||
filename: string,
|
||||
polyfill?: boolean,
|
||||
transform: TransformFn,
|
||||
transformer: Transformer,
|
||||
variants?: TransformVariants,
|
||||
|};
|
||||
|
||||
@@ -47,17 +47,23 @@ function transformModule(
|
||||
return transformJSON(code, options, callback);
|
||||
}
|
||||
|
||||
const {filename, transform, variants = defaultVariants} = options;
|
||||
const {filename, transformer, variants = defaultVariants} = options;
|
||||
const tasks = {};
|
||||
Object.keys(variants).forEach(name => {
|
||||
tasks[name] = cb => transform({
|
||||
filename,
|
||||
sourceCode: code,
|
||||
options: variants[name],
|
||||
}, cb);
|
||||
tasks[name] = cb => {
|
||||
try {
|
||||
cb(null, transformer.transform(
|
||||
code,
|
||||
filename,
|
||||
variants[name],
|
||||
));
|
||||
} catch (error) {
|
||||
cb(error, null);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
series(tasks, (error, results: {[key: string]: TransformFnResult}) => {
|
||||
series(tasks, (error, results: {[key: string]: TransformerResult}) => {
|
||||
if (error) {
|
||||
callback(error);
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user