webpack.conf.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. const { modifyWebpackConfig, shallowMerge, defaultOptions } = require('@gera2ld/plaid');
  2. const { isProd } = require('@gera2ld/plaid/util');
  3. const webpack = require('webpack');
  4. const WrapperWebpackPlugin = require('wrapper-webpack-plugin');
  5. const TerserPlugin = require('terser-webpack-plugin');
  6. const projectConfig = require('./plaid.conf');
  7. const mergedConfig = shallowMerge(defaultOptions, projectConfig);
  8. const INIT_FUNC_NAME = 'VMInitInjection';
  9. const definitions = new webpack.DefinePlugin({
  10. 'process.env.INIT_FUNC_NAME': JSON.stringify(INIT_FUNC_NAME),
  11. 'process.env.DEBUG': JSON.stringify(process.env.DEBUG || false),
  12. });
  13. const minimizerOptions = {
  14. cache: true,
  15. parallel: true,
  16. sourceMap: true,
  17. terserOptions: {
  18. output: {
  19. ascii_only: true,
  20. },
  21. },
  22. };
  23. const minimizer = isProd && [
  24. new TerserPlugin({
  25. chunkFilter: ({ name }) => !name.startsWith('public/'),
  26. ...minimizerOptions,
  27. terserOptions: {
  28. ...minimizerOptions.terserOptions,
  29. compress: {
  30. ecma: 6,
  31. // 'safe' since we don't rely on function prototypes
  32. unsafe_arrows: true,
  33. },
  34. },
  35. }),
  36. new TerserPlugin({
  37. chunkFilter: ({ name }) => name.startsWith('public/'),
  38. ...minimizerOptions,
  39. }),
  40. ];
  41. const modify = (extra, init) => modifyWebpackConfig(
  42. (config) => {
  43. config.plugins.push(definitions);
  44. if (init) init(config);
  45. return config;
  46. }, {
  47. projectConfig: {
  48. ...mergedConfig,
  49. ...extra,
  50. optimization: {
  51. ...mergedConfig.optimization,
  52. ...(extra || {}).pages && {
  53. runtimeChunk: false,
  54. splitChunks: false,
  55. },
  56. minimizer,
  57. },
  58. },
  59. },
  60. );
  61. module.exports = Promise.all([
  62. modify(),
  63. modify({
  64. pages: {
  65. injected: {
  66. entry: './src/injected',
  67. },
  68. },
  69. }),
  70. modify({
  71. pages: {
  72. 'injected-web': {
  73. entry: './src/injected/web',
  74. },
  75. },
  76. }, (config) => {
  77. config.output.libraryTarget = 'commonjs2';
  78. config.plugins.push(
  79. new WrapperWebpackPlugin({
  80. header: `\
  81. window.${INIT_FUNC_NAME} = function () {
  82. var module = { exports: {} };
  83. `,
  84. footer: `
  85. var exports = module.exports;
  86. return exports.__esModule ? exports['default'] : exports;
  87. };0;`,
  88. }),
  89. );
  90. }),
  91. ]);