webpack.conf.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const HtmlWebpackPlugin = require('html-webpack-plugin');
  2. const WrapperWebpackPlugin = require('wrapper-webpack-plugin');
  3. const base = require('./webpack.base.conf');
  4. const { isProd, INIT_FUNC_NAME } = require('./util');
  5. const MINIFY = isProd && {
  6. collapseWhitespace: true,
  7. removeAttributeQuotes: true,
  8. removeComments: true,
  9. removeOptionalTags: true,
  10. removeRedundantAttributes: true,
  11. removeScriptTypeAttributes: true,
  12. removeStyleLinkTypeAttributes: true,
  13. };
  14. const defaultTemplateOptions = {
  15. minify: MINIFY,
  16. template: 'scripts/template.html',
  17. meta: {
  18. viewport: 'width=device-width,initial-scale=1.0,user-scalable=no',
  19. },
  20. css: [],
  21. js: [],
  22. };
  23. const targets = module.exports = [];
  24. const pages = {
  25. 'browser': {
  26. entry: './src/common/browser',
  27. },
  28. 'background/index': {
  29. entry: './src/background',
  30. html: {},
  31. },
  32. 'options/index': {
  33. entry: './src/options',
  34. html: {
  35. js: [
  36. '/public/lib/zip.js/zip.js',
  37. ],
  38. },
  39. },
  40. 'confirm/index': {
  41. entry: './src/confirm',
  42. html: {},
  43. },
  44. 'popup/index': {
  45. entry: './src/popup',
  46. html: {},
  47. },
  48. injected: {
  49. entry: './src/injected',
  50. },
  51. };
  52. const entries = Object.entries(pages)
  53. .reduce((res, [key, { entry }]) => Object.assign(res, { [key]: entry }), {});
  54. const htmlPlugins = Object.entries(pages)
  55. .map(([key, { html }]) => {
  56. let options;
  57. if (html) {
  58. options = {
  59. filename: `${key}.html`,
  60. chunks: ['browser', key],
  61. ...defaultTemplateOptions,
  62. };
  63. if (typeof html === 'function') {
  64. options = html(options);
  65. } else {
  66. options = {
  67. ...options,
  68. ...html,
  69. };
  70. }
  71. }
  72. if (options) {
  73. return new HtmlWebpackPlugin(options);
  74. }
  75. })
  76. .filter(Boolean);
  77. targets.push({
  78. ...base,
  79. entry: entries,
  80. optimization: {
  81. ...base.optimization,
  82. splitChunks: {
  83. cacheGroups: {
  84. common: {
  85. name: 'common',
  86. minChunks: 2,
  87. chunks(chunk) {
  88. return ![
  89. 'browser',
  90. 'injected',
  91. ].includes(chunk.name);
  92. },
  93. },
  94. },
  95. },
  96. },
  97. plugins: [
  98. ...base.plugins,
  99. ...htmlPlugins,
  100. ],
  101. });
  102. targets.push({
  103. ...base,
  104. entry: {
  105. 'injected-web': './src/injected/web',
  106. },
  107. output: {
  108. ...base.output,
  109. libraryTarget: 'commonjs2',
  110. },
  111. plugins: [
  112. ...base.plugins,
  113. new WrapperWebpackPlugin({
  114. header: `\
  115. window.${INIT_FUNC_NAME} = function () {
  116. var module = { exports: {} };
  117. `,
  118. footer: `
  119. var exports = module.exports;
  120. return exports.__esModule ? exports['default'] : exports;
  121. };0;`,
  122. }),
  123. ],
  124. });