webpack.config.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2017-2018 @polkadot/apps authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the ISC 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 packages = ['app-accounts', 'app-addresses', 'app-explorer', 'app-extrinsics', 'app-rpc', 'app-storage', 'app-toolbox', 'app-vanitygen', 'ui-app', 'ui-identicon', 'ui-keyring', 'ui-react-rx', 'ui-react', 'ui-signer'];
  11. function createWebpack ({ alias = {}, context, name = 'index' }) {
  12. const pkgJson = require(path.join(context, 'package.json'));
  13. const ENV = process.env.NODE_ENV || 'development';
  14. const isProd = ENV === 'production';
  15. const hasPublic = fs.existsSync(path.join(context, 'public'));
  16. const plugins = hasPublic
  17. ? [new CopyWebpackPlugin([{ from: 'public' }])]
  18. : [];
  19. return {
  20. context,
  21. devtool: isProd ? 'source-map' : 'cheap-eval-source-map',
  22. entry: `./src/${name}.tsx`,
  23. mode: ENV,
  24. output: {
  25. path: path.join(context, 'build'),
  26. filename: `[name].[hash:8].js`,
  27. chunkFilename: `[name].[chunkhash:8].js`
  28. },
  29. resolve: {
  30. alias,
  31. extensions: ['.js', '.jsx', '.ts', '.tsx']
  32. },
  33. module: {
  34. rules: [
  35. {
  36. test: /\.css$/,
  37. exclude: /(node_modules)/,
  38. use: [
  39. isProd
  40. ? MiniCssExtractPlugin.loader
  41. : require.resolve('style-loader'),
  42. {
  43. loader: require.resolve('css-loader'),
  44. options: {
  45. importLoaders: 1
  46. }
  47. },
  48. {
  49. loader: require.resolve('postcss-loader'),
  50. options: {
  51. ident: 'postcss',
  52. plugins: () => [
  53. require('precss'),
  54. require('autoprefixer'),
  55. require('postcss-simple-vars'),
  56. require('postcss-nested'),
  57. require('postcss-import'),
  58. require('postcss-clean')(),
  59. require('postcss-flexbugs-fixes')
  60. ]
  61. }
  62. }
  63. ]
  64. },
  65. {
  66. test: /\.css$/,
  67. include: /node_modules/,
  68. use: [
  69. isProd
  70. ? MiniCssExtractPlugin.loader
  71. : require.resolve('style-loader'),
  72. require.resolve('css-loader')
  73. ]
  74. },
  75. {
  76. test: /\.(js|ts|tsx)$/,
  77. exclude: /(node_modules)/,
  78. use: [
  79. require.resolve('thread-loader'),
  80. {
  81. loader: require.resolve('babel-loader'),
  82. options: require('@polkadot/dev-react/config/babel')
  83. }
  84. ]
  85. },
  86. {
  87. test: [/\.bmp$/, /\.gif$/, /\.jpe?g$/, /\.png$/],
  88. use: [
  89. {
  90. loader: require.resolve('url-loader'),
  91. options: {
  92. limit: 10000,
  93. name: 'static/[name].[hash:8].[ext]'
  94. }
  95. }
  96. ]
  97. },
  98. {
  99. test: [/\.eot$/, /\.ttf$/, /\.svg$/, /\.woff$/, /\.woff2$/],
  100. use: [
  101. {
  102. loader: require.resolve('file-loader'),
  103. options: {
  104. name: 'static/[name].[hash:8].[ext]'
  105. }
  106. }
  107. ]
  108. }
  109. ]
  110. },
  111. node: {
  112. child_process: 'empty',
  113. dgram: 'empty',
  114. fs: 'empty',
  115. net: 'empty',
  116. tls: 'empty'
  117. },
  118. optimization: {
  119. runtimeChunk: 'single',
  120. splitChunks: {
  121. cacheGroups: {
  122. vendor: {
  123. chunks: 'initial',
  124. enforce: true,
  125. name: 'vendor',
  126. test: /node_modules\/(bn\.js|i18next|lodash|react|semantic-ui)/
  127. }
  128. }
  129. }
  130. },
  131. performance: {
  132. hints: false
  133. },
  134. plugins: plugins.concat([
  135. new webpack.DefinePlugin({
  136. 'process.env': {
  137. NODE_ENV: JSON.stringify(ENV),
  138. VERSION: JSON.stringify(pkgJson.version),
  139. WS_URL: JSON.stringify(process.env.WS_URL)
  140. }
  141. }),
  142. new HtmlWebpackPlugin({
  143. inject: true,
  144. template: path.join(context, `${hasPublic ? 'public/' : ''}${name}.html`)
  145. }),
  146. new webpack.optimize.SplitChunksPlugin(),
  147. new MiniCssExtractPlugin({
  148. filename: `[name].[contenthash:8].css`
  149. })
  150. ])
  151. };
  152. }
  153. module.exports = createWebpack({
  154. context: __dirname,
  155. alias: packages.reduce((alias, pkg) => {
  156. alias[`@polkadot/${pkg}`] = path.resolve(__dirname, `../${pkg}/src`);
  157. return alias;
  158. }, {})
  159. });