webpack.renderer.config.js 4.7 KB

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