webpack.config.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * FeHelper Webpack Config
  3. * @author zhaoxianlie
  4. */
  5. let path = require('path');
  6. let shell = require('shelljs');
  7. let glob = require('glob');
  8. let uglifyCss = require('uglifycss');
  9. let htmlMinifier = require('html-minifier');
  10. let fs = require('fs');
  11. let CopyWebpackPlugin = require('copy-webpack-plugin');
  12. let rootPath = path.resolve('.');
  13. // html 文件压缩
  14. let htmlCompress = function () {
  15. glob.sync('output/apps/**/*.html').map(cf => {
  16. try {
  17. let rawHtml = fs.readFileSync(cf).toString();
  18. let compressedHtml = htmlMinifier.minify(rawHtml, {
  19. collapseWhitespace: true
  20. });
  21. fs.writeFileSync(cf, compressedHtml);
  22. } catch (e) {
  23. console.log(cf, '压缩失败')
  24. }
  25. });
  26. };
  27. // css 文件压缩
  28. let cssCompress = function () {
  29. let reg = /\@import\s+url\(\s*('|")([^\)]+)\1\s*\)/gm;
  30. glob.sync('output/apps/**/*.css').map(cf => {
  31. let rawCss = fs.readFileSync(cf);
  32. rawCss = rawCss.toString().replace(reg, ($0, $1, importedFile) => {
  33. let iPath = path.resolve(cf, '../' + importedFile);
  34. return fs.readFileSync(iPath).toString() + ';';
  35. });
  36. let uglifiedCss = uglifyCss.processString(rawCss, {maxLineLen: 500, expandVars: true});
  37. fs.writeFileSync(cf, uglifiedCss);
  38. });
  39. };
  40. // zip the fehelper
  41. let zipForChromeWebStore = function () {
  42. shell.cd(`${rootPath}/output`);
  43. shell.exec('zip -r fehelper.zip apps/ > /dev/null');
  44. };
  45. // 所有的功能模块
  46. let mods = (() => {
  47. shell.mkdir('-p', 'output');
  48. shell.rm('-rf', 'output/*');
  49. shell.mkdir('-p', 'output/apps');
  50. return shell.ls('apps');
  51. })();
  52. // 动态生成entry
  53. let buildEntry = (globPath) => {
  54. let entries = {}, dirname, basename;
  55. glob.sync(globPath).map(jf => {
  56. dirname = path.dirname(jf);
  57. basename = path.basename(jf, '.js');
  58. entries[path.join(dirname, basename)] = './' + jf;
  59. });
  60. return entries;
  61. };
  62. // 文件拷贝的配置
  63. let copyConfig = (() => {
  64. return mods.map(mod => {
  65. let from = {glob: './apps/' + ((mod === 'manifest.json') ? mod : (mod + '/**/*'))};
  66. return {from: from, to: './'}
  67. });
  68. })();
  69. module.exports = {
  70. entry: buildEntry('apps/**/*.js'),
  71. output: {
  72. path: path.resolve(__dirname, 'output'),
  73. filename: "[name].js",
  74. chunkFilename: '[id].js'
  75. },
  76. optimization: {
  77. minimize: false
  78. },
  79. plugins: [
  80. new CopyWebpackPlugin(copyConfig, {ignore: ['*.js']}),
  81. function () {
  82. this.plugin('done', stats => {
  83. cssCompress();
  84. htmlCompress();
  85. zipForChromeWebStore();
  86. })
  87. }
  88. ]
  89. };