Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const
  4. webpack = require('webpack'),
  5. uglify = require("uglify-js"),
  6. path = require('path'),
  7. fs = require('fs'),
  8. MemoryFS = require('memory-fs'),
  9.  
  10. BASE_PATHS = {
  11. RESOURCES: 'src/main/webapp/resources/',
  12. CONTROLLERS: 'src/main/webapp/resources/js/controllers/',
  13. },
  14.  
  15. WEBPACK_CONFIGURATION = {
  16. name: 'eonline-mobile',
  17. entry: {
  18. apps: path.join(__dirname, BASE_PATHS.CONTROLLERS + 'more/apps.js'),
  19. gallery: path.join(__dirname, BASE_PATHS.CONTROLLERS + 'photos/gallery.js')
  20. },
  21. output: {
  22.  
  23. pathinfo: false,
  24. libraryTarget: 'umd',
  25.  
  26. path: '/' /*path.join(__dirname, BASE_PATHS.RESOURCES, 'bundle')*/,
  27. filename: '[name].js'
  28. },
  29. devtool: 'source-map'
  30. };
  31.  
  32.  
  33. const mfs = new MemoryFS();
  34.  
  35. /**
  36. * @type {Compiler}
  37. */
  38. const compiler = webpack(WEBPACK_CONFIGURATION);
  39. compiler.outputFileSystem = mfs;
  40.  
  41. const filesInMemory = [];
  42.  
  43. const FileById = function (name, data) {
  44. this.name = name;
  45. this.data = data;
  46. };
  47.  
  48. compiler.run((error, stats) =>
  49. {
  50. console.log('webpack: bundle complete');
  51.  
  52. console.assert((error == null), error);
  53.  
  54. if (mfs.data) {
  55.  
  56. Object.keys(mfs.data).forEach((item) => {
  57.  
  58. if (item.indexOf('.map') === -1) {
  59.  
  60. const content = mfs.readFileSync(path.join('/', item),'utf8');
  61.  
  62. console.log('memory file sync complete for ', item);
  63.  
  64. const ast = uglify.minify(
  65. {
  66. item: content
  67. },
  68. {
  69. fromString: true,
  70. warnings: false,
  71. mangleProperties: true,
  72. compress: {
  73. unsafe: true,
  74. collapse_vars: true,
  75. passes: 3,
  76. reduce_vars: true,
  77. join_vars: true,
  78. if_return: true,
  79. booleans: true
  80. }
  81. });
  82.  
  83. console.log('uglify: compression complete for ', item);
  84.  
  85. filesInMemory.push(new FileById(item, ast.code));
  86. }
  87. });
  88. }
  89.  
  90. if (filesInMemory.length) {
  91. writeCodeToFile(0);
  92. }
  93.  
  94. });
  95.  
  96. function writeCodeToFile(index)
  97. {
  98. if (filesInMemory[index])
  99. {
  100. console.log('write file #', index+1);
  101.  
  102. var fileInMemory = filesInMemory[index];
  103.  
  104. fs.writeFile(fileInMemory.name, fileInMemory.data, null, () => {
  105. writeCodeToFile(index + 1);
  106. });
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement