/node_modules/grunt/node_modules/lodash/build/post-compile.js

https://gitlab.com/CORP-RESELLER/amcharts-accessibility-plugin · JavaScript · 85 lines · 53 code · 9 blank · 23 comment · 5 complexity · 66b0287287cd4bad0df2c9722e2477d1 MD5 · raw file

  1. #!/usr/bin/env node
  2. ;(function() {
  3. 'use strict';
  4. /** The Node filesystem module */
  5. var fs = require('fs');
  6. /** The minimal license/copyright template */
  7. var licenseTemplate = {
  8. 'lodash':
  9. '/*!\n' +
  10. ' Lo-Dash @VERSION lodash.com/license\n' +
  11. ' Underscore.js 1.4.2 underscorejs.org/LICENSE\n' +
  12. '*/',
  13. 'underscore':
  14. '/*! Underscore.js @VERSION underscorejs.org/LICENSE */'
  15. };
  16. /*--------------------------------------------------------------------------*/
  17. /**
  18. * Post-process a given minified Lo-Dash `source`, preparing it for
  19. * deployment.
  20. *
  21. * @param {String} source The source to process.
  22. * @returns {String} Returns the processed source.
  23. */
  24. function postprocess(source) {
  25. // remove old copyright/license header
  26. source = source.replace(/^\/\*![\s\S]+?\*\/\n/, '');
  27. // move vars exposed by the Closure Compiler into the IIFE
  28. source = source.replace(/^((?:(['"])use strict\2;)?(?:var (?:[a-z]+=(?:!0|!1|null)[,;])+)?)([\s\S]*?function[^)]+\){)/, '$3$1');
  29. // correct overly aggressive Closure Compiler advanced optimizations
  30. source = source.replace(/prototype\s*=\s*{\s*valueOf\s*:\s*1\s*}/, 'prototype={valueOf:1,y:1}');
  31. // unescape properties (i.e. foo["bar"] => foo.bar)
  32. source = source.replace(/(\w)\["([^."]+)"\]/g, function(match, left, right) {
  33. return /\W/.test(right) ? match : (left + '.' + right);
  34. });
  35. // flip `typeof` expressions to help optimize Safari and
  36. // correct the AMD module definition for AMD build optimizers
  37. // (e.g. from `"number" == typeof x` to `typeof x == "number")
  38. source = source.replace(/(return)?("[^"]+")\s*([!=]=)\s*(typeof(?:\s*\([^)]+\)|\s+[\w.]+))/g, function(match, ret, type, equality, expression) {
  39. return (ret ? ret + ' ' : '') + expression + equality + type;
  40. });
  41. // add trailing semicolon
  42. if (source) {
  43. source = source.replace(/[\s;]*$/, ';');
  44. }
  45. // exit early if version snippet isn't found
  46. var snippet = /VERSION\s*[=:]\s*([\'"])(.*?)\1/.exec(source);
  47. if (!snippet) {
  48. return source;
  49. }
  50. // add license
  51. return licenseTemplate[/call\(this\);?$/.test(source) ? 'underscore' : 'lodash']
  52. .replace('@VERSION', snippet[2]) + '\n;' + source;
  53. }
  54. /*--------------------------------------------------------------------------*/
  55. // expose `postprocess`
  56. if (module != require.main) {
  57. module.exports = postprocess;
  58. }
  59. else {
  60. // read the Lo-Dash source file from the first argument if the script
  61. // was invoked directly (e.g. `node post-compile.js source.js`) and write to
  62. // the same file
  63. (function() {
  64. var options = process.argv;
  65. if (options.length < 3) {
  66. return;
  67. }
  68. var filePath = options[options.length - 1],
  69. source = fs.readFileSync(filePath, 'utf8');
  70. fs.writeFileSync(filePath, postprocess(source), 'utf8');
  71. }());
  72. }
  73. }());