webpack.config.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* eslint-disable */
  2. const { getConfig, dev } = require('./webpack.config.base');
  3. const { spawn, execSync } = require('child_process');
  4. const CopyPlugin = require('copy-webpack-plugin');
  5. const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
  6. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
  7. let terser = require('terser');
  8. /* eslint-enable */
  9. let electronProcess;
  10. const mainConfig = getConfig({
  11. target: 'electron-main',
  12. devtool: dev ? 'inline-source-map' : false,
  13. watch: dev,
  14. entry: {
  15. main: './src/main',
  16. },
  17. plugins: [
  18. new CopyPlugin({
  19. patterns: [
  20. {
  21. from:
  22. 'node_modules/@cliqz/adblocker-electron-preload/dist/preload.cjs.js',
  23. to: 'preload.js',
  24. transform: async (fileContent, path) => {
  25. return (
  26. await terser.minify(fileContent.toString())
  27. ).code.toString();
  28. },
  29. },
  30. ],
  31. }),
  32. ],
  33. });
  34. const preloadConfig = getConfig({
  35. target: 'web',
  36. devtool: false,
  37. watch: dev,
  38. entry: {
  39. 'view-preload': './src/preloads/view-preload',
  40. },
  41. plugins: [],
  42. });
  43. if (process.env.ENABLE_EXTENSIONS) {
  44. preloadConfig.entry['popup-preload'] = './src/preloads/popup-preload';
  45. preloadConfig.entry['extensions-preload'] =
  46. './src/preloads/extensions-preload';
  47. }
  48. if (process.env.START === '1') {
  49. mainConfig.plugins.push({
  50. apply: (compiler) => {
  51. compiler.hooks.afterEmit.tap('AfterEmitPlugin', () => {
  52. if (electronProcess) {
  53. try {
  54. if (process.platform === 'win32') {
  55. execSync(`taskkill /pid ${electronProcess.pid} /f /t`);
  56. } else {
  57. electronProcess.kill();
  58. }
  59. electronProcess = null;
  60. } catch (e) {}
  61. }
  62. electronProcess = spawn('npm', ['start'], {
  63. shell: true,
  64. env: process.env,
  65. stdio: 'inherit',
  66. });
  67. });
  68. },
  69. });
  70. }
  71. module.exports = [mainConfig, preloadConfig];