webpack.renderer.config.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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: /\.less$/,
  31. use: ['vue-style-loader', 'css-loader', 'less-loader']
  32. },
  33. {
  34. test: /\.css$/,
  35. use: ['vue-style-loader', 'css-loader']
  36. },
  37. {
  38. test: /\.html$/,
  39. use: 'vue-html-loader'
  40. },
  41. {
  42. test: /\.js$/,
  43. use: 'babel-loader',
  44. exclude: /node_modules/
  45. },
  46. {
  47. test: /\.node$/,
  48. use: 'node-loader'
  49. },
  50. {
  51. test: /\.vue$/,
  52. use: {
  53. loader: 'vue-loader',
  54. options: {
  55. extractCSS: process.env.NODE_ENV === 'production',
  56. loaders: {
  57. sass: 'vue-style-loader!css-loader!sass-loader?indentedSyntax=1',
  58. scss: 'vue-style-loader!css-loader!sass-loader',
  59. less: 'vue-style-loader!css-loader!less-loader'
  60. }
  61. }
  62. }
  63. },
  64. {
  65. test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
  66. use: {
  67. loader: 'url-loader',
  68. query: {
  69. // limit: 10000,
  70. // name: 'imgs/[name]--[folder].[ext]'
  71. // name: 'imgs/[name].[ext]'
  72. name: 'static/Data/[name].[ext]'
  73. }
  74. }
  75. },
  76. {
  77. test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
  78. loader: 'url-loader',
  79. options: {
  80. limit: 10000,
  81. name: 'media/[name]--[folder].[ext]'
  82. }
  83. },
  84. {
  85. test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
  86. use: {
  87. loader: 'url-loader',
  88. query: {
  89. limit: 10000,
  90. name: 'fonts/[name]--[folder].[ext]'
  91. }
  92. }
  93. }
  94. ]
  95. },
  96. node: {
  97. __dirname: process.env.NODE_ENV !== 'production',
  98. __filename: process.env.NODE_ENV !== 'production'
  99. },
  100. plugins: [
  101. new VueLoaderPlugin(),
  102. new MiniCssExtractPlugin({filename: 'styles.css'}),
  103. new HtmlWebpackPlugin({
  104. filename: 'index.html',
  105. template: path.resolve(__dirname, '../src/index.ejs'),
  106. /*templateParameters(compilation, assets, options) {
  107. return {
  108. compilation: compilation,
  109. webpack: compilation.getStats().toJson(),
  110. webpackConfig: compilation.options,
  111. htmlWebpackPlugin: {
  112. files: assets,
  113. options: options
  114. },
  115. process,
  116. };
  117. },*/
  118. minify: {
  119. collapseWhitespace: true,
  120. removeAttributeQuotes: true,
  121. removeComments: true
  122. },
  123. nodeModules: process.env.NODE_ENV !== 'production'
  124. ? path.resolve(__dirname, '../node_modules')
  125. : false
  126. }),
  127. new webpack.HotModuleReplacementPlugin(),
  128. new webpack.NoEmitOnErrorsPlugin()
  129. ],
  130. output: {
  131. filename: '[name].js',
  132. libraryTarget: 'commonjs2',
  133. path: path.join(__dirname, '../dist/electron')
  134. },
  135. resolve: {
  136. alias: {
  137. '@': path.join(__dirname, '../src/renderer'),
  138. 'vue$': 'vue/dist/vue.esm.js',
  139. 'static':path.resolve(__dirname,'../static')
  140. },
  141. extensions: ['.js', '.vue', '.json', '.css', '.node'],
  142. modules: [path.resolve('node_modules'), 'node_modules'],
  143. },
  144. target: 'electron-renderer'
  145. }
  146. /**
  147. * Adjust rendererConfig for development settings
  148. */
  149. if (process.env.NODE_ENV !== 'production') {
  150. rendererConfig.plugins.push(
  151. new webpack.DefinePlugin({
  152. '__static': `"${path.join(__dirname, '../static').replace(/\\/g, '\\\\')}"`
  153. })
  154. )
  155. }
  156. /**
  157. * Adjust rendererConfig for production settings
  158. */
  159. if (process.env.NODE_ENV === 'production') {
  160. rendererConfig.devtool = ''
  161. rendererConfig.plugins.push(
  162. new MinifyPlugin(),
  163. new CopyWebpackPlugin([
  164. /*{
  165. from: path.join(__dirname, '../static'),
  166. to: path.join(__dirname, '../dist/electron/static'),
  167. ignore: ['.*']
  168. }*/
  169. {
  170. from: path.resolve(__dirname, '../static'),
  171. to: 'static',
  172. ignore: ['.*']
  173. }
  174. ]),
  175. new webpack.DefinePlugin({
  176. 'process.env.NODE_ENV': '"production"'
  177. }),
  178. new webpack.LoaderOptionsPlugin({
  179. minimize: true
  180. })
  181. )
  182. }
  183. module.exports = rendererConfig