mirror of https://github.com/status-im/metro.git
Add support for wrapping polyfills
Summary: This adds support to wrap polyfills differently from regular modules (`(function(global){ ... }(this))` instead of `__d(function...)`. Reviewed By: cpojer Differential Revision: D4147031 fbshipit-source-id: c19e968c1498cac653ab2649e3089d8c29571926
This commit is contained in:
parent
3064549bca
commit
ef448d4fa1
|
@ -27,6 +27,7 @@ const basename = path.basename;
|
||||||
const dirname = path.dirname;
|
const dirname = path.dirname;
|
||||||
const defaultVariants = {default: {}};
|
const defaultVariants = {default: {}};
|
||||||
const moduleFactoryParameters = ['require', 'module', 'global', 'exports'];
|
const moduleFactoryParameters = ['require', 'module', 'global', 'exports'];
|
||||||
|
const polyfillFactoryParameters = ['global'];
|
||||||
|
|
||||||
function transformJSON(infile, options, outfile, callback) {
|
function transformJSON(infile, options, outfile, callback) {
|
||||||
const json = fs.readFileSync(infile, 'utf8');
|
const json = fs.readFileSync(infile, 'utf8');
|
||||||
|
@ -95,7 +96,8 @@ function transformModule(infile, options, outfile, callback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.keys(transformed).forEach(key => {
|
Object.keys(transformed).forEach(key => {
|
||||||
transformed[key] = makeResult(transformed[key].ast, filename, code);
|
transformed[key] =
|
||||||
|
makeResult(transformed[key].ast, filename, code, options.polyfill);
|
||||||
});
|
});
|
||||||
|
|
||||||
const annotations = docblock.parseAsObject(docblock.extract(code));
|
const annotations = docblock.parseAsObject(docblock.extract(code));
|
||||||
|
@ -117,7 +119,13 @@ function transformModule(infile, options, outfile, callback) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function optimizeModule(infile, outfile, options, callback) {
|
function optimizeModule(
|
||||||
|
infile,
|
||||||
|
outfile,
|
||||||
|
isPolyfill,
|
||||||
|
inliningOptions,
|
||||||
|
callback,
|
||||||
|
) {
|
||||||
const data = JSON.parse(fs.readFileSync(infile, 'utf8'));
|
const data = JSON.parse(fs.readFileSync(infile, 'utf8'));
|
||||||
const transformed = data.transformed;
|
const transformed = data.transformed;
|
||||||
const result = Object.assign({}, data);
|
const result = Object.assign({}, data);
|
||||||
|
@ -127,39 +135,55 @@ function optimizeModule(infile, outfile, options, callback) {
|
||||||
const code = data.code;
|
const code = data.code;
|
||||||
|
|
||||||
Object.keys(transformed).forEach(key => {
|
Object.keys(transformed).forEach(key => {
|
||||||
result.transformed[key] = optimize(transformed[key], file, code, options);
|
result.transformed[key] =
|
||||||
|
optimize(transformed[key], file, code, isPolyfill, inliningOptions);
|
||||||
});
|
});
|
||||||
writeResult(outfile, result);
|
writeResult(outfile, result);
|
||||||
|
|
||||||
callback(null);
|
callback(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeResult(ast, filename, sourceCode) {
|
function makeResult(ast, filename, sourceCode, isPolyfill = false) {
|
||||||
const dependencies = collectDependencies(ast);
|
const dependencies = isPolyfill ? [] : collectDependencies(ast);
|
||||||
const file = wrapModule(ast);
|
const file = isPolyfill ? wrapPolyfill(ast) : wrapModule(ast);
|
||||||
|
|
||||||
const gen = generate(file, filename, sourceCode);
|
const gen = generate(file, filename, sourceCode);
|
||||||
return {code: gen.code, map: gen.map, dependencies};
|
return {code: gen.code, map: gen.map, dependencies};
|
||||||
}
|
}
|
||||||
|
|
||||||
function wrapModule(file) {
|
function wrapModule(file) {
|
||||||
const p = file.program;
|
|
||||||
const t = babel.types;
|
const t = babel.types;
|
||||||
const factory = t.functionExpression(
|
const factory = functionFromProgram(file.program, moduleFactoryParameters);
|
||||||
t.identifier(''),
|
|
||||||
moduleFactoryParameters.map(makeIdentifier),
|
|
||||||
t.blockStatement(p.body, p.directives),
|
|
||||||
);
|
|
||||||
const def = t.callExpression(t.identifier('__d'), [factory]);
|
const def = t.callExpression(t.identifier('__d'), [factory]);
|
||||||
return t.file(t.program([t.expressionStatement(def)]));
|
return t.file(t.program([t.expressionStatement(def)]));
|
||||||
}
|
}
|
||||||
|
|
||||||
function optimize(transformed, file, originalCode, options) {
|
function wrapPolyfill(file) {
|
||||||
|
const t = babel.types;
|
||||||
|
const factory = functionFromProgram(file.program, polyfillFactoryParameters);
|
||||||
|
const iife = t.callExpression(factory, [t.identifier('this')]);
|
||||||
|
return t.file(t.program([t.expressionStatement(iife)]));
|
||||||
|
}
|
||||||
|
|
||||||
|
function functionFromProgram(program, parameters) {
|
||||||
|
const t = babel.types;
|
||||||
|
return t.functionExpression(
|
||||||
|
t.identifier(''),
|
||||||
|
parameters.map(makeIdentifier),
|
||||||
|
t.blockStatement(program.body, program.directives),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function optimize(transformed, file, originalCode, isPolyfill, options) {
|
||||||
const optimized =
|
const optimized =
|
||||||
optimizeCode(transformed.code, transformed.map, file, options);
|
optimizeCode(transformed.code, transformed.map, file, options);
|
||||||
|
|
||||||
const dependencies = collectDependencies.forOptimization(
|
const dependencies = isPolyfill
|
||||||
optimized.ast, transformed.dependencies);
|
? []
|
||||||
|
: collectDependencies.forOptimization(
|
||||||
|
optimized.ast,
|
||||||
|
transformed.dependencies,
|
||||||
|
);
|
||||||
|
|
||||||
const inputMap = transformed.map;
|
const inputMap = transformed.map;
|
||||||
const gen = generate(optimized.ast, file, originalCode);
|
const gen = generate(optimized.ast, file, originalCode);
|
||||||
|
|
|
@ -24,7 +24,8 @@ declare export function transformModule(
|
||||||
declare export function optimizeModule(
|
declare export function optimizeModule(
|
||||||
infile: string,
|
infile: string,
|
||||||
outfile: string,
|
outfile: string,
|
||||||
options: {
|
isPolyfill?: boolean,
|
||||||
|
inliningOptions: {
|
||||||
dev?: boolean,
|
dev?: boolean,
|
||||||
platform?: string,
|
platform?: string,
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue