webpack.base.config.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 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 { WebpackPluginServe } = require('webpack-plugin-serve');
  11. const findPackages = require('../../scripts/findPackages');
  12. function createWebpack (ENV, context) {
  13. const pkgJson = require(path.join(context, 'package.json'));
  14. const isProd = ENV === 'production';
  15. const hasPublic = fs.existsSync(path.join(context, 'public'));
  16. const plugins = hasPublic
  17. ? [new CopyWebpackPlugin({ patterns: [{ from: 'public' }] })]
  18. : [];
  19. !isProd && plugins.push(
  20. new WebpackPluginServe({
  21. hmr: false, // switch off, Chrome WASM memory leak
  22. liveReload: false, // explict off, overrides hmr
  23. port: 3000,
  24. progress: false, // since we have hmr off, disable
  25. static: path.join(process.cwd(), '/build')
  26. })
  27. );
  28. const alias = findPackages().reduce((alias, { dir, name }) => {
  29. alias[name] = path.resolve(context, `../${dir}/src`);
  30. return alias;
  31. }, {});
  32. return {
  33. context,
  34. entry: ['@babel/polyfill', './src/index.tsx'],
  35. mode: ENV,
  36. module: {
  37. rules: [
  38. {
  39. exclude: /(node_modules)/,
  40. test: /\.css$/,
  41. use: [
  42. isProd
  43. ? MiniCssExtractPlugin.loader
  44. : require.resolve('style-loader'),
  45. {
  46. loader: require.resolve('css-loader'),
  47. options: {
  48. importLoaders: 1
  49. }
  50. }
  51. ]
  52. },
  53. {
  54. include: /node_modules/,
  55. test: /\.css$/,
  56. use: [
  57. isProd
  58. ? MiniCssExtractPlugin.loader
  59. : require.resolve('style-loader'),
  60. require.resolve('css-loader')
  61. ]
  62. },
  63. {
  64. exclude: /(node_modules)/,
  65. test: /\.(js|ts|tsx)$/,
  66. use: [
  67. require.resolve('thread-loader'),
  68. {
  69. loader: require.resolve('babel-loader'),
  70. options: require('@polkadot/dev/config/babel')
  71. }
  72. ]
  73. },
  74. {
  75. test: /\.md$/,
  76. use: [
  77. require.resolve('html-loader'),
  78. require.resolve('markdown-loader')
  79. ]
  80. },
  81. {
  82. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  83. use: [
  84. {
  85. loader: require.resolve('url-loader'),
  86. options: {
  87. esModule: false,
  88. limit: 10000,
  89. name: 'static/[name].[hash:8].[ext]'
  90. }
  91. }
  92. ]
  93. },
  94. {
  95. test: [/\.eot$/, /\.ttf$/, /\.svg$/, /\.woff$/, /\.woff2$/],
  96. use: [
  97. {
  98. loader: require.resolve('file-loader'),
  99. options: {
  100. esModule: false,
  101. name: 'static/[name].[hash:8].[ext]'
  102. }
  103. }
  104. ]
  105. }
  106. ]
  107. },
  108. node: {
  109. child_process: 'empty',
  110. dgram: 'empty',
  111. fs: 'empty',
  112. net: 'empty',
  113. tls: 'empty'
  114. },
  115. optimization: {
  116. runtimeChunk: 'single',
  117. splitChunks: {
  118. cacheGroups: {
  119. polkadotJsApi: {
  120. chunks: 'initial',
  121. enforce: true,
  122. name: 'polkadotjs.libs',
  123. test: /node_modules\/(@ledgerhq|@zondax|edgeware|@polkadot\/(api|extension|keyring|metadata|react|rpc|types|ui|util|vanitygen))/
  124. },
  125. polkadotJsWasm: {
  126. chunks: 'initial',
  127. enforce: true,
  128. name: 'polkadotjs.wasm',
  129. test: /node_modules\/@polkadot\/(wasm)/
  130. },
  131. reactIcons: {
  132. chunks: 'initial',
  133. enforce: true,
  134. name: 'react.fa',
  135. test: /node_modules\/(@fortawesome)/
  136. },
  137. reactSUI: {
  138. chunks: 'initial',
  139. enforce: true,
  140. name: 'react.sui',
  141. test: /node_modules\/(@semantic-ui-react|@stardust|classnames|keyboard-key|prop-types|semantic-ui-react|semantic-ui-css)/
  142. },
  143. reactUI: {
  144. chunks: 'initial',
  145. enforce: true,
  146. name: 'react.ui',
  147. test: /node_modules\/(@emotion|chart\.js|codeflask|copy-to-clipboard|create-react|file-selector|file-saver|hoist-non-react|i18next|jdenticon|mini-create-react|popper\.js|qrcode-generator|react|react-|remark-parse|styled-components)/
  148. },
  149. vendor01: {
  150. chunks: 'initial',
  151. enforce: true,
  152. name: 'other.01',
  153. test: /node_modules\/(@babel|ansi-styles|asn1|browserify-|chalk|color|color-|crypto-browserify|des\.js|diffie-hellman|elliptic|event-emitter|events|eventemitter3|hash|hmac-drbg|js-sha3|lodash|md5|memoizee|miller-rabin|object-|path-|parse-asn1|pbkdf2|process|public-encrypt|query-string|ripemd160|readable-stream|regenerator-runtime|rtcpeerconnection-shim|stream-browserify|store|timers-browserify|tslib|unified|unist-util|util|vfile|vm-browserify|webrtc-adapter|whatwg-fetch|xxhashjs)/
  154. },
  155. vendor02: {
  156. chunks: 'initial',
  157. enforce: true,
  158. name: 'other.02',
  159. test: /node_modules\/(attr-accept|base-x|base64-js|blakejs|bip39|bip66|bn\.js|brorand|buffer|camelcase|cipher-base|core-js|core-util|create-|cuint|decode-uri|deep-equal|define-properties|detect-browser|es-abstract|es5-ext|es6-symbol|extend|function-bind|has-symbols|history|html-parse|ieee754|ip-|is-|minimalistic-crypto-utils|moment|next-tick|node-libs-browser|randombytes|randomfill|regexp|rxjs|safe-buffer|scheduler|sdp|secp256k1|setimmediate|sha\.js|through)/
  160. }
  161. }
  162. }
  163. },
  164. output: {
  165. chunkFilename: '[name].[chunkhash:8].js',
  166. filename: '[name].[hash:8].js',
  167. globalObject: '(typeof self !== \'undefined\' ? self : this)',
  168. path: path.join(context, 'build'),
  169. publicPath: ''
  170. },
  171. performance: {
  172. hints: false
  173. },
  174. plugins: plugins.concat([
  175. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  176. new webpack.DefinePlugin({
  177. 'process.env': {
  178. NODE_ENV: JSON.stringify(ENV),
  179. VERSION: JSON.stringify(pkgJson.version),
  180. WS_URL: JSON.stringify(process.env.WS_URL)
  181. }
  182. }),
  183. new webpack.optimize.SplitChunksPlugin(),
  184. new MiniCssExtractPlugin({
  185. filename: '[name].[contenthash:8].css'
  186. })
  187. ]).filter((plugin) => plugin),
  188. resolve: {
  189. alias,
  190. extensions: ['.js', '.jsx', '.ts', '.tsx']
  191. },
  192. watch: !isProd,
  193. watchOptions: {
  194. ignored: ['.yarn', /build/, /node_modules/]
  195. }
  196. };
  197. }
  198. module.exports = createWebpack;