packager: buck worker: better types

Reviewed By: davidaurelio

Differential Revision: D4970025

fbshipit-source-id: 77db309befebe539d25bd8df1039e2304176ca3f
This commit is contained in:
Jean Lauliac 2017-05-02 03:41:39 -07:00 committed by Facebook Github Bot
parent cf4a98b053
commit 91ff2159d9
8 changed files with 72 additions and 50 deletions

View File

@ -12,7 +12,7 @@
'use strict';
import type {CachedReadResult, ReadResult} from '../../node-haste/Module';
import type {TransformedFile} from '../types.flow';
import type {TransformedCodeFile} from '../types.flow';
import type {ModuleCache} from './node-haste.flow';
module.exports = class Module {
@ -25,7 +25,7 @@ module.exports = class Module {
constructor(
path: string,
moduleCache: ModuleCache,
info: TransformedFile,
info: TransformedCodeFile,
) {
this.hasteID = Promise.resolve(info.hasteID);
this.moduleCache = moduleCache;

View File

@ -14,19 +14,19 @@
const Module = require('./Module');
const Package = require('./Package');
import type {PackageData, TransformedFile} from '../types.flow';
import type {PackageData, TransformedCodeFile} from '../types.flow';
type GetClosestPackageFn = (filePath: string) => ?string;
module.exports = class ModuleCache {
_getClosestPackage: GetClosestPackageFn;
getTransformedFile: string => TransformedFile;
getTransformedFile: string => TransformedCodeFile;
modules: Map<string, Module>;
packages: Map<string, Package>;
constructor(
getClosestPackage: GetClosestPackageFn,
getTransformedFile: string => TransformedFile,
getTransformedFile: string => TransformedCodeFile,
) {
this._getClosestPackage = getClosestPackage;
this.getTransformedFile = getTransformedFile;

View File

@ -18,7 +18,7 @@ import type { // eslint-disable-line sort-requires
import type {
ResolveFn,
TransformedFile,
TransformedCodeFile,
} from '../types.flow';
const DependencyGraphHelpers = require('../../node-haste/DependencyGraph/DependencyGraphHelpers');
@ -34,7 +34,7 @@ const defaults = require('../../../defaults');
type ResolveOptions = {|
assetExts: Extensions,
extraNodeModules: {[id: string]: string},
transformedFiles: {[path: Path]: TransformedFile},
transformedFiles: {[path: Path]: TransformedCodeFile},
|};
const platforms = new Set(defaults.platforms);

View File

@ -27,10 +27,10 @@ export type File = {|
code: string,
map?: ?Object,
path: string,
type: FileTypes,
type: CodeFileTypes,
|};
type FileTypes = 'module' | 'script' | 'asset';
type CodeFileTypes = 'module' | 'script';
export type GraphFn = (
entryPoints: Iterable<string>,
@ -125,16 +125,31 @@ export type TransformResults = {[string]: TransformResult};
export type TransformVariants = {[key: string]: Object};
export type TransformedFile = {
assetContent: ?string,
code: string,
file: string,
hasteID: ?string,
export type TransformedCodeFile = {
+code: string,
+file: string,
+hasteID: ?string,
package?: PackageData,
transformed: TransformResults,
type: FileTypes,
+transformed: TransformResults,
+type: CodeFileTypes,
};
export type AssetFile = {|
+assetContentBase64: string,
+filePath: string,
|};
export type TransformedSourceFile =
| {|
+type: 'code',
+details: TransformedCodeFile,
|}
| {|
+type: 'asset',
+details: AssetFile,
|}
;
export type LibraryOptions = {|
dependencies?: Array<string>,
platform?: string,
@ -145,7 +160,7 @@ export type Base64Content = string;
export type AssetContentsByPath = {[destFilePath: string]: Base64Content};
export type Library = {|
files: Array<TransformedFile>,
+files: Array<TransformedCodeFile>,
/* cannot be a Map because it's JSONified later on */
assets: AssetContentsByPath,
+assets: AssetContentsByPath,
|};

View File

@ -36,7 +36,7 @@ describe('optimizing JS modules', () => {
if (error) {
throw error;
}
transformResult = JSON.stringify(result);
transformResult = JSON.stringify(result.details);
done();
});
});

View File

@ -45,7 +45,8 @@ describe('transforming JS modules:', () => {
it('passes through file name and code', done => {
transformModule(sourceCode, options(), (error, result) => {
expect(result).toEqual(expect.objectContaining({
expect(result.type).toBe('code');
expect(result.details).toEqual(expect.objectContaining({
code: sourceCode,
file: filename,
}));
@ -57,21 +58,24 @@ describe('transforming JS modules:', () => {
const hasteID = 'TheModule';
const codeWithHasteID = `/** @providesModule ${hasteID} */`;
transformModule(codeWithHasteID, options(), (error, result) => {
expect(result).toEqual(expect.objectContaining({hasteID}));
expect(result.type).toBe('code');
expect(result.details).toEqual(expect.objectContaining({hasteID}));
done();
});
});
it('sets `type` to `"module"` by default', done => {
transformModule(sourceCode, options(), (error, result) => {
expect(result).toEqual(expect.objectContaining({type: 'module'}));
expect(result.type).toBe('code');
expect(result.details).toEqual(expect.objectContaining({type: 'module'}));
done();
});
});
it('sets `type` to `"script"` if the input is a polyfill', done => {
transformModule(sourceCode, {...options(), polyfill: true}, (error, result) => {
expect(result).toEqual(expect.objectContaining({type: 'script'}));
expect(result.type).toBe('code');
expect(result.details).toEqual(expect.objectContaining({type: 'script'}));
done();
});
});
@ -105,7 +109,7 @@ describe('transforming JS modules:', () => {
transformModule(sourceCode, options(), (error, result) => {
expect(error).toEqual(null);
const {code, dependencyMapName} = result.transformed.default;
const {code, dependencyMapName} = result.details.transformed.default;
expect(code.replace(/\s+/g, ''))
.toEqual(
`__d(function(global,require,module,exports,${
@ -119,7 +123,7 @@ describe('transforming JS modules:', () => {
transformModule(sourceCode, {...options(), polyfill: true}, (error, result) => {
expect(error).toEqual(null);
const {code} = result.transformed.default;
const {code} = result.details.transformed.default;
expect(code.replace(/\s+/g, ''))
.toEqual(`(function(global){${transformedCode}})(this);`);
done();
@ -128,7 +132,7 @@ describe('transforming JS modules:', () => {
it('creates source maps', done => {
transformModule(sourceCode, options(), (error, result) => {
const {code, map} = result.transformed.default;
const {code, map} = result.details.transformed.default;
const column = code.indexOf('code');
const consumer = new SourceMapConsumer(map);
expect(consumer.originalPositionFor({line: 1, column}))
@ -145,7 +149,7 @@ describe('transforming JS modules:', () => {
transformer.transform.stub.returns(transformResult(body));
transformModule(code, options(), (error, result) => {
expect(result.transformed.default)
expect(result.details.transformed.default)
.toEqual(expect.objectContaining({dependencies: [dep1, dep2]}));
done();
});
@ -160,7 +164,7 @@ describe('transforming JS modules:', () => {
.returns(transformResult([]));
transformModule(sourceCode, options(variants), (error, result) => {
const {dev, prod} = result.transformed;
const {dev, prod} = result.details.transformed;
expect(dev.code.replace(/\s+/g, ''))
.toEqual(
`__d(function(global,require,module,exports,${
@ -179,7 +183,7 @@ describe('transforming JS modules:', () => {
const json = '{"foo":"bar"}';
transformModule(json, {...options(), filename: 'some.json'}, (error, result) => {
const {code} = result.transformed.default;
const {code} = result.details.transformed.default;
expect(code.replace(/\s+/g, ''))
.toEqual(
'__d(function(global,require,module,exports){' +
@ -191,7 +195,7 @@ describe('transforming JS modules:', () => {
it('does not create source maps for JSON files', done => {
transformModule('{}', {...options(), filename: 'some.json'}, (error, result) => {
expect(result.transformed.default)
expect(result.details.transformed.default)
.toEqual(expect.objectContaining({map: null}));
done();
});
@ -209,7 +213,7 @@ describe('transforming JS modules:', () => {
JSON.stringify(pkg),
{...options(), filename: 'arbitrary/package.json'},
(error, result) => {
expect(result.package).toEqual(pkg);
expect(result.details.package).toEqual(pkg);
done();
},
);

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 {TransformedFile, TransformResult} from '../types.flow';
import type {TransformedCodeFile, TransformResult} from '../types.flow';
export type OptimizationOptions = {|
dev: boolean,
@ -27,9 +27,9 @@ export type OptimizationOptions = {|
|};
function optimizeModule(
data: string | TransformedFile,
data: string | TransformedCodeFile,
optimizationOptions: OptimizationOptions,
): TransformedFile {
): TransformedCodeFile {
if (typeof data === 'string') {
data = JSON.parse(data);
}

View File

@ -20,7 +20,8 @@ const {basename} = require('path');
import type {
Callback,
TransformedFile,
TransformedCodeFile,
TransformedSourceFile,
Transformer,
TransformerResult,
TransformResult,
@ -41,7 +42,7 @@ const polyfillFactoryParameters = ['global'];
function transformModule(
content: Buffer,
options: TransformOptions,
callback: Callback<TransformedFile>,
callback: Callback<TransformedSourceFile>,
): void {
if (options.filename.endsWith('.png')) {
transformAsset(content, options, callback);
@ -86,12 +87,15 @@ function transformModule(
const annotations = docblock.parseAsObject(docblock.extract(code));
callback(null, {
assetContent: null,
code,
file: filename,
hasteID: annotations.providesModule || null,
transformed,
type: options.polyfill ? 'script' : 'module',
type: 'code',
details: {
assetContent: null,
code,
file: filename,
hasteID: annotations.providesModule || null,
transformed,
type: options.polyfill ? 'script' : 'module',
},
});
});
return;
@ -116,7 +120,7 @@ function transformJSON(json, options, callback) {
.keys(options.variants || defaultVariants)
.forEach(key => (transformed[key] = moduleData));
const result: TransformedFile = {
const result: TransformedCodeFile = {
assetContent: null,
code: json,
file: filename,
@ -133,20 +137,19 @@ function transformJSON(json, options, callback) {
'react-native': value['react-native'],
};
}
callback(null, result);
callback(null, {type: 'code', details: result});
}
function transformAsset(
content: Buffer,
options: TransformOptions,
callback: Callback<TransformedFile>,
callback: Callback<TransformedSourceFile>,
) {
callback(null, {
assetContent: content.toString('base64'),
code: '',
file: options.filename,
hasteID: null,
transformed: {},
details: {
assetContentBase64: content.toString('base64'),
filePath: options.filename,
},
type: 'asset',
});
}