mirror of https://github.com/status-im/metro.git
BREAKING: metro transformers should only output AST
Reviewed By: jeanlauliac Differential Revision: D6396416 fbshipit-source-id: e4c6f0c0feb1ac638dc13dd3eb400f1bc72b5d44
This commit is contained in:
parent
f434d43f54
commit
b193fc3436
|
@ -15,12 +15,14 @@ jest
|
|||
.mock('../constant-folding')
|
||||
.mock('../extract-dependencies')
|
||||
.mock('../inline')
|
||||
.mock('../minify');
|
||||
.mock('../minify')
|
||||
.mock('babel-generator');
|
||||
|
||||
const {objectContaining} = jasmine;
|
||||
|
||||
describe('code transformation worker:', () => {
|
||||
let transformCode;
|
||||
let babelGenerator;
|
||||
|
||||
let extractDependencies, transformer;
|
||||
beforeEach(() => {
|
||||
|
@ -35,6 +37,13 @@ describe('code transformation worker:', () => {
|
|||
map: [],
|
||||
})),
|
||||
};
|
||||
|
||||
babelGenerator = require('babel-generator');
|
||||
|
||||
babelGenerator.default.mockReturnValue({
|
||||
code: '',
|
||||
map: [],
|
||||
});
|
||||
});
|
||||
|
||||
it('calls the transform with file name, source code, and transform options', function() {
|
||||
|
@ -90,6 +99,11 @@ describe('code transformation worker:', () => {
|
|||
map: [],
|
||||
};
|
||||
|
||||
babelGenerator.default.mockReturnValue({
|
||||
code: 'some.other(code)',
|
||||
map: [],
|
||||
});
|
||||
|
||||
const data = await transformCode(
|
||||
transformer,
|
||||
'filename',
|
||||
|
@ -104,6 +118,12 @@ describe('code transformation worker:', () => {
|
|||
it('removes the leading `module.exports` before returning if the file is a JSON file, even if minified', async () => {
|
||||
const code = '{a:1,b:2}';
|
||||
const filePath = 'arbitrary/file.json';
|
||||
|
||||
babelGenerator.default.mockReturnValue({
|
||||
code: '{a:1,b:2}',
|
||||
map: [],
|
||||
});
|
||||
|
||||
const data = await transformCode(transformer, filePath, filePath, code, {});
|
||||
|
||||
expect(data.result.code).toEqual(code);
|
||||
|
@ -116,6 +136,11 @@ describe('code transformation worker:', () => {
|
|||
};
|
||||
const filePath = 'arbitrary/file.js';
|
||||
|
||||
babelGenerator.default.mockReturnValue({
|
||||
code: `${shebang} \n arbitrary(code)`,
|
||||
map: [],
|
||||
});
|
||||
|
||||
const data = await transformCode(
|
||||
transformer,
|
||||
filePath,
|
||||
|
@ -155,6 +180,11 @@ describe('code transformation worker:', () => {
|
|||
it('passes the transformed code the `extractDependencies`', async () => {
|
||||
const code = 'arbitrary(code)';
|
||||
|
||||
babelGenerator.default.mockReturnValue({
|
||||
code: 'arbitrary(code)',
|
||||
map: [],
|
||||
});
|
||||
|
||||
await transformCode(transformer, 'filename', 'local/filename', code, {});
|
||||
|
||||
expect(extractDependencies).toBeCalledWith(code, 'filename');
|
||||
|
@ -182,6 +212,11 @@ describe('code transformation worker:', () => {
|
|||
it('does not extract requires of JSON files', async () => {
|
||||
const jsonStr = '{"arbitrary":"json"}';
|
||||
|
||||
babelGenerator.default.mockReturnValue({
|
||||
code: '{"arbitrary":"json"}',
|
||||
map: [],
|
||||
});
|
||||
|
||||
const data = await transformCode(
|
||||
transformer,
|
||||
'arbitrary.json',
|
||||
|
|
|
@ -46,9 +46,7 @@ export type TransformArgs<ExtraOptions: {}> = {|
|
|||
|};
|
||||
|
||||
export type TransformResults = {
|
||||
ast: ?Ast,
|
||||
code: string,
|
||||
map: ?MappingsMap | RawMappings,
|
||||
ast: Ast,
|
||||
};
|
||||
|
||||
export type Transform<ExtraOptions: {}> = (
|
||||
|
@ -126,22 +124,20 @@ async function transformCode(
|
|||
src: sourceCode,
|
||||
});
|
||||
|
||||
// If we receive AST from the transformer, serialize it into code/map.
|
||||
const transformed = result.ast
|
||||
? generate(
|
||||
result.ast,
|
||||
{
|
||||
code: false,
|
||||
comments: false,
|
||||
compact: false,
|
||||
filename: localPath,
|
||||
retainLines: false,
|
||||
sourceFileName: filename,
|
||||
sourceMaps: true,
|
||||
},
|
||||
sourceCode,
|
||||
)
|
||||
: {code: result.code, map: result.map};
|
||||
// Serialize the AST received from the transformer.
|
||||
const transformed = generate(
|
||||
result.ast,
|
||||
{
|
||||
code: false,
|
||||
comments: false,
|
||||
compact: false,
|
||||
filename: localPath,
|
||||
retainLines: false,
|
||||
sourceFileName: filename,
|
||||
sourceMaps: true,
|
||||
},
|
||||
sourceCode,
|
||||
);
|
||||
|
||||
// If the transformer returns standard sourcemaps, we need to transform them
|
||||
// to rawMappings so we can process them correctly.
|
||||
|
|
|
@ -17,7 +17,6 @@ const babel = require('babel-core');
|
|||
const crypto = require('crypto');
|
||||
const externalHelpersPlugin = require('babel-plugin-external-helpers');
|
||||
const fs = require('fs');
|
||||
const generate = require('babel-generator').default;
|
||||
const inlineRequiresPlugin = require('babel-preset-fbjs/plugins/inline-requires');
|
||||
const json5 = require('json5');
|
||||
const makeHMRConfig = require('babel-preset-react-native/configs/hmr');
|
||||
|
@ -132,36 +131,9 @@ function transform({filename, options, src, plugins}: Params) {
|
|||
|
||||
try {
|
||||
const babelConfig = buildBabelConfig(filename, options, plugins);
|
||||
const {ast, ignored} = babel.transform(src, babelConfig);
|
||||
const {ast} = babel.transform(src, babelConfig);
|
||||
|
||||
if (ignored) {
|
||||
return {
|
||||
ast: null,
|
||||
code: src,
|
||||
filename,
|
||||
map: null,
|
||||
};
|
||||
} else {
|
||||
const result = generate(
|
||||
ast,
|
||||
{
|
||||
comments: false,
|
||||
compact: false,
|
||||
filename,
|
||||
retainLines: !!options.retainLines,
|
||||
sourceFileName: filename,
|
||||
sourceMaps: true,
|
||||
},
|
||||
src,
|
||||
);
|
||||
|
||||
return {
|
||||
ast,
|
||||
code: result.code,
|
||||
filename,
|
||||
map: options.generateSourceMaps ? result.map : result.rawMappings,
|
||||
};
|
||||
}
|
||||
return {ast};
|
||||
} finally {
|
||||
process.env.BABEL_ENV = OLD_BABEL_ENV;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue