webpack.base.config.js 7.0 KB

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