webpack.base.config.js 6.6 KB

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