webpack.renderer.config.js 4.6 KB

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