webpack.config.base.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /* eslint-disable */
  2. const { resolve, join } = require('path');
  3. const { writeFileSync, readFileSync, existsSync } = require('fs');
  4. const { merge } = require('webpack-merge');
  5. const webpack = require('webpack');
  6. const HtmlWebpackPlugin = require('html-webpack-plugin');
  7. const createStyledComponentsTransformer = require('typescript-plugin-styled-components')
  8. .default;
  9. const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
  10. const TerserPlugin = require('terser-webpack-plugin');
  11. const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin');
  12. /* eslint-enable */
  13. const INCLUDE = resolve(__dirname, 'src');
  14. const BUILD_FLAGS = {
  15. ENABLE_EXTENSIONS: true,
  16. ENABLE_AUTOFILL: false,
  17. };
  18. process.env = {
  19. ...process.env,
  20. ...BUILD_FLAGS,
  21. };
  22. const dev = process.env.DEV === '1';
  23. process.env.NODE_ENV = dev ? 'development' : 'production';
  24. const styledComponentsTransformer = createStyledComponentsTransformer({
  25. minify: !dev,
  26. displayName: dev,
  27. });
  28. const config = {
  29. mode: dev ? 'development' : 'production',
  30. devtool: dev ? 'eval-source-map' : false,
  31. output: {
  32. path: resolve(__dirname, 'build'),
  33. filename: '[name].bundle.js',
  34. },
  35. module: {
  36. rules: [
  37. {
  38. test: /\.(png|gif|jpg|woff2|ttf|svg)$/,
  39. include: INCLUDE,
  40. use: [
  41. {
  42. loader: 'file-loader',
  43. options: {
  44. esModule: false,
  45. outputPath: 'res',
  46. },
  47. },
  48. ],
  49. },
  50. {
  51. test: /\.tsx|ts$/,
  52. use: [
  53. {
  54. loader: 'ts-loader',
  55. options: {
  56. experimentalWatchApi: dev,
  57. transpileOnly: true, // |dev| to throw CI when a ts error occurs
  58. getCustomTransformers: () => ({
  59. before: [styledComponentsTransformer],
  60. }),
  61. },
  62. },
  63. ],
  64. include: INCLUDE,
  65. },
  66. ],
  67. },
  68. node: {
  69. __dirname: false,
  70. __filename: false,
  71. },
  72. resolve: {
  73. modules: ['node_modules'],
  74. extensions: ['.js', '.jsx', '.tsx', '.ts', '.json'],
  75. alias: {
  76. '~': INCLUDE,
  77. },
  78. plugins: [new TsconfigPathsPlugin()],
  79. },
  80. plugins: [
  81. new webpack.EnvironmentPlugin(['NODE_ENV', ...Object.keys(BUILD_FLAGS)]),
  82. ],
  83. externals: {
  84. keytar: `require('keytar')`,
  85. electron: 'require("electron")',
  86. fs: 'require("fs")',
  87. os: 'require("os")',
  88. path: 'require("path")',
  89. },
  90. optimization: {
  91. minimizer: !dev
  92. ? [
  93. new TerserPlugin({
  94. extractComments: true,
  95. terserOptions: {
  96. ecma: 8,
  97. output: {
  98. comments: false,
  99. },
  100. },
  101. parallel: true,
  102. }),
  103. ]
  104. : [],
  105. },
  106. };
  107. if (dev) {
  108. config.module.rules[1].use.splice(0, 0, {
  109. loader: 'babel-loader',
  110. options: { plugins: ['react-refresh/babel'] },
  111. });
  112. }
  113. function getConfig(...cfg) {
  114. return merge(config, ...cfg);
  115. }
  116. const getHtml = (name) => {
  117. return new HtmlWebpackPlugin({
  118. title: 'Wexond',
  119. template: 'static/pages/app.html',
  120. filename: `${name}.html`,
  121. chunks: [name],
  122. });
  123. };
  124. const applyEntries = (config, entries) => {
  125. for (const entry of entries) {
  126. config.entry[entry] = [
  127. `./src/renderer/pre-entry`,
  128. `./src/renderer/views/${entry}`,
  129. ];
  130. config.plugins.push(getHtml(entry));
  131. }
  132. };
  133. const getBaseConfig = (name) => {
  134. const config = {
  135. plugins: [],
  136. output: {},
  137. entry: {},
  138. optimization: {
  139. runtimeChunk: {
  140. name: `runtime.${name}`,
  141. },
  142. splitChunks: {
  143. chunks: 'all',
  144. maxInitialRequests: Infinity,
  145. },
  146. },
  147. };
  148. return config;
  149. };
  150. module.exports = { getConfig, dev, getHtml, applyEntries, getBaseConfig };