webpack.renderer.config.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. 'use strict'
  2. process.env.BABEL_ENV = 'renderer'
  3. const path = require('path')
  4. const {
  5. dependencies
  6. } = require('../package.json')
  7. const webpack = require('webpack')
  8. const MinifyPlugin = require("babel-minify-webpack-plugin")
  9. const CopyWebpackPlugin = require('copy-webpack-plugin')
  10. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  11. const HtmlWebpackPlugin = require('html-webpack-plugin')
  12. const {
  13. VueLoaderPlugin
  14. } = require('vue-loader')
  15. /**
  16. * List of node_modules to include in webpack bundle
  17. *
  18. * Required for specific packages like Vue UI libraries
  19. * that provide pure *.vue files that need compiling
  20. * https://simulatedgreg.gitbooks.io/electron-vue/content/en/webpack-configurations.html#white-listing-externals
  21. */
  22. let whiteListedModules = ['vue', 'element-ui','vue-router']
  23. let rendererConfig = {
  24. devtool: '#cheap-module-eval-source-map',
  25. entry: {
  26. renderer: path.join(__dirname, '../src/renderer/main.js')
  27. },
  28. externals: [
  29. ...Object.keys(dependencies || {}).filter(d => !whiteListedModules.includes(d))
  30. ],
  31. module: {
  32. rules: [
  33. // {
  34. // test: /\.(js|vue)$/,
  35. // enforce: 'pre',
  36. // exclude: /node_modules/,
  37. // use: {
  38. // loader: 'eslint-loader',
  39. // options: {
  40. // formatter: require('eslint-friendly-formatter')
  41. // }
  42. // }
  43. // },
  44. {
  45. test: /\.scss$/,
  46. use: ['vue-style-loader', 'css-loader', 'sass-loader']
  47. },
  48. {
  49. test: /\.sass$/,
  50. use: ['vue-style-loader', 'css-loader', 'sass-loader?indentedSyntax']
  51. },
  52. {
  53. test: /\.less$/,
  54. use: ['vue-style-loader', 'css-loader', 'less-loader']
  55. },
  56. {
  57. test: /\.css$/,
  58. use: ['vue-style-loader', 'css-loader']
  59. },
  60. {
  61. test: /\.html$/,
  62. use: 'vue-html-loader'
  63. },
  64. {
  65. test: /\.js$/,
  66. use: 'babel-loader',
  67. exclude: /node_modules/
  68. },
  69. {
  70. test: /\.node$/,
  71. use: 'node-loader'
  72. },
  73. {
  74. test: /\.vue$/,
  75. use: {
  76. loader: 'vue-loader',
  77. options: {
  78. extractCSS: process.env.NODE_ENV === 'production',
  79. loaders: {
  80. sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax=1',
  81. scss: 'vue-style-loader!css-loader!sass-loader',
  82. less: 'vue-style-loader!css-loader!less-loader'
  83. }
  84. }
  85. }
  86. },
  87. {
  88. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  89. use: {
  90. loader: 'url-loader',
  91. query: {
  92. limit: 10000,
  93. name: 'imgs/[name]--[folder].[ext]'
  94. }
  95. }
  96. },
  97. {
  98. test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
  99. loader: 'url-loader',
  100. options: {
  101. limit: 10000,
  102. name: 'media/[name]--[folder].[ext]'
  103. }
  104. },
  105. {
  106. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  107. use: {
  108. loader: 'url-loader',
  109. query: {
  110. limit: 10000,
  111. name: 'fonts/[name]--[folder].[ext]'
  112. }
  113. }
  114. }
  115. ]
  116. },
  117. node: {
  118. __dirname: process.env.NODE_ENV !== 'production',
  119. __filename: process.env.NODE_ENV !== 'production'
  120. },
  121. plugins: [
  122. new VueLoaderPlugin(),
  123. new MiniCssExtractPlugin({
  124. filename: 'styles.css'
  125. }),
  126. new HtmlWebpackPlugin({
  127. filename: 'index.html',
  128. template: path.resolve(__dirname, '../src/index.ejs'),
  129. templateParameters(compilation, assets, options) {
  130. return {
  131. compilation: compilation,
  132. webpack: compilation.getStats().toJson(),
  133. webpackConfig: compilation.options,
  134. htmlWebpackPlugin: {
  135. files: assets,
  136. options: options,
  137. },
  138. process,
  139. };
  140. },
  141. minify: {
  142. collapseWhitespace: true,
  143. removeAttributeQuotes: true,
  144. removeComments: true
  145. },
  146. nodeModules: process.env.NODE_ENV !== 'production' ?
  147. path.resolve(__dirname, '../node_modules') : false
  148. //nodeModules: path.resolve(__dirname, '../node_modules')
  149. }),
  150. new webpack.NoEmitOnErrorsPlugin()
  151. ],
  152. output: {
  153. filename: '[name].js',
  154. libraryTarget: 'commonjs2',
  155. path: path.join(__dirname, '../dist/electron')
  156. },
  157. resolve: {
  158. alias: {
  159. '@': path.join(__dirname, '../src/renderer'),
  160. 'vue$': 'vue/dist/vue.esm.js'
  161. },
  162. extensions: ['.js', '.vue', '.json', '.css', '.node']
  163. },
  164. target: 'electron-renderer'
  165. }
  166. /**
  167. * Adjust rendererConfig for development settings
  168. */
  169. if (process.env.NODE_ENV !== 'production') {
  170. rendererConfig.plugins.push(
  171. new webpack.HotModuleReplacementPlugin(),
  172. new webpack.DefinePlugin({
  173. '__static': `"${path.join(__dirname, '../static').replace(/\\/g, '\\\\')}"`
  174. })
  175. )
  176. }
  177. /**
  178. * Adjust rendererConfig for production settings
  179. */
  180. if (process.env.NODE_ENV === 'production') {
  181. rendererConfig.devtool = ''
  182. rendererConfig.plugins.push(
  183. new MinifyPlugin(),
  184. new CopyWebpackPlugin([{
  185. from: path.join(__dirname, '../static'),
  186. to: path.join(__dirname, '../dist/electron/static'),
  187. ignore: ['.*']
  188. }]),
  189. new webpack.DefinePlugin({
  190. 'process.env.NODE_ENV': '"production"'
  191. }),
  192. new webpack.LoaderOptionsPlugin({
  193. minimize: true
  194. })
  195. )
  196. }
  197. module.exports = rendererConfig