这两天摆弄webpack,躺过很多坑,直到今天看了一位博主的文章才得以解决。他对配置中的各个部分做说明。

下面的配置99.9%抄自博主: https://www.cnblogs.com/nianyifenzhizuo/p/10271001.html

安装package.json中的node-sass可能因为网络原因不能成功安装

报错信息有一大串,其中提及python和gyp,其实不用去安装他们。只要执行下面的命令:

  1. npm set sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
  1. npm i node-sass -D

 以下为一个vue项目的基本配置:

项目根目录:

  1. {
  2. "name": "shop",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "clean": "rimraf dist",
  8. "build": "cross-env NODE_ENV=production webpack --config ./build/webpack.prod.conf.js",
  9. "dev": "cross-env NODE_ENV=development webpack-dev-server --config ./build/webpack.dev.conf.js",
  10. "start": "npm run clean && npm run build"
  11. },
  12. "author": "asd",
  13. "license": "ISC",
  14. "devDependencies": {
  15. "@babel/core": "^7.2.2",
  16. "@babel/plugin-transform-runtime": "^7.2.0",
  17. "autoprefixer": "^9.4.6",
  18. "babel-loader": "^8.0.5",
  19. "babel-plugin-transform-remove-console": "^6.9.4",
  20. "clean-webpack-plugin": "^1.0.1",
  21. "cross-env": "^5.2.0",
  22. "css-loader": "^2.1.0",
  23. "file-loader": "^3.0.1",
  24. "html-webpack-plugin": "^3.2.0",
  25. "mini-css-extract-plugin": "^0.5.0",
  26. "node-sass": "^4.11.0",
  27. "optimize-css-assets-webpack-plugin": "^5.0.1",
  28. "postcss-loader": "^3.0.0",
  29. "purify-css": "^1.2.5",
  30. "purifycss-webpack": "^0.7.0",
  31. "rimraf": "^2.6.3",
  32. "sass-loader": "^7.1.0",
  33. "style-loader": "^0.23.1",
  34. "uglifyjs-webpack-plugin": "^2.1.1",
  35. "url-loader": "^1.1.2",
  36. "vue-loader": "^15.6.2",
  37. "vue-style-loader": "^4.1.2",
  38. "vue-template-compiler": "^2.5.22",
  39. "webpack": "^4.29.0",
  40. "webpack-cli": "^3.2.1",
  41. "webpack-dev-server": "^3.1.14",
  42. "webpack-merge": "^4.2.1"
  43. },
  44. "dependencies": {
  45. "@babel/preset-env": "^7.3.1",
  46. "@babel/runtime": "^7.3.1",
  47. "axios": "^0.18.0",
  48. "body-parser": "^1.18.3",
  49. "cookie-parser": "^1.4.4",
  50. "express": "^4.16.4",
  51. "glob-all": "^3.1.0",
  52. "mongoose": "^5.4.11",
  53. "vue": "^2.5.22",
  54. "vue-lazyload": "^1.2.6",
  55. "vue-router": "^3.0.2"
  56. }
  57. }

package.json

  1. module.exports = {
  2. loader: 'postcss-loader',
  3. plugins: [
  4. require('autoprefixer')
  5. ]
  6. }

postcss.config.js

  1. {
  2. "presets": [
  3. [
  4. "@babel/preset-env",
  5. {
  6. "modules": false
  7. }
  8. ]
  9. ],
  10. "plugins": [
  11. "@babel/plugin-transform-runtime", [
  12. "transform-remove-console",
  13. {
  14. "include": ["error", "warn"]
  15. }
  16. ]
  17. ]
  18. }

.babelrc

项目根目录=>build文件夹

  1. const webpack = require('webpack')
  2. const { resolve }= require('path')
  3. const VueLoaderPlugin = require('vue-loader/lib/plugin')
  4. const HtmlWebpackPlugin = require('html-webpack-plugin')
  5. const MiniCssExtractPlugin = require('mini-css-extract-plugin')
  6.  
  7. const isDev = process.env.NODE_ENV === 'production'
  8.  
  9. let pluginsConfig = [
  10. new VueLoaderPlugin(),
  11. new HtmlWebpackPlugin({
  12. title: 'my App',
  13. template: resolve(__dirname, '../src/index.html')
  14. }),
  15. new webpack.DefinePlugin({
  16. 'process.env': {
  17. 'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
  18. }
  19. })
  20. ]
  21. if (!isDev) {
  22. pluginsConfig = pluginsConfig.concat(new MiniCssExtractPlugin({
  23. filename: "css/[name]_[contenthash].css"
  24. }))
  25. }
  26.  
  27. module.exports = {
  28. mode: process.env.NODE_ENV || 'production',
  29. entry: resolve(__dirname, '../src/index.js'),
  30. output: {
  31. filename: 'bundle.js',
  32. path: resolve(__dirname, '../dist')
  33. },
  34. resolve: {
  35. //引入时可以省略后缀
  36. extensions: ['.js', '.vue', '.json'],
  37. alias: {
  38. 'vue$': 'vue/dist/vue.esm.js',
  39. //@就了代表了src文件夹所在路径
  40. '@': resolve('src'),
  41. }
  42. },
  43. module: {
  44. rules: [{
  45. test: /\.vue$/,
  46. loader: 'vue-loader'
  47. },
  48. {
  49. test: /\.scss$/,
  50. use: [
  51. isDev ? 'vue-style-loader' : MiniCssExtractPlugin.loader,
  52. {
  53. loader: 'css-loader',
  54. options: {
  55. modules: true,
  56. localIdentName: '[local]_[hash:base64:8]'
  57. }
  58. },
  59. 'sass-loader'
  60. ]
  61. },
  62. {
  63. test: /\.(png|jpg|gif)$/,
  64. loader: 'url-loader?name=images/[name]-[contenthash:5].[ext]&limit=2000'
  65. },
  66. {
  67. test: /\.js$/,
  68. loader: 'babel-loader?cacheDirectory',
  69. exclude: '/node_modules/',
  70. include: resolve(__dirname, '../src')
  71. }
  72. ]
  73. },
  74. plugins: pluginsConfig,
  75. }

webpack.base.confg.js

  1. const webpack = require('webpack')
  2. const path = require('path')
  3. const WebPackBaseConfig = require('./webpack.base.conf.js')
  4. const CleanWebpackPlugin = require('clean-webpack-plugin')
  5. const WebPackMerge = require('webpack-merge')
  6.  
  7. module.exports = WebPackMerge(WebPackBaseConfig, {
  8. devtool: 'inline-source-map',
  9. devServer: {
  10. contentBase: path.join(__dirname, 'dist'), //告诉服务器从哪个目录中提供内容
  11. compress: true, //启用 gzip 压缩
  12. port: 9000, //端口号
  13. host: '0.0.0.0', //默认是 localhost。如果希望服务器外部可访问,则设置为0.0.0.0
  14. hot: true //启用热替换模块 必须配置 webpack.HotModuleReplacementPlugin
  15. },
  16. plugins: [
  17. new CleanWebpackPlugin(['dist']), //清理文件夹
  18. new webpack.HotModuleReplacementPlugin(), //启用热替换模块
  19. new webpack.NamedModulesPlugin() //启用HMR时,插件将显示模块的相对路径
  20. ]
  21. })

webpack.dev.conf.js

  1. const webpack = require('webpack')
  2. const path = require('path')
  3. const WebPackMerge = require('webpack-merge')
  4. const WebPackBaseConfig = require('./webpack.base.conf.js')
  5. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  6. const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin')
  7. const glob = require('glob-all')
  8. const PurifyCSSPlugin = require('purifycss-webpack')
  9.  
  10. module.exports = WebPackMerge(WebPackBaseConfig, {
  11. output: {
  12. filename: '[name].js',
  13. chunkFilename: '[name].[chunkhash:5].js',
  14. path: path.resolve(__dirname, 'dist')
  15. },
  16. optimization: {
  17. splitChunks: {
  18. cacheGroups: {
  19. commons: {
  20. test: /[\\/]node_modules[\\/]/,
  21. name: 'vendors',
  22. chunks: 'all'
  23. }
  24. }
  25. },
  26. runtimeChunk: true,
  27. minimizer: [
  28. new UglifyJsPlugin({
  29. cache: true,
  30. parallel: true
  31. }),
  32. new OptimizeCSSAssetsPlugin()
  33. ]
  34. },
  35. plugins: [
  36. new PurifyCSSPlugin({
  37. paths: glob.sync([
  38. path.join(__dirname, '../src/')
  39. ]),
  40. purifyOptions: {
  41. whitelist: ['*purify*']
  42. }
  43. }),
  44. ]
  45. })

webpack.prod.conf.js

 

项目根目录=>src文件夹

  1. import Vue from 'vue'
  2. import router from './router/index.js'
  3.  
  4. new Vue({
  5. el: '#app',
  6. router
  7. })

index.js

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset='utf-8'>
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="renderer" content="webkit|ie-comp|ie-stand">
  7. <title></title>
  8. </head>
  9. <body>
  10. <div id="app" v-cloak>
  11. <router-view></router-view>
  12. </div>
  13. </body>
  14. </html>

index.html

  1. <template>
  2. <div id="app">
  3. <img src="./assets/logo.png">
  4. <router-view/>
  5. </div>
  6. </template>
  7.  
  8. <script>
  9. export default {
  10. name: 'App'
  11. }
  12. </script>
  13.  
  14. <style>
  15. #app {
  16. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  17. -webkit-font-smoothing: antialiased;
  18. -moz-osx-font-smoothing: grayscale;
  19. text-align: center;
  20. color: #2c3e50;
  21. margin-top: 60px;
  22. }
  23. </style>

App.vue

项目根目录=>src文件夹=>router文件夹

  1. import Vue from 'vue'
  2. import Router from 'vue-router'
  3. import HelloWorld from '@/components/HelloWorld'
  4.  
  5. Vue.use(Router)
  6.  
  7. export default new Router({
  8. routes: [
  9. {
  10. path: '/',
  11. name: 'HelloWorld',
  12. component: HelloWorld
  13. }
  14. ]
  15. })

index.js

项目根目录=>src文件夹=>components文件夹

  1. <template>
  2. <div class="hello">
  3. <h1>{{ msg }}</h1>
  4. <h2>Essential Links</h2>
  5. <ul>
  6. <li>
  7. <a
  8. href="https://vuejs.org"
  9. target="_blank"
  10. >
  11. Core Docs
  12. </a>
  13. </li>
  14. <li>
  15. <a
  16. href="https://forum.vuejs.org"
  17. target="_blank"
  18. >
  19. Forum
  20. </a>
  21. </li>
  22. <li>
  23. <a
  24. href="https://chat.vuejs.org"
  25. target="_blank"
  26. >
  27. Community Chat
  28. </a>
  29. </li>
  30. <li>
  31. <a
  32. href="https://twitter.com/vuejs"
  33. target="_blank"
  34. >
  35. Twitter
  36. </a>
  37. </li>
  38. <br>
  39. <li>
  40. <a
  41. href="http://vuejs-templates.github.io/webpack/"
  42. target="_blank"
  43. >
  44. Docs for This Template
  45. </a>
  46. </li>
  47. </ul>
  48. <h2>Ecosystem</h2>
  49. <ul>
  50. <li>
  51. <a
  52. href="http://router.vuejs.org/"
  53. target="_blank"
  54. >
  55. vue-router
  56. </a>
  57. </li>
  58. <li>
  59. <a
  60. href="http://vuex.vuejs.org/"
  61. target="_blank"
  62. >
  63. vuex
  64. </a>
  65. </li>
  66. <li>
  67. <a
  68. href="http://vue-loader.vuejs.org/"
  69. target="_blank"
  70. >
  71. vue-loader
  72. </a>
  73. </li>
  74. <li>
  75. <a
  76. href="https://github.com/vuejs/awesome-vue"
  77. target="_blank"
  78. >
  79. awesome-vue
  80. </a>
  81. </li>
  82. </ul>
  83. </div>
  84. </template>
  85.  
  86. <script>
  87. export default {
  88. name: 'HelloWorld',
  89. data () {
  90. return {
  91. msg: 'Welcome to Your Vue.js App'
  92. }
  93. }
  94. }
  95. </script>
  96.  
  97. <!-- Add "scoped" attribute to limit CSS to this component only -->
  98. <style scoped, lang="sass" src='@/assets/css/admin.scss'></style>

HelloWorld.vue

项目根目录=>src文件夹=>assets文件夹=>css
  1. h1, h2 {
  2. font-weight: normal;
  3. }
  4. ul {
  5. list-style-type: none;
  6. padding: 0;
  7. }
  8. li {
  9. display: inline-block;
  10. margin: 0 10px;
  11. }
  12.  
  13. $link-color: #ff6000;
  14. a {
  15. color: $link_color;
  16. }

admin.scss

vue项目的webpack4.X配置的更多相关文章

  1. 基于Vue cli生成的Vue项目的webpack4升级

    前面的话 本文将详细介绍从webpack3到webpack4的升级过程 概述 相比于webpack3,webpack4可以零配置运行,打包速度比之前提高了90%,可以直接到ES6的代码进行无用代码剔除 ...

  2. webstorm如何调试vue项目的js

    webstorm如何调试vue项目的js webstormvuewebstorm调试jsjs 1.编辑调试配置,新建JavaScript调试配置,并设置要访问的url地址,如下图所示: 在URL处填写 ...

  3. vue项目的mode:history模式

    最近做的Vue + Vue-Router + Webpack +minitUI项目碰到的问题,在此记录一下,Vue-router 中有hash模式和history模式,vue的路由默认是hash模式, ...

  4. maven(四):一个基本maven项目的pom.xml配置

    继续之前创建的test项目,一个基本项目的pom.xml文件,通常至少有三个部分 第一部分,项目坐标,信息描述等 <modelVersion>4.0.0</modelVersion& ...

  5. vue 项目的I18n国际化之路

    I18n (internationalization ) ---未完善 产品国际化是产品后期维护及推广中重要的一环,通过国际化操作使得产品能更好适应不同语言和地区的需求 国际化重点:1. 语言语言本地 ...

  6. Maven项目的pom.xml配置文件格式初识

    Maven项目 有pom.xml文件的项目就已经是一个maven项目了,但是还没有被maven托管,我们需要将该项目添加为maven项目 <project xmlns="http:// ...

  7. java新项目的eclipse统一配置记录

    1.new java file的模版 /** * @Title:${file_name} * @Copyright: Copyright (c) 2016 * @Description: * < ...

  8. 56.关于vue项目的seo问题

    不可否定的是,vue现在火.但是在实际项目中,特别是像一下交互网站,我们不可避免会考虑到的是seo问题,这直接关系到我们网站的排名,很多人说用vue搭建的网站不能做优化,那我们真的要放弃vue,放弃前 ...

  9. Vue项目的npm环境搭建

    Vue项目的环境搭建主要步骤如下: vue项目创建 安装NodeJS +到官网下载自己系统对应的版本,这里我们下载Windows系统的64位zip文件,下载完成后解压,可以看到里面有一个node.ex ...

随机推荐

  1. win10下CorelDRAW菜单栏字体变成白色了怎么办?

    相信很多同学安装了win10系统之后,你的 CDR 菜单栏就变了,变得没有任何内容,以白色显示,win10系统与CorelDRAW早期的版本兼容方面存在问题,(CorelDRAW x7/X8无此问题出 ...

  2. 密信(MeSince),将取代传统电子邮件

    电子邮件发展至今已经有几十年的历史,但仍然是最重要的现代互联网应用之一.在全球范围内,每小时发送的非垃圾邮件数量超过30亿封,从工作场景的使用到个人生活,电子邮件都扮演着不可或缺的角色.但是由于明文电 ...

  3. Use gdb attach pid and debug it

  4. HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )

    链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...

  5. 代理上网环境配置docker私有库

    最后更新时间:2018年12月27日 Docker使用代理上网去 pull 各类 images,需要做如下配置: 创建目录: /etc/systemd/system/docker.service.d ...

  6. javascript 中一些奇葩的日期换算

    1.获取今天的0时0分0秒(常用于开始日期的获取) new Date(new Date().toLocaleDateString()); // Mon Nov 12 2018 00:00:00 GMT ...

  7. Python-基础-day4

    深浅copy 1.先看赋值运算 h1 = [1,2,3,['aihuidi','hhhh']] h2 = h1 h1[0] = 111 print(h1) print(h2) #结果: # [111, ...

  8. pytorch 6 build_nn_quickly 快速搭建神经网络

    import torch import torch.nn.functional as F # replace following class code with an easy sequential ...

  9. Windows桌面美化

    [工具链接]链接: https://pan.baidu.com/s/12aUGsu91F8WfaW5HU5ps3g 提取码: dnan [样例] [美化步骤] 1.解压下载文件,安装两个软件: Sta ...

  10. js获得 json对象的个数(长度)

    function getJsonObjLength(jsonObj) { var Length = 0; for (var item in jsonObj) { Length++; } return ...