1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-26 10:08:06 +02:00

Removes last UMDs from the modules.

This commit is contained in:
Yury Delendik 2017-05-30 20:38:22 -05:00
parent e51718711b
commit 66c8893815
28 changed files with 71 additions and 224 deletions

View file

@ -80,6 +80,14 @@ function handlePreprocessorAction(ctx, actionName, args, loc) {
function postprocessNode(ctx, node) {
switch (node.type) {
case 'ExportNamedDeclaration':
case 'ImportDeclaration':
if (node.source && node.source.type === 'Literal' &&
ctx.map && ctx.map[node.source.value]) {
var newValue = ctx.map[node.source.value];
node.source.value = node.source.raw = newValue;
}
break;
case 'IfStatement':
if (isLiteral(node.test, true)) {
// if (true) stmt1; => stmt1
@ -165,6 +173,13 @@ function postprocessNode(ctx, node) {
return handlePreprocessorAction(ctx, action,
node.arguments, node.loc);
}
// require('string')
if (node.callee.type === 'Identifier' && node.callee.name === 'require' &&
node.arguments.length === 1 && node.arguments[0].type === 'Literal' &&
ctx.map && ctx.map[node.arguments[0].value]) {
var requireName = node.arguments[0];
requireName.value = requireName.raw = ctx.map[requireName.value];
}
break;
case 'BlockStatement':
var subExpressionIndex = 0;
@ -201,86 +216,6 @@ function postprocessNode(ctx, node) {
block.body.pop();
}
break;
case 'Program':
// Checking for a function closure that looks like UMD header.
node.body.some(function (item, index) {
// Is it `(function (root, factory) { ? }(this, function (?) {?}));` ?
if (item.type !== 'ExpressionStatement' ||
item.expression.type !== 'CallExpression' ||
item.expression.callee.type !== 'FunctionExpression' ||
item.expression.callee.params.length !== 2 ||
item.expression.arguments.length !== 2 ||
item.expression.arguments[0].type !== 'ThisExpression' ||
item.expression.arguments[1].type !== 'FunctionExpression') {
return false;
}
var init = item.expression.callee;
// Is init body looks like
// `if (?) { ? } else if (typeof exports !== 'undefined') { ? } ...`?
if (init.body.type !== 'BlockStatement' ||
init.body.body.length !== 1 ||
init.body.body[0].type !== 'IfStatement') {
return false;
}
var initIf = init.body.body[0];
if (initIf.alternate.type !== 'IfStatement' ||
initIf.alternate.test.type !== 'BinaryExpression' ||
initIf.alternate.test.operator !== '!==' ||
initIf.alternate.test.left.type !== 'UnaryExpression' ||
initIf.alternate.test.left.operator !== 'typeof' ||
initIf.alternate.test.left.argument.type !== 'Identifier' ||
initIf.alternate.test.left.argument.name !== 'exports' ||
initIf.alternate.test.right.type !== 'Literal' ||
initIf.alternate.test.right.value !== 'undefined' ||
initIf.alternate.consequent.type !== 'BlockStatement') {
return false;
}
var commonJsInit = initIf.alternate.consequent;
// Is commonJsInit `factory(exports, ...)` ?
if (commonJsInit.body.length !== 1 ||
commonJsInit.body[0].type !== 'ExpressionStatement' ||
commonJsInit.body[0].expression.type !== 'CallExpression' ||
commonJsInit.body[0].expression.callee.type !== 'Identifier') {
return false;
}
var commonJsInitArgs = commonJsInit.body[0].expression.arguments;
if (commonJsInitArgs.length === 0 ||
commonJsInitArgs[0].type !== 'Identifier' ||
commonJsInitArgs[0].name !== 'exports') {
return false;
}
var factory = item.expression.arguments[1];
// Is factory `function (exports, ....) { ? }` ?
if (factory.params.length === 0 ||
factory.params[0].type !== 'Identifier' ||
factory.params[0].name !== 'exports' ||
factory.body.type !== 'BlockStatement') {
return true;
}
var factoryParams = factory.params;
var factoryBody = factory.body;
// Remove closure and function and replacing parameters with vars.
node.body.splice(index, 1);
for (var i = 1, ii = factoryParams.length; i < ii; i++) {
var varNode = {
type: 'VariableDeclaration',
'declarations': [{
type: 'VariableDeclarator',
id: factoryParams[i],
init: commonJsInitArgs[i] || null,
loc: factoryParams[i].loc
}],
kind: 'var'
};
node.body.splice(index++, 0, varNode);
}
factoryBody.body.forEach(function (item) {
node.body.splice(index++, 0, item);
});
return true;
});
break;
}
return node;
}