webpack.base.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2017-2020 @polkadot/apps authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. /* eslint-disable @typescript-eslint/camelcase */
  5. const fs = require('fs');
  6. const path = require('path');
  7. const webpack = require('webpack');
  8. const CopyWebpackPlugin = require('copy-webpack-plugin');
  9. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  10. const ENV = process.env.NODE_ENV || 'development';
  11. function createWebpack ({ alias = {}, context, name = 'index' }) {
  12. const pkgJson = require(path.join(context, 'package.json'));
  13. const isProd = ENV === 'production';
  14. const hasPublic = fs.existsSync(path.join(context, 'public'));
  15. const plugins = hasPublic
  16. ? [new CopyWebpackPlugin([{ from: 'public' }])]
  17. : [];
  18. // disabled, smooths dev load, was -
  19. // isProd ? 'source-map' : 'cheap-eval-source-map',
  20. return {
  21. context,
  22. entry: [
  23. '@babel/polyfill',
  24. `./src/${name}.tsx`,
  25. isProd
  26. ? null
  27. : null // 'webpack-plugin-serve/client'
  28. ].filter((entry) => entry),
  29. mode: ENV,
  30. module: {
  31. rules: [
  32. {
  33. exclude: /(node_modules)/,
  34. test: /\.css$/,
  35. use: [
  36. isProd
  37. ? MiniCssExtractPlugin.loader
  38. : require.resolve('style-loader'),
  39. {
  40. loader: require.resolve('css-loader'),
  41. options: {
  42. importLoaders: 1
  43. }
  44. }
  45. ]
  46. },
  47. {
  48. include: /node_modules/,
  49. test: /\.css$/,
  50. use: [
  51. isProd
  52. ? MiniCssExtractPlugin.loader
  53. : require.resolve('style-loader'),
  54. require.resolve('css-loader')
  55. ]
  56. },
  57. {
  58. exclude: /(node_modules)/,
  59. test: /\.(js|ts|tsx)$/,
  60. use: [
  61. require.resolve('thread-loader'),
  62. {
  63. loader: require.resolve('babel-loader'),
  64. options: require('@polkadot/dev/config/babel')
  65. }
  66. ]
  67. },
  68. {
  69. test: /\.md$/,
  70. use: [
  71. require.resolve('html-loader'),
  72. require.resolve('markdown-loader')
  73. ]
  74. },
  75. {
  76. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  77. use: [
  78. {
  79. loader: require.resolve('url-loader'),
  80. options: {
  81. esModule: false,
  82. limit: 10000,
  83. name: 'static/[name].[hash:8].[ext]'
  84. }
  85. }
  86. ]
  87. },
  88. {
  89. test: [/\.eot$/, /\.ttf$/, /\.svg$/, /\.woff$/, /\.woff2$/],
  90. use: [
  91. {
  92. loader: require.resolve('file-loader'),
  93. options: {
  94. esModule: false,
  95. name: 'static/[name].[hash:8].[ext]'
  96. }
  97. }
  98. ]
  99. }
  100. ]
  101. },
  102. node: {
  103. child_process: 'empty',
  104. dgram: 'empty',
  105. fs: 'empty',
  106. net: 'empty',
  107. tls: 'empty'
  108. },
  109. optimization: {
  110. runtimeChunk: 'single',
  111. splitChunks: {
  112. cacheGroups: {
  113. polkadotJs: {
  114. chunks: 'initial',
  115. enforce: true,
  116. name: 'polkadotjs',
  117. test: /node_modules\/(@polkadot\/wasm-crypto)/
  118. },
  119. vendorOther: {
  120. chunks: 'initial',
  121. enforce: true,
  122. name: 'vendor',
  123. test: /node_modules\/(asn1|bn\.js|buffer|cuint|elliptic|lodash|moment|readable-stream|rxjs)/
  124. },
  125. vendorReact: {
  126. chunks: 'initial',
  127. enforce: true,
  128. name: 'react',
  129. test: /node_modules\/(chart|i18next|react|semantic-ui)/
  130. }
  131. }
  132. }
  133. },
  134. output: {
  135. chunkFilename: '[name].[chunkhash:8].js',
  136. filename: '[name].[hash:8].js',
  137. globalObject: '(typeof self !== \'undefined\' ? self : this)',
  138. path: path.join(context, 'build'),
  139. publicPath: ''
  140. },
  141. performance: {
  142. hints: false
  143. },
  144. plugins: plugins.concat([
  145. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  146. new webpack.DefinePlugin({
  147. 'process.env': {
  148. NODE_ENV: JSON.stringify(ENV),
  149. VERSION: JSON.stringify(pkgJson.version),
  150. WS_URL: JSON.stringify(process.env.WS_URL)
  151. }
  152. }),
  153. new webpack.optimize.SplitChunksPlugin(),
  154. new MiniCssExtractPlugin({
  155. filename: '[name].[contenthash:8].css'
  156. })
  157. ]).filter((plugin) => plugin),
  158. resolve: {
  159. alias,
  160. extensions: ['.js', '.jsx', '.ts', '.tsx']
  161. },
  162. watch: !isProd,
  163. watchOptions: {
  164. ignored: ['.yarn', /build/, /node_modules/]
  165. }
  166. };
  167. }
  168. module.exports = createWebpack;