vue-cli中chainWebpack的使用

  1. npx webpack --watch // 热编译
  2. npx webpack-dev-Server //启动服务
  3. npx webpack-dev-Server --open //启动服务,并打开浏览器
  4. npx webpack --env production --env goal=local //编译的结果中有编译是输入的信息,通过这些信息确定使用那个环境(这时的配置文件需要导出一个函数,通过函数返回配置对象)
  5. npx webpack --env production //生产环境下打包
  6. npx webpack --env development //开发模式打包
  7. npx webpack -c ./comfig/webpack.config.dev.js //打包,并指定运行那个配置文件,当前是开发环境
  8. npm install --save-dev html-webpack-plugin // 自动引入文件
  9. npm i webpack-dev-Server -D //自动刷新
  10. npm i webpack webpack-cli --save--dev
  11. npm i less-loader less -D //解析less,scss
  12. npm i css-loader -D //解析css(或是由less-loader解析过less文件)
  13. npm i style-loader -D // 将css-loader解析后的文件放到网页上
  14. npm i csv-loader xml-loader -D //xml csv/tsv文件解析
  15. npm i toml yaml json5 -D //解析toml、yaml、json5文件
  16. npm i -D babel-loader @babel/core @babel/preset-env
  17. // babel-loader 解析es6为低版本es语法,适配浏览器
  18. // @babel/core babel核心模块
  19. //@babel/preset-env babel预设,一组babel插件集合
  20. npm i @babel/runtime -D // 包含regeneratorRuntime运行时需要
  21. npm i @babel/plugin-transform-runtime -D //使regeneratorRuntime在被需要时,自动导入,编译时需要
  22. // 解析.vue文件
  23. npm i --save-dev vue-loader-plugin
  24. npm i -D vue-loader
  25. npm i mini-css-extract-plugin -D //抽离文件
  26. npm i css-minimizer-webpack-plugin -D //压缩文件
  27. npm i terser-webpack-plugin -D //压缩代码
  1. //提示找不到webpack 运行下面代码
  2. npm link webpack
  3. // 合并配置文件
  4. npm i webpack-merge -D

此时可以执行npx webpack命令,npx依托于npm,它的作用就是观察在当前目录下有没有你想运行的命令,没有就会去上一级目录寻找。

vue需要安装的loader和plugin

  1. 全局安装webpack 打包工具

    1. npm install webpack webpack-cli -g
  2. 解析css 和 展示样式的loader —save-dev开发时需求

    1. npm install css-loader style-loader --save-dev
  3. 解析url 背景图片或者需要地址的时候

    1. npm install url-loader --save-dev
  4. 解析.vue文件 模块化 组件

    1. npm install vue-loader vue-template-compiler --save-dev
  5. 自动生成index.html文件自动导入js

    1. npm install html-webpack-plugin --save-dev
  6. 发布时使用 压缩文件

    1. npm install uglifyjs-webpack-plugin --save-dev
  7. 服务器运行

    1. npm install webpach-dev-server --save-dev
  8. 还有一些相关配置做一个笔记 ```javascript const path = require(‘path’) const webpack = require(‘webpack’) //自动生成html什么七七八八插件 const HtmlWebpackPlugin = require(‘html-webpack-plugin’) //压缩js插件 const UglifyjsWebpackPlugin = require(‘uglifyjs-webpack-plugin’) const VueLoaderPlugin = require(‘vue-loader’)

module.exports = { //入口 (打包的文件) entry: ‘./fvue/main.js’, //出口 (打包生成的文件,之后使用) output: { //使用绝对路径,找到文件夹 path: path.resolve(__dirname, ‘dist’), filename: ‘bundle.js’, //url默认文件夹 HtmlWebpackPlugin使用了该插件就不需要这个配置了 // publicPath: “dist/“ }, // mode: “development”,

//一些loader module: { rules: [{ test: /.css$/i, use: [“style-loader”, “css-loader”], },

  1. {
  2. test: /\.(png|jpg|gif)$/i,
  3. use: [{
  4. loader: 'url-loader',
  5. options: {
  6. limit: 8192,
  7. name: 'img/[name].[hash:8].[ext]'
  8. },
  9. }],
  10. },
  11. {
  12. test: /\.vue$/,
  13. loader: 'vue-loader'
  14. },
  15. ],

},

//编译vue中的template
resolve: { //alias 别名 alias: { ‘vue$’: ‘vue/dist/vue.esm.js’ } }, plugins: [ // make sure to include the plugin for the magic // new VueLoaderPlugin(),//这个会报错不知道为什么 new webpack.BannerPlugin(“最终版权归’orange’所有”), new HtmlWebpackPlugin({ template: ‘index.html’ }) ],

devServer: { contentBase: ‘./dist’, inline: true, } }

```