Guest User

webpack.prod

a guest
Jan 27th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. const fs = require("fs");
  2. const path = require("path");
  3. const CleanWebpackPlugin = require("clean-webpack-plugin");
  4. const CopyWebpackPlugin = require("copy-webpack-plugin");
  5. const HtmlWebpackPlugin = require("html-webpack-plugin");
  6. const MiniCssExtractPlugin = require("mini-css-extract-plugin");
  7. const FaviconsWebpackPlugin = require("favicons-webpack-plugin");
  8. const ImageminPlugin = require('imagemin-webpack-plugin');
  9.  
  10.  
  11. const generateHtmlPlugins = (templateDir) => {
  12. const templateFiles = fs.readdirSync(path.resolve(__dirname, templateDir));
  13. return templateFiles.map(item => {
  14. const parts = item.split(".");
  15. const name = parts[0];
  16. const extension = parts[1];
  17. return new HtmlWebpackPlugin({
  18. filename: `${name}.html`,
  19. template: path.resolve(__dirname, `${templateDir}/${name}.${extension}`),
  20. inject: "body",
  21. });
  22. });
  23. }
  24.  
  25. const productionConfig = {
  26. mode: "production",
  27. entry: [
  28. path.resolve(__dirname, "src/js/index.js"),
  29. path.resolve(__dirname, "src/styles/index.sass"),
  30. ],
  31. output: {
  32. path: path.resolve(__dirname, "dist"),
  33. filename: "js/bundle.[contenthash].js",
  34. },
  35. module: {
  36. rules: [
  37. {
  38. test: /\.pug$/,
  39. use: "pug-loader"
  40. },
  41. {
  42. test: /\.js$/,
  43. include: path.resolve(__dirname, "src/js"),
  44. exclude: /node_modules/,
  45. use: {
  46. loader: "babel-loader",
  47. options: {
  48. presets: ["@babel/preset-env"],
  49. }
  50. }
  51. },
  52. {
  53. test: /\.(png|jpg|gif|svg)$/,
  54. use: [
  55. {
  56. loader: "file-loader",
  57. options: { name: "img/[name].[ext]" }
  58. }
  59. ]
  60. },
  61. {
  62. test: /\.sass$/,
  63. include: path.resolve(__dirname, "src/styles"),
  64. use: [
  65. MiniCssExtractPlugin.loader,
  66. "css-loader",
  67. {
  68. loader: "postcss-loader",
  69. options: {
  70. plugins: (loader) => [
  71. require("postcss-flexbugs-fixes"),
  72. require("autoprefixer")({
  73. flexbox: "no-2009",
  74. grid: "autoplace",
  75. }),
  76. require("postcss-font-magician")({
  77. hosted: [
  78. path.resolve(__dirname, "src/assets/fonts")
  79. ],
  80. }),
  81. require("cssnano")({
  82. preset: ["default", {
  83. discardComments: {
  84. removeAll: true,
  85. },
  86. }]
  87. }),
  88. ],
  89. sourceMap: false,
  90. },
  91. },
  92. {
  93. loader: "resolve-url-loader"
  94. },
  95. "sass-loader"
  96. ]
  97. }
  98. ]
  99. },
  100. plugins: [
  101. new CleanWebpackPlugin("dist"),
  102. new CopyWebpackPlugin([
  103. {
  104. from: "./src/assets/fonts",
  105. to: "./fonts"
  106. },
  107. {
  108. from: "./src/assets/icons",
  109. to: "./img"
  110. },
  111. {
  112. from: "./src/assets/images",
  113. to: "./img"
  114. },
  115. ]),
  116. ...generateHtmlPlugins("src/html/pages"),
  117. new MiniCssExtractPlugin({
  118. filename: "css/styles.[contenthash].css",
  119. chunkFilename: "css/[id].[contenthash].css"
  120. }),
  121. new FaviconsWebpackPlugin({
  122. logo: path.resolve(__dirname, "src/assets/icons/favicon.png"),
  123. prefix: "icons/",
  124. emitStats: false,
  125. statsFilename: "favicon_stats.json",
  126. persistentCache: true,
  127. inject: true,
  128. background: "#fff",
  129. title: "Site template",
  130. icons: {
  131. android: true,
  132. appleIcon: true,
  133. appleStartup: true,
  134. coast: true,
  135. favicons: true,
  136. firefox: true,
  137. opengraph: true,
  138. twitter: true,
  139. yandex: true,
  140. windows: true
  141. }
  142. }),
  143. new ImageminPlugin({
  144. test: /\.(jpe?g|png|gif|svg)$/i,
  145. pngquant: {
  146. quality: '95-100'
  147. }
  148. })
  149. ]
  150. }
  151.  
  152. module.exports = productionConfig;
Advertisement
Add Comment
Please, Sign In to add comment