webpack.conf.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. if (options.inlineSource) options.inject = false;
  74. return new HtmlWebpackPlugin(options);
  75. }
  76. })
  77. .filter(Boolean);
  78. targets.push({
  79. ...base,
  80. entry: entries,
  81. optimization: {
  82. ...base.optimization,
  83. splitChunks: {
  84. cacheGroups: {
  85. common: {
  86. name: 'common',
  87. minChunks: 2,
  88. chunks(chunk) {
  89. return ![
  90. 'browser',
  91. 'injected',
  92. ].includes(chunk.name);
  93. },
  94. },
  95. },
  96. },
  97. },
  98. plugins: [
  99. ...base.plugins,
  100. ...htmlPlugins,
  101. ],
  102. });
  103. targets.push({
  104. ...base,
  105. entry: {
  106. 'injected-web': './src/injected/web',
  107. },
  108. output: {
  109. ...base.output,
  110. libraryTarget: 'commonjs2',
  111. },
  112. plugins: [
  113. ...base.plugins,
  114. new WrapperWebpackPlugin({
  115. header: `\
  116. window.${INIT_FUNC_NAME} = function () {
  117. var module = { exports: {} };
  118. `,
  119. footer: `
  120. var exports = module.exports;
  121. return exports.__esModule ? exports['default'] : exports;
  122. };0;`,
  123. }),
  124. ],
  125. });