Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. /*jshint latedef:nofunc, node:true*/
  4.  
  5. // Modules
  6. var fs = require('fs');
  7. var path = require('path');
  8. var dependencyPath = path.join('node_modules', 'cordova-uglify', 'node_modules');
  9. // cordova-uglify module dependencies
  10. var UglifyJS = require(path.join(dependencyPath, 'uglify-js'));
  11. var CleanCSS = require(path.join(dependencyPath, 'clean-css'));
  12. var ngAnnotate = require(path.join(dependencyPath, 'ng-annotate'));
  13.  
  14. // Process
  15. var rootDir = process.argv[2];
  16. var platformPath = path.join(rootDir, 'platforms');
  17. var platforms = process.env.CORDOVA_PLATFORMS.split(',');
  18. var cliCommand = process.env.CORDOVA_CMDLINE;
  19.  
  20. // Hook configuration
  21. var configFilePath = path.join(rootDir, 'hooks/uglify-config.json');
  22. var hookConfig = JSON.parse(fs.readFileSync(configFilePath));
  23. var isRelease = hookConfig.alwaysRun || (cliCommand.indexOf('--release') > -1);
  24. var recursiveFolderSearch = hookConfig.recursiveFolderSearch; // set this to false to manually indicate the folders to process
  25. var foldersToProcess = hookConfig.foldersToProcess; // add other www folders in here if needed (ex. js/controllers)
  26. var cssMinifier = new CleanCSS(hookConfig.cleanCssOptions);
  27.  
  28. // Exit
  29. if (!isRelease) {
  30. return;
  31. }
  32.  
  33. // Run uglifier
  34. run();
  35.  
  36. /**
  37. * Run compression for all specified platforms.
  38. * @return {undefined}
  39. */
  40. function run() {
  41. platforms.forEach(function(platform) {
  42. var wwwPath;
  43.  
  44. switch (platform) {
  45. case 'android':
  46. wwwPath = path.join(platformPath, platform, 'assets', 'www');
  47. break;
  48.  
  49. case 'ios':
  50. case 'browser':
  51. case 'wp8':
  52. case 'windows':
  53. wwwPath = path.join(platformPath, platform, 'www');
  54. break;
  55.  
  56. default:
  57. console.log('this hook only supports android, ios, wp8, windows, and browser currently');
  58. return;
  59. }
  60.  
  61. processFolders(wwwPath);
  62. });
  63. }
  64.  
  65. /**
  66. * Processes defined folders.
  67. * @param {string} wwwPath - Path to www directory
  68. * @return {undefined}
  69. */
  70. function processFolders(wwwPath) {
  71. foldersToProcess.forEach(function(folder) {
  72. processFiles(path.join(wwwPath, folder));
  73. });
  74. }
  75.  
  76. /**
  77. * Processes files in directories.
  78. * @param {string} dir - Directory path
  79. * @return {undefined}
  80. */
  81. function processFiles(dir) {
  82. fs.readdir(dir, function(err, list) {
  83. if (err) {
  84. console.log('processFiles err: ' + err);
  85.  
  86. return;
  87. }
  88.  
  89. list.forEach(function(file) {
  90. file = path.join(dir, file);
  91.  
  92. fs.stat(file, function(err, stat) {
  93. if (stat.isFile()) {
  94. compress(file);
  95.  
  96. return;
  97. }
  98.  
  99. if (recursiveFolderSearch && stat.isDirectory()) {
  100. processFiles(file);
  101.  
  102. return;
  103. }
  104. });
  105. });
  106. });
  107. }
  108.  
  109. /**
  110. * Compresses file.
  111. * @param {string} file - File path
  112. * @return {undefined}
  113. */
  114. function compress(file) {
  115. var ext = path.extname(file),
  116. res,
  117. source,
  118. result;
  119.  
  120. switch (ext) {
  121. case '.js':
  122. console.log('uglifying js file ' + file);
  123.  
  124. res = ngAnnotate(String(fs.readFileSync(file)), {
  125. add: true
  126. });
  127. result = UglifyJS.minify(res.src, hookConfig.uglifyJsOptions);
  128. fs.writeFileSync(file, result.code, 'utf8'); // overwrite the original unminified file
  129. break;
  130.  
  131. case '.css':
  132. console.log('minifying css file ' + file);
  133.  
  134. source = fs.readFileSync(file, 'utf8');
  135. result = cssMinifier.minify(source);
  136. fs.writeFileSync(file, result.styles, 'utf8'); // overwrite the original unminified file
  137. break;
  138.  
  139. default:
  140. console.log('encountered a ' + ext + ' file, not compressing it');
  141. break;
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement