webpack.base.config.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 isLive = !(process.env.IS_LIVE === 'false' || process.env.IS_LIVE === false);
  27. const hasPublic = fs.existsSync(path.join(context, 'public'));
  28. const plugins = hasPublic
  29. ? [new CopyWebpackPlugin({ patterns: [{ from: 'public' }] })]
  30. : [];
  31. !isProd && plugins.push(
  32. new WebpackPluginServe({
  33. hmr: false, // switch off, Chrome WASM memory leak
  34. liveReload: false, // explict off, overrides hmr
  35. port: 3000,
  36. progress: false, // since we have hmr off, disable
  37. static: path.join(process.cwd(), '/build')
  38. })
  39. );
  40. const alias = findPackages().reduce((alias, { dir, name }) => {
  41. alias[name] = path.resolve(context, `../${dir}/src`);
  42. return alias;
  43. }, {});
  44. // Add @joystream/types as alias to automatically process any changes:
  45. alias['@joystream/types'] = path.resolve(context, '../../../types/src');
  46. return {
  47. context,
  48. // Make it quicker if we're not in a LIVE mode
  49. entry: !isLive ? './src/notLive.ts' : ['@babel/polyfill', './src/index.tsx'],
  50. mode: ENV,
  51. module: {
  52. rules: [
  53. {
  54. exclude: /(node_modules)/,
  55. test: /\.css$/,
  56. use: [
  57. isProd
  58. ? MiniCssExtractPlugin.loader
  59. : require.resolve('style-loader'),
  60. {
  61. loader: require.resolve('css-loader'),
  62. options: {
  63. importLoaders: 1
  64. }
  65. }
  66. ]
  67. },
  68. {
  69. test: /\.s[ac]ss$/i,
  70. use: [
  71. // Creates `style` nodes from JS strings
  72. 'style-loader',
  73. // Translates CSS into CommonJS
  74. 'css-loader',
  75. // Compiles Sass to CSS
  76. 'sass-loader'
  77. ]
  78. },
  79. {
  80. include: /node_modules/,
  81. test: /\.css$/,
  82. use: [
  83. isProd
  84. ? MiniCssExtractPlugin.loader
  85. : require.resolve('style-loader'),
  86. require.resolve('css-loader')
  87. ]
  88. },
  89. {
  90. exclude: /(node_modules)/,
  91. test: /\.(js|ts|tsx)$/,
  92. use: [
  93. require.resolve('thread-loader'),
  94. {
  95. loader: require.resolve('babel-loader'),
  96. options: require('@polkadot/dev/config/babel')
  97. }
  98. ]
  99. },
  100. {
  101. test: /\.md$/,
  102. use: [
  103. require.resolve('html-loader'),
  104. require.resolve('markdown-loader')
  105. ]
  106. },
  107. {
  108. // Original config had "exclude: [/semantic-ui-css/]"
  109. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  110. use: [
  111. {
  112. loader: require.resolve('url-loader'),
  113. options: {
  114. esModule: false,
  115. limit: 10000,
  116. name: 'static/[name].[hash:8].[ext]'
  117. }
  118. }
  119. ]
  120. },
  121. {
  122. // Original config had "exclude: [/semantic-ui-css/]", because Semantic UI Icons
  123. // are not used in polkadot-js/apps repository, but they are used in ours
  124. test: [/\.eot$/, /\.ttf$/, /\.svg$/, /\.woff$/, /\.woff2$/],
  125. use: [
  126. {
  127. loader: require.resolve('file-loader'),
  128. options: {
  129. esModule: false,
  130. name: 'static/[name].[hash:8].[ext]'
  131. }
  132. }
  133. ]
  134. }
  135. ]
  136. },
  137. node: {
  138. child_process: 'empty',
  139. dgram: 'empty',
  140. fs: 'empty',
  141. net: 'empty',
  142. tls: 'empty'
  143. },
  144. optimization: {
  145. runtimeChunk: 'single',
  146. splitChunks: {
  147. cacheGroups: {
  148. ...mapChunks('polkadot', [
  149. /* 00 */ /node_modules\/@polkadot\/(wasm)/,
  150. /* 01 */ /node_modules\/(@polkadot\/(api|metadata|rpc|types))/,
  151. /* 02 */ /node_modules\/(@polkadot\/(extension|keyring|react|ui|util|vanitygen)|@acala-network|@edgeware|@ledgerhq|@open-web3|@zondax|edgeware)/
  152. ]),
  153. ...mapChunks('react', [
  154. /* 00 */ /node_modules\/(@fortawesome)/,
  155. /* 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)/
  156. ]),
  157. ...mapChunks('other', [
  158. /* 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)/,
  159. /* 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)/,
  160. /* 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)/
  161. ])
  162. }
  163. }
  164. },
  165. output: {
  166. chunkFilename: '[name].[chunkhash:8].js',
  167. filename: '[name].[hash:8].js',
  168. globalObject: '(typeof self !== \'undefined\' ? self : this)',
  169. path: path.join(context, 'build'),
  170. publicPath: ''
  171. },
  172. performance: {
  173. hints: false
  174. },
  175. plugins: plugins.concat([
  176. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  177. new webpack.DefinePlugin({
  178. 'process.env': {
  179. NODE_ENV: JSON.stringify(ENV),
  180. VERSION: JSON.stringify(pkgJson.version),
  181. WS_URL: JSON.stringify(process.env.WS_URL)
  182. }
  183. }),
  184. new webpack.optimize.SplitChunksPlugin(),
  185. new MiniCssExtractPlugin({
  186. filename: '[name].[contenthash:8].css'
  187. })
  188. ]).filter((plugin) => plugin),
  189. resolve: {
  190. alias,
  191. extensions: ['.js', '.jsx', '.ts', '.tsx']
  192. },
  193. watch: !isProd,
  194. watchOptions: {
  195. ignored: ['.yarn', /build/, /node_modules/]
  196. }
  197. };
  198. }
  199. module.exports = createWebpack;