webpack.config.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2017-2019 @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. const fs = require('fs');
  5. const path = require('path');
  6. const webpack = require('webpack');
  7. const CopyWebpackPlugin = require('copy-webpack-plugin');
  8. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  9. const HtmlWebpackPlugin = require('html-webpack-plugin');
  10. const { WebpackPluginServe } = require('webpack-plugin-serve');
  11. const packages = [
  12. 'app-accounts',
  13. 'app-addresses',
  14. 'app-democracy',
  15. 'app-explorer',
  16. 'app-extrinsics',
  17. 'app-js',
  18. 'app-settings',
  19. 'app-staking',
  20. 'app-storage',
  21. 'app-123code',
  22. 'app-toolbox',
  23. 'app-transfer',
  24. 'ui-api',
  25. 'ui-app',
  26. 'ui-params',
  27. 'ui-reactive',
  28. 'ui-signer'
  29. ];
  30. const DEFAULT_THEME = process.env.TRAVIS_BRANCH === 'next'
  31. ? 'substrate'
  32. : 'polkadot';
  33. function createWebpack ({ alias = {}, context, name = 'index' }) {
  34. const pkgJson = require(path.join(context, 'package.json'));
  35. const ENV = process.env.NODE_ENV || 'development';
  36. const isProd = ENV === 'production';
  37. const hasPublic = fs.existsSync(path.join(context, 'public'));
  38. const plugins = hasPublic
  39. ? [new CopyWebpackPlugin([{ from: 'public' }])]
  40. : [];
  41. return {
  42. context,
  43. devtool: isProd ? 'source-map' : 'cheap-eval-source-map',
  44. entry: [
  45. `./src/${name}.tsx`,
  46. isProd
  47. ? null
  48. : 'webpack-plugin-serve/client'
  49. ].filter((entry) => entry),
  50. mode: ENV,
  51. output: {
  52. chunkFilename: `[name].[chunkhash:8].js`,
  53. filename: `[name].[hash:8].js`,
  54. globalObject: `(typeof self !== 'undefined' ? self : this)`,
  55. path: path.join(context, 'build')
  56. },
  57. resolve: {
  58. alias,
  59. extensions: ['.js', '.jsx', '.ts', '.tsx']
  60. },
  61. module: {
  62. rules: [
  63. {
  64. test: /\.css$/,
  65. exclude: /(node_modules)/,
  66. use: [
  67. isProd
  68. ? MiniCssExtractPlugin.loader
  69. : require.resolve('style-loader'),
  70. {
  71. loader: require.resolve('css-loader'),
  72. options: {
  73. importLoaders: 1
  74. }
  75. },
  76. {
  77. loader: require.resolve('postcss-loader'),
  78. options: {
  79. ident: 'postcss',
  80. plugins: () => [
  81. require('precss'),
  82. require('autoprefixer'),
  83. require('postcss-simple-vars'),
  84. require('postcss-nested'),
  85. require('postcss-import'),
  86. require('postcss-clean')(),
  87. require('postcss-flexbugs-fixes')
  88. ]
  89. }
  90. }
  91. ]
  92. },
  93. {
  94. test: /\.css$/,
  95. include: /node_modules/,
  96. use: [
  97. isProd
  98. ? MiniCssExtractPlugin.loader
  99. : require.resolve('style-loader'),
  100. require.resolve('css-loader')
  101. ]
  102. },
  103. {
  104. test: /\.(js|ts|tsx)$/,
  105. exclude: /(node_modules)/,
  106. use: [
  107. require.resolve('thread-loader'),
  108. {
  109. loader: require.resolve('babel-loader'),
  110. options: require('@polkadot/dev-react/config/babel')
  111. }
  112. ]
  113. },
  114. {
  115. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  116. use: [
  117. {
  118. loader: require.resolve('url-loader'),
  119. options: {
  120. limit: 10000,
  121. name: 'static/[name].[hash:8].[ext]'
  122. }
  123. }
  124. ]
  125. },
  126. {
  127. test: [/\.eot$/, /\.ttf$/, /\.svg$/, /\.woff$/, /\.woff2$/],
  128. use: [
  129. {
  130. loader: require.resolve('file-loader'),
  131. options: {
  132. name: 'static/[name].[hash:8].[ext]'
  133. }
  134. }
  135. ]
  136. }
  137. ]
  138. },
  139. node: {
  140. child_process: 'empty',
  141. dgram: 'empty',
  142. fs: 'empty',
  143. net: 'empty',
  144. tls: 'empty'
  145. },
  146. optimization: {
  147. runtimeChunk: 'single',
  148. splitChunks: {
  149. cacheGroups: {
  150. vendorOther: {
  151. chunks: 'initial',
  152. enforce: true,
  153. name: 'vendor',
  154. test: /node_modules\/(asn1|bn\.js|buffer|cuint|elliptic|lodash|moment|readable-stream|rxjs)/
  155. },
  156. vendorReact: {
  157. chunks: 'initial',
  158. enforce: true,
  159. name: 'react',
  160. test: /node_modules\/(chart|i18next|react|semantic-ui)/
  161. },
  162. vendorSodium: {
  163. chunks: 'initial',
  164. enforce: true,
  165. name: 'sodium',
  166. test: /node_modules\/(libsodium)/
  167. }
  168. }
  169. }
  170. },
  171. performance: {
  172. hints: false
  173. },
  174. plugins: plugins.concat([
  175. isProd
  176. ? null
  177. : new WebpackPluginServe({
  178. port: 3000,
  179. static: path.join(process.cwd(), '/build')
  180. }),
  181. new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
  182. new webpack.DefinePlugin({
  183. 'process.env': {
  184. NODE_ENV: JSON.stringify(ENV),
  185. VERSION: JSON.stringify(pkgJson.version),
  186. WS_URL: JSON.stringify(process.env.WS_URL)
  187. }
  188. }),
  189. new HtmlWebpackPlugin({
  190. inject: true,
  191. template: path.join(context, `${hasPublic ? 'public/' : ''}${name}.html`),
  192. PAGE_TITLE: 'Polkadot/Substrate Portal'
  193. }),
  194. new webpack.optimize.SplitChunksPlugin(),
  195. new MiniCssExtractPlugin({
  196. filename: `[name].[contenthash:8].css`
  197. })
  198. ]).filter((entry) => entry),
  199. watch: !isProd
  200. };
  201. }
  202. module.exports = createWebpack({
  203. context: __dirname,
  204. alias: packages.reduce((alias, pkg) => {
  205. alias[`@polkadot/${pkg}`] = path.resolve(__dirname, `../${pkg}/src`);
  206. return alias;
  207. }, {})
  208. });