2 webpack 4 加vue搭建开发环境最终配置
1 package.json
{ "name": "c", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "dev": "webpack-dev-server --open --config webpack.config.js" }, "author": "", "license": "ISC", "devDependencies": { "@babel/core": "^7.1.6", "@babel/preset-env": "^7.1.6", "babel": "^6.23.0", "babel-core": "^6.26.3", "babel-loader": "^8.0.4", "babel-plugin-transform-runtime": "^6.23.0", "babel-preset-es2015": "^6.24.1", "babel-runtime": "^6.26.0", "css-loader": "^1.0.1", "extract-text-webpack-plugin": "^3.0.2", "mini-css-extract-plugin": "^0.4.5", "style-loader": "^0.23.1", "vue-hot-reload-api": "^2.3.1", "vue-loader": "^15.4.2", "vue-style-loader": "^4.1.2", "vue-template-compiler": "^2.5.17", "webpack": "^4.26.0", "webpack-cli": "^3.1.2", "webpack-dev-server": "^3.1.10" }, "dependencies": { "vue": "^2.5.17" } }
2 webpack.config.js
// var path=require("path"); //var extractTextPlugin=require('extract-text-webpack-plugin'); var extractTextPlugin=require('mini-css-extract-plugin'); const VueLoaderPlugin=require('vue-loader/lib/plugin'); var config = { mode:'development', entry:{ //配置的单入口,webpack会从main.js文件开始工作 main:'./main' }, output:{ //打包后文件的输出目录 path:path.join(__dirname,'./dist'), //指定资源文件引用的目录 publicPath:'/dist/', //用于指定输出文件的名称 filename:'main.js' }, module:{ //再module对象的rules属性可以指定一系列的loeders,每一个loader都必须包含test和use两个选项 //此配置的意思: //当webpack编译过程中遇到require和import语句导入一个后缀名为.css的文件时 //,先将他通过css-loader转换,再通过style-loader转换,然后继续打包 rules:[ /*{ test:/\.css$/, use:[ 'style-loader', 'css-loader' ] }*/ { test:/\.vue$/, loader:'vue-loader', }, { test:/\.css$/, use:[ extractTextPlugin.loader, 'css-loader', ] }, { test:/\.js$/, loader:'babel-loader', exclude:/node_modules/ }, ] }, plugins:[ new VueLoaderPlugin(), //重命名提取后的css文件 new extractTextPlugin({ filename: "[name].css", chunkFilename: "[id].css" }), ] }; module.exports = config
3 最终运行npm run dev能够处理.vue文件
大家可以结合上篇笔记参考dev环境的配置