webpack.base.config.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. test: /\.s[ac]ss$/i,
  66. use: [
  67. // Creates `style` nodes from JS strings
  68. 'style-loader',
  69. // Translates CSS into CommonJS
  70. 'css-loader',
  71. // Compiles Sass to CSS
  72. 'sass-loader'
  73. ]
  74. },
  75. {
  76. include: /node_modules/,
  77. test: /\.css$/,
  78. use: [
  79. isProd
  80. ? MiniCssExtractPlugin.loader
  81. : require.resolve('style-loader'),
  82. require.resolve('css-loader')
  83. ]
  84. },
  85. {
  86. exclude: /(node_modules)/,
  87. test: /\.(js|ts|tsx)$/,
  88. use: [
  89. require.resolve('thread-loader'),
  90. {
  91. loader: require.resolve('babel-loader'),
  92. options: require('@polkadot/dev/config/babel')
  93. }
  94. ]
  95. },
  96. {
  97. test: /\.md$/,
  98. use: [
  99. require.resolve('html-loader'),
  100. require.resolve('markdown-loader')
  101. ]
  102. },
  103. {
  104. // Original config had "exclude: [/semantic-ui-css/]"
  105. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  106. use: [
  107. {
  108. loader: require.resolve('url-loader'),
  109. options: {
  110. esModule: false,
  111. limit: 10000,
  112. name: 'static/[name].[hash:8].[ext]'
  113. }
  114. }
  115. ]
  116. },
  117. {
  118. // Original config had "exclude: [/semantic-ui-css/]", because Semantic UI Icons
  119. // are not used in polkadot-js/apps repository, but they are used in ours
  120. test: [/\.eot$/, /\.ttf$/, /\.svg$/, /\.woff$/, /\.woff2$/],
  121. use: [
  122. {
  123. loader: require.resolve('file-loader'),
  124. options: {
  125. esModule: false,
  126. name: 'static/[name].[hash:8].[ext]'
  127. }
  128. }
  129. ]
  130. }
  131. ]
  132. },
  133. node: {
  134. child_process: 'empty',
  135. dgram: 'empty',
  136. fs: 'empty',
  137. net: 'empty',
  138. tls: 'empty'
  139. },
  140. optimization: {
  141. runtimeChunk: 'single',
  142. splitChunks: {
  143. cacheGroups: {
  144. ...mapChunks('polkadot', [
  145. /* 00 */ /node_modules\/@polkadot\/(wasm)/,
  146. /* 01 */ /node_modules\/(@polkadot\/(api|metadata|rpc|types))/,
  147. /* 02 */ /node_modules\/(@polkadot\/(extension|keyring|react|ui|util|vanitygen)|@acala-network|@edgeware|@ledgerhq|@open-web3|@zondax|edgeware)/
  148. ]),
  149. ...mapChunks('react', [
  150. /* 00 */ /node_modules\/(@fortawesome)/,
  151. /* 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)/
  152. ]),
  153. ...mapChunks('other', [
  154. /* 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)/,
  155. /* 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)/,
  156. /* 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)/
  157. ])
  158. }
  159. }
  160. },
  161. output: {
  162. chunkFilename: '[name].[chunkhash:8].js',
  163. filename: '[name].[hash:8].js',
  164. globalObject: '(typeof self !== \'undefined\' ? self : this)',
  165. path: path.join(context, 'build'),
  166. publicPath: ''
  167. },
  168. performance: {
  169. hints: false
  170. },
  171. plugins: plugins.concat([
  172. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  173. new webpack.DefinePlugin({
  174. 'process.env': {
  175. NODE_ENV: JSON.stringify(ENV),
  176. VERSION: JSON.stringify(pkgJson.version),
  177. WS_URL: JSON.stringify(process.env.WS_URL)
  178. }
  179. }),
  180. new webpack.optimize.SplitChunksPlugin(),
  181. new MiniCssExtractPlugin({
  182. filename: '[name].[contenthash:8].css'
  183. })
  184. ]).filter((plugin) => plugin),
  185. resolve: {
  186. alias,
  187. extensions: ['.js', '.jsx', '.ts', '.tsx']
  188. },
  189. watch: !isProd,
  190. watchOptions: {
  191. ignored: ['.yarn', /build/, /node_modules/]
  192. }
  193. };
  194. }
  195. module.exports = createWebpack;