mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
packager: buck worker: better types
Reviewed By: davidaurelio Differential Revision: D4970025 fbshipit-source-id: 77db309befebe539d25bd8df1039e2304176ca3f
This commit is contained in:
committed by
Facebook Github Bot
parent
cf4a98b053
commit
91ff2159d9
@@ -36,7 +36,7 @@ describe('optimizing JS modules', () => {
|
||||
if (error) {
|
||||
throw error;
|
||||
}
|
||||
transformResult = JSON.stringify(result);
|
||||
transformResult = JSON.stringify(result.details);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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();
|
||||
},
|
||||
);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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',
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user