mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Stronger typing for transformers
Reviewed By: jeanlauliac Differential Revision: D5006679 fbshipit-source-id: 795c60db363fb53bc74697e4befe50995e9b97a7
This commit is contained in:
committed by
Facebook Github Bot
parent
3dfed2e865
commit
73fc439bc0
@@ -13,6 +13,7 @@
|
||||
import type {MappingsMap, SourceMap} from '../lib/SourceMap';
|
||||
import type {Ast} from 'babel-core';
|
||||
import type {Console} from 'console';
|
||||
export type {Transformer} from '../JSTransformer/worker/worker.js';
|
||||
|
||||
export type Callback<A = void, B = void>
|
||||
= (Error => void)
|
||||
@@ -105,15 +106,6 @@ export type TransformerResult = {|
|
||||
map: ?MappingsMap,
|
||||
|};
|
||||
|
||||
export type Transformer = {
|
||||
transform: (
|
||||
sourceCode: string,
|
||||
filename: string,
|
||||
options: ?{},
|
||||
plugins?: Array<string | Object | [string | Object, any]>,
|
||||
) => {ast: ?Ast, code: string, map: ?MappingsMap}
|
||||
};
|
||||
|
||||
export type TransformResult = {|
|
||||
code: string,
|
||||
dependencies: Array<string>,
|
||||
|
||||
@@ -80,6 +80,15 @@ describe('transforming JS modules:', () => {
|
||||
});
|
||||
});
|
||||
|
||||
const defaults = {
|
||||
dev: false,
|
||||
generateSourceMaps: true,
|
||||
hot: false,
|
||||
inlineRequires: false,
|
||||
platform: '',
|
||||
projectRoot: '',
|
||||
};
|
||||
|
||||
it('calls the passed-in transform function with code, file name, and options ' +
|
||||
'for all passed in variants',
|
||||
done => {
|
||||
@@ -87,9 +96,9 @@ describe('transforming JS modules:', () => {
|
||||
|
||||
transformModule(sourceCode, options(variants), () => {
|
||||
expect(transformer.transform)
|
||||
.toBeCalledWith(sourceCode, filename, variants.dev);
|
||||
.toBeCalledWith(sourceCode, filename, {...defaults, ...variants.dev});
|
||||
expect(transformer.transform)
|
||||
.toBeCalledWith(sourceCode, filename, variants.prod);
|
||||
.toBeCalledWith(sourceCode, filename, {...defaults, ...variants.prod});
|
||||
done();
|
||||
});
|
||||
},
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
const JsFileWrapping = require('./JsFileWrapping');
|
||||
|
||||
const asyncify = require('async/asyncify');
|
||||
const collectDependencies = require('./collect-dependencies');
|
||||
const defaults = require('../../../defaults');
|
||||
const docblock = require('../../node-haste/DependencyGraph/docblock');
|
||||
@@ -34,10 +35,18 @@ import type {
|
||||
export type TransformOptions = {|
|
||||
filename: string,
|
||||
polyfill?: boolean,
|
||||
transformer: Transformer,
|
||||
transformer: Transformer<*>,
|
||||
variants?: TransformVariants,
|
||||
|};
|
||||
|
||||
const defaultTransformOptions = {
|
||||
dev: true,
|
||||
generateSourceMaps: true,
|
||||
hot: false,
|
||||
inlineRequires: false,
|
||||
platform: '',
|
||||
projectRoot: '',
|
||||
};
|
||||
const defaultVariants = {default: {}};
|
||||
|
||||
const ASSET_EXTENSIONS = new Set(defaults.assetExts);
|
||||
@@ -61,17 +70,12 @@ function transformModule(
|
||||
const {filename, transformer, variants = defaultVariants} = options;
|
||||
const tasks = {};
|
||||
Object.keys(variants).forEach(name => {
|
||||
tasks[name] = cb => {
|
||||
try {
|
||||
cb(null, transformer.transform(
|
||||
code,
|
||||
filename,
|
||||
variants[name],
|
||||
));
|
||||
} catch (error) {
|
||||
cb(error, null);
|
||||
}
|
||||
};
|
||||
tasks[name] = asyncify(() => transformer.transform(
|
||||
code,
|
||||
filename,
|
||||
{...defaultTransformOptions, ...variants[name]},
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
series(tasks, (error, results: {[key: string]: TransformerResult}) => {
|
||||
|
||||
Reference in New Issue
Block a user