|
1 | | -/*! amdclean - v2.2.8 - 2014-10-02 |
| 1 | +/*! amdclean - v2.3.0 - 2014-10-08 |
2 | 2 | * http://gregfranko.com/amdclean |
3 | 3 | * Copyright (c) 2014 Greg Franko */ |
4 | 4 |
|
@@ -96,7 +96,10 @@ _defaultOptions_ = { |
96 | 96 | 'end': '\n}());' |
97 | 97 | }, |
98 | 98 | // Determines if certain aggressive file size optimization techniques will be used to transform the soure code |
99 | | - 'aggressiveOptimizations': false |
| 99 | + 'aggressiveOptimizations': false, |
| 100 | + // Configuration info for modules |
| 101 | + // Note: Further info can be found here - http://requirejs.org/docs/api.html#config-moduleconfig |
| 102 | + 'config': {} |
100 | 103 | }; |
101 | 104 | // errorMsgs.js |
102 | 105 | // ============ |
@@ -379,7 +382,7 @@ convertToIIFE = function convertToIIFE(obj) { |
379 | 382 | // Returns a function expression that is executed immediately |
380 | 383 | // e.g. var example = function(){}() |
381 | 384 | convertToIIFEDeclaration = function convertToIIFEDeclaration(obj) { |
382 | | - var moduleName = obj.moduleName, callbackFuncParams = obj.callbackFuncParams, isOptimized = obj.isOptimized, callback = obj.callbackFunc, node = obj.node, name = callback.name, type = callback.type, range = node.range || defaultValues.defaultRange, loc = node.loc || defaultValues.defaultLOC, callbackFunc = function () { |
| 385 | + var amdclean = this, options = amdclean.options, moduleId = obj.moduleId, moduleName = obj.moduleName, hasModuleParam = obj.hasModuleParam, hasExportsParam = obj.hasExportsParam, callbackFuncParams = obj.callbackFuncParams, isOptimized = obj.isOptimized, callback = obj.callbackFunc, node = obj.node, name = callback.name, type = callback.type, range = node.range || defaultValues.defaultRange, loc = node.loc || defaultValues.defaultLOC, callbackFunc = function () { |
383 | 386 | var cbFunc = obj.callbackFunc; |
384 | 387 | if (type === 'Identifier' && name !== 'undefined') { |
385 | 388 | cbFunc = { |
@@ -454,7 +457,40 @@ convertToIIFEDeclaration = function convertToIIFEDeclaration(obj) { |
454 | 457 | }; |
455 | 458 | } |
456 | 459 | return cbFunc; |
457 | | - }(), dependencyNames = obj.dependencyNames, cb = function () { |
| 460 | + }(), dependencyNames = function () { |
| 461 | + var depNames = obj.dependencyNames, objExpression = { |
| 462 | + 'type': 'ObjectExpression', |
| 463 | + 'properties': [], |
| 464 | + 'range': range, |
| 465 | + 'loc': loc |
| 466 | + }, configMemberExpression = { |
| 467 | + 'type': 'MemberExpression', |
| 468 | + 'computed': false, |
| 469 | + 'object': { |
| 470 | + 'type': 'Identifier', |
| 471 | + 'name': 'module' |
| 472 | + }, |
| 473 | + 'property': { |
| 474 | + 'type': 'Identifier', |
| 475 | + 'name': moduleId |
| 476 | + } |
| 477 | + }, moduleDepIndex; |
| 478 | + if (options.config && options.config[moduleId]) { |
| 479 | + if (hasExportsParam && hasModuleParam) { |
| 480 | + return [ |
| 481 | + objExpression, |
| 482 | + objExpression, |
| 483 | + configMemberExpression |
| 484 | + ]; |
| 485 | + } else if (hasModuleParam) { |
| 486 | + moduleDepIndex = _.findIndex(depNames, function (currentDep) { |
| 487 | + return currentDep.name === '{}'; |
| 488 | + }); |
| 489 | + depNames[moduleDepIndex] = configMemberExpression; |
| 490 | + } |
| 491 | + } |
| 492 | + return depNames; |
| 493 | + }(), cb = function () { |
458 | 494 | if (callbackFunc.type === 'Literal' || callbackFunc.type === 'Identifier' && callbackFunc.name === 'undefined' || isOptimized === true) { |
459 | 495 | return callbackFunc; |
460 | 496 | } else { |
@@ -641,6 +677,9 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) { |
641 | 677 | }(), originalCallbackFuncParams, hasExportsParam = function () { |
642 | 678 | var cbParams = callbackFunc.params || []; |
643 | 679 | return _.where(cbParams, { 'name': 'exports' }).length; |
| 680 | + }(), hasModuleParam = function () { |
| 681 | + var cbParams = callbackFunc.params || []; |
| 682 | + return _.where(cbParams, { 'name': 'module' }).length; |
644 | 683 | }(), normalizeDependencyNames = {}, dependencyNames = function () { |
645 | 684 | var deps = [], currentName; |
646 | 685 | _.each(dependencies, function (currentDependency) { |
@@ -827,9 +866,11 @@ convertToFunctionExpression = function convertToFunctionExpression(obj) { |
827 | 866 | }); |
828 | 867 | if (isDefine) { |
829 | 868 | return convertToIIFEDeclaration.call(amdclean, { |
| 869 | + 'moduleId': moduleId, |
830 | 870 | 'moduleName': moduleName, |
831 | 871 | 'dependencyNames': dependencyNames, |
832 | 872 | 'callbackFuncParams': callbackFuncParams, |
| 873 | + 'hasModuleParam': hasModuleParam, |
833 | 874 | 'hasExportsParam': hasExportsParam, |
834 | 875 | 'callbackFunc': callbackFunc, |
835 | 876 | 'isOptimized': isOptimized, |
@@ -1237,7 +1278,7 @@ generateCode = function generateCode(ast) { |
1237 | 1278 | // ======== |
1238 | 1279 | // Removes any AMD and/or CommonJS trace from the provided source code |
1239 | 1280 | clean = function clean() { |
1240 | | - var amdclean = this, options = amdclean.options, ignoreModules = options.ignoreModules, originalAst = {}, ast = {}, generatedCode, declarations = [], hoistedVariables = {}, hoistedCallbackParameters = {}, defaultRange = defaultValues.defaultRange, defaultLOC = defaultValues.defaultLOC; |
| 1281 | + var amdclean = this, options = amdclean.options, ignoreModules = options.ignoreModules, originalAst = {}, ast = {}, configAst = {}, generatedCode, declarations = [], hoistedVariables = {}, hoistedCallbackParameters = {}, defaultRange = defaultValues.defaultRange, defaultLOC = defaultValues.defaultLOC; |
1241 | 1282 | // Creates and stores an AST representation of the code |
1242 | 1283 | originalAst = createAst.call(amdclean); |
1243 | 1284 | // Loops through the AST, finds all module ids, and stores them in the current instance storedModules property |
@@ -1422,6 +1463,58 @@ clean = function clean() { |
1422 | 1463 | }); |
1423 | 1464 | } |
1424 | 1465 | }); |
| 1466 | + // Adds a local module variable if a user wants local module information available to them |
| 1467 | + if (_.isObject(options.config) && !_.isEmpty(options.config)) { |
| 1468 | + configAst = function () { |
| 1469 | + var props = []; |
| 1470 | + _.each(options.config, function (val, key) { |
| 1471 | + var currentModuleConfig = options.config[key]; |
| 1472 | + props.push({ |
| 1473 | + 'type': 'Property', |
| 1474 | + 'key': { |
| 1475 | + 'type': 'Literal', |
| 1476 | + 'value': key |
| 1477 | + }, |
| 1478 | + 'value': { |
| 1479 | + 'type': 'ObjectExpression', |
| 1480 | + 'properties': [{ |
| 1481 | + 'type': 'Property', |
| 1482 | + 'key': { |
| 1483 | + 'type': 'Literal', |
| 1484 | + 'value': 'config' |
| 1485 | + }, |
| 1486 | + 'value': { |
| 1487 | + 'type': 'FunctionExpression', |
| 1488 | + 'id': null, |
| 1489 | + 'params': [], |
| 1490 | + 'defaults': [], |
| 1491 | + 'body': { |
| 1492 | + 'type': 'BlockStatement', |
| 1493 | + 'body': [{ |
| 1494 | + 'type': 'ReturnStatement', |
| 1495 | + 'argument': createAst.call(amdclean, 'var x =' + JSON.stringify(currentModuleConfig)).body[0].declarations[0].init |
| 1496 | + }] |
| 1497 | + } |
| 1498 | + }, |
| 1499 | + 'kind': 'init' |
| 1500 | + }] |
| 1501 | + } |
| 1502 | + }); |
| 1503 | + }); |
| 1504 | + return { |
| 1505 | + 'type': 'VariableDeclarator', |
| 1506 | + 'id': { |
| 1507 | + 'type': 'Identifier', |
| 1508 | + 'name': 'module' |
| 1509 | + }, |
| 1510 | + 'init': { |
| 1511 | + 'type': 'ObjectExpression', |
| 1512 | + 'properties': props |
| 1513 | + } |
| 1514 | + }; |
| 1515 | + }(); |
| 1516 | + declarations.push(configAst); |
| 1517 | + } |
1425 | 1518 | // If there are declarations, the declarations are preprended to the beginning of the code block |
1426 | 1519 | if (declarations.length) { |
1427 | 1520 | ast.body.unshift({ |
@@ -1570,7 +1663,7 @@ clean = function clean() { |
1570 | 1663 | // The object that is publicly accessible |
1571 | 1664 | publicAPI = { |
1572 | 1665 | // Current project version number |
1573 | | - 'VERSION': '2.2.8', |
| 1666 | + 'VERSION': '2.3.0', |
1574 | 1667 | 'clean': function (options, overloadedOptions) { |
1575 | 1668 | // Creates a new AMDclean instance |
1576 | 1669 | var amdclean = new AMDclean(options, overloadedOptions), cleanedCode = amdclean.clean(); |
|
0 commit comments