webpack.config.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. const webpack = require("webpack");
  2. const path = require("path");
  3. const HtmlWebpackPlugin = require("html-webpack-plugin");
  4. const ExtractTextPlugin = require("extract-text-webpack-plugin");
  5. const autoprefixer = require("autoprefixer");
  6. const nodeEnv = process.env.NODE_ENV || "development";
  7. const plugins = [
  8. new webpack.optimize.CommonsChunkPlugin({
  9. name: "commons",
  10. filename: "[name]-[hash].js",
  11. chunks: ["vendor", "app"]
  12. }),
  13. new HtmlWebpackPlugin({
  14. hash: true,
  15. chunks: ["commons", "app"],
  16. template: "index.tpl.html",
  17. inject: "body",
  18. filename: "index.html"
  19. }),
  20. new webpack.optimize.OccurrenceOrderPlugin(),
  21. new webpack.HotModuleReplacementPlugin(),
  22. new webpack.NoEmitOnErrorsPlugin(),
  23. new webpack.DefinePlugin({
  24. "process.env": {
  25. NODE_ENV: JSON.stringify(nodeEnv),
  26. },
  27. }),
  28. new webpack.NamedModulesPlugin(),
  29. new webpack.LoaderOptionsPlugin({
  30. options: {
  31. postcss: [
  32. autoprefixer({
  33. browsers: [
  34. "last 3 version",
  35. "ie >= 10",
  36. ],
  37. }),
  38. ],
  39. context: path.join(__dirname, "./app"),
  40. },
  41. }),
  42. ];
  43. const rules = [
  44. {
  45. test: /\.(js|jsx)$/,
  46. exclude: /node_modules/,
  47. use: [
  48. "babel-loader",
  49. ],
  50. },
  51. {
  52. test: /\.(png|gif|jpg|svg)$/,
  53. include: path.join(__dirname, "./app/assets/images"),
  54. use: "url-loader?limit=20480&name=assets/[name]-[hash].[ext]",
  55. },
  56. ];
  57. if (nodeEnv === "production") {
  58. plugins.push(
  59. new webpack.LoaderOptionsPlugin({
  60. minimize: true,
  61. debug: false,
  62. }),
  63. new webpack.optimize.UglifyJsPlugin({
  64. compress: {
  65. warnings: false,
  66. screw_ie8: true,
  67. conditionals: true,
  68. unused: true,
  69. comparisons: true,
  70. sequences: true,
  71. dead_code: true,
  72. evaluate: true,
  73. if_return: true,
  74. join_vars: true,
  75. },
  76. output: {
  77. comments: false,
  78. },
  79. }),
  80. new ExtractTextPlugin("style-[hash].css")
  81. );
  82. rules.push({
  83. test: /\.scss$/,
  84. loader: ExtractTextPlugin.extract({
  85. fallback: "style-loader",
  86. use: "css-loader!postcss-loader!sass-loader",
  87. })
  88. });
  89. } else {
  90. rules.push({
  91. test: /\.scss$/,
  92. exclude: /node_modules/,
  93. use: [
  94. "style-loader",
  95. // "css-loader?sourceMap",
  96. "css-loader",
  97. "postcss-loader",
  98. "sass-loader?sourceMap",
  99. ]
  100. });
  101. }
  102. module.exports = {
  103. devtool: nodeEnv === "production" ? "eval" : "source-map",
  104. context: path.join(__dirname, "./app"),
  105. entry: {
  106. app: "./js/index.js",
  107. vendor: [
  108. "babel-polyfill",
  109. "es6-promise",
  110. "immutable",
  111. "isomorphic-fetch",
  112. "react-dom",
  113. "react-redux",
  114. "react-router",
  115. "react",
  116. "redux-thunk",
  117. "redux",
  118. ],
  119. },
  120. output: {
  121. path: path.join(__dirname, "./dist"),
  122. publicPath: "/",
  123. filename: "[name]-[hash].js",
  124. },
  125. module: {
  126. rules,
  127. },
  128. resolve: {
  129. extensions: [".webpack-loader.js", ".web-loader.js", ".loader.js", ".js", ".jsx"],
  130. modules: [
  131. path.resolve(__dirname, "node_modules"),
  132. path.join(__dirname, "./app/js"),
  133. ],
  134. },
  135. plugins
  136. };