项目目录结构

  1. .
  2. ├── README.md
  3. ├── package.json
  4. ├── postcss.config.js
  5. ├── public
  6. └── index.html
  7. ├── src
  8. ├── app.vue
  9. ├── assets
  10. ├── css
  11. └── index.css
  12. └── image
  13. └── login-form-bg.png
  14. ├── entry.js
  15. ├── router
  16. └── index.js
  17. ├── utils
  18. └── index.js
  19. ├── views
  20. └── index.vue
  21. └── vuex
  22. └── index.js
  23. └── webpack.config.js

Webpack配置

  1. const path = require("path");
  2. const webpack = require("webpack");
  3. const HtmlWebpackPlugin = require('html-webpack-plugin');
  4. const { CleanWebpackPlugin } = require('clean-webpack-plugin');
  5. const VueLoaderPlugin = require('vue-loader/lib/plugin');
  6. const MiniCssExtractPlugin = require('mini-css-extract-plugin');
  7. module.exports = {
  8. entry: "./src/entry.js",
  9. output: {
  10. filename: "js/[name].js",
  11. path: path.resolve(__dirname, "dist")
  12. },
  13. devtool: process.env.NODE_ENV == 'develoption' ? "inline-source-map" : "source-map",
  14. resolve: {
  15. extensions: ['.js', '.vue', '.json'],
  16. alias: {
  17. '@': path.resolve(__dirname, './src')
  18. }
  19. },
  20. module: {
  21. rules: [
  22. {
  23. test: /\.css$/,
  24. use: [
  25. process.env.NODE_ENV !== 'production'
  26. ? 'vue-style-loader'
  27. : MiniCssExtractPlugin.loader
  28. , "style-loader", "css-loader", "postcss-loader"]
  29. },
  30. {
  31. test: /\.less$/,
  32. use: [process.env.NODE_ENV !== 'production'
  33. ? 'vue-style-loader'
  34. : MiniCssExtractPlugin.loader, "css-loader", "less-loader", "postcss-loader"]
  35. },
  36. {
  37. test: /\.(png|svg|jpg|gif)$/,
  38. use: [{
  39. loader: "file-loader",
  40. options: {
  41. outputPath: 'images'
  42. }
  43. }]
  44. },
  45. {
  46. test: /\.(woff|woff2|eot|ttf|otf)$/,
  47. use: [{
  48. loader: "file-loader",
  49. options: {
  50. outputPath: 'fonts'
  51. }
  52. }]
  53. },
  54. {
  55. test: /\.vue$/,
  56. use: [
  57. "vue-loader"
  58. ]
  59. }, {
  60. test: /\.m?js$/,
  61. use: [
  62. "babel-loader"
  63. ]
  64. }
  65. ]
  66. },
  67. devServer: {
  68. contentBase: './dist',
  69. hot: true,
  70. },
  71. plugins: [
  72. new VueLoaderPlugin(),
  73. new CleanWebpackPlugin(),
  74. new HtmlWebpackPlugin({
  75. title: "首页",
  76. filename: "index.html",
  77. template: "./public/index.html"
  78. }),
  79. new MiniCssExtractPlugin({
  80. filename: 'css/[name][chunkhash].css',
  81. chunkFilename: "css/[id][chunkhash].css"
  82. }),
  83. new webpack.HotModuleReplacementPlugin(),
  84. new webpack.optimize.SplitChunksPlugin()
  85. ]
  86. }
  1. //postcss.config.js
  2. module.exports = {
  3. plugins: [
  4. require('autoprefixer')
  5. ]
  6. }
  7. //.babelrc
  8. {
  9. "presets": [
  10. [
  11. "env",
  12. {
  13. "modules": false,
  14. "targets": {
  15. "browsers": [
  16. "> 1%",
  17. "last 2 versions",
  18. "not ie <= 8"
  19. ]
  20. }
  21. }
  22. ]
  23. ]
  24. }