yarn init //初始化package.json yarn add webpack webpack-cli //添加webpack、webpack-cli //ps:不知那个版本开始就须要安装webpack-cli了
// index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>webpack v4</title> </head> <body> <div id="root"></div> <script src="dist/main.js"></script> </body> </html>
// src/index.js console.log('hello webpack v4');
//终端 webpack
你会发现目录底下多了dist文件夹里面有main.js,打开index.html 能够看到console里已经打印出了hello webpack v4,
在终端会有一个警告提示,大体意思是 没有设置mode将会使用生产模式,须要指定开发环境或者生产环境,打开dist/main.js,发现js确实是被压缩过的css
//console hello webpack v4
//package.json ... "scripts":{ "start":"--mode development", "build": "--mode production" } ...
yarn start //开发环境 yarn build //生产环境(压缩)
手动打开浏览器太麻烦html
//终端 yarn add webpack-dev-server //添加webpack-dev-server
//修改index.html <script src="main.js"></script>
//修改package.json ... "scripts": { "start": "webpack-dev-server --mode development --open", "build": "webpack --mode production" } ...
yarn start
自动打开浏览器了, 修改index.js试试
咱们将一个一个解决问题,不会一次安装全部依赖vue
yarn add vue //添加依赖
//app.vue <template> <section class="main"> <p>我来了{{name}}</p> </section> </template> <script> export default { data () { return { name:"vue + webpack" } } } </script> <style> .main>p{ color: #000; } </style>
import Vue from 'vue' import App from './pages/app.vue' new Vue({ el:"#root", render:h=>h(App) })
//终端 yarn start
这时候可预见的报错来了,node
Uncaught Error: Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type. //翻译:模块解析失败,您可能须要一个适当的加载程序来处理这个文件类型
看报错,咱们一个个解决
搜索关键词,很容易发现是没有安装vue-loader,新建webpack.config.jswebpack
//webpack.config.js module.exports = { module:{ rules:[ {test:/\.vue$/,use:'vue-loader'} ] } }
vue-loader在15以后须要在webpack.config.js中当插件引入web
const VueLoaderPlugin = require('vue-loader/lib/plugin') module.exports = { // ... plugins: [ new VueLoaderPlugin() ] }
//终端 yarn start
再次报错json
Uncaught Error: Module build failed: Error: Cannot find module 'vue-template-compiler' ...
很明显,要你安装vue-template-compiler浏览器
yarn add vue-template-compiler
//终端 yarn start
继续报错app
Module not found: Error: Can't resolve 'css-loader'
提示安装css-loaderwebpack-dev-server
yarn add css-loader
bingo! webpack4+vue2第一个项目成功运行
这时候细心的你发现css被塞到head里面,咱们来把css分离出来。使用webpack的插件extract-text-webpack-plugin@next
yarn add extract-text-webpack-plugin@next
//webpack.config.js const ExtractTextWebapckPlugin = require('extract-text-webpack-plugin'); //引入插件 module.exports = { module:{ rules:[ { test: /\.vue$/, loader: 'vue-loader', options: { loaders: { css: ExtractTextWebapckPlugin.extract({ use: 'css-loader' }) } } } ] }, plugins:[ new ExtractTextWebapckPlugin('style.css') ] }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>webpack v4</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="root"></div> <script src="main.js"></script> </body> </html>
//终端 yarn build
打包完成后dist目录里面出现了main.js和style.css
新建文件夹img,用来放图片
//app.vue <template> <section class="main"> <p>我来了{{name}}</p> <img src="../img/1.jpg" alt=""> </section> </template> <script> export default { data () { return { name:"vue + webpack" } } } </script> <style> .main>p{ color: #000; } </style>
//终端 yarn start
//报错 ./src/img/1.jpg Module parse failed: Unexpected character '�' (1:0) You may need an appropriate loader to handle this file type.
基本能够看出来是图片有问题,没有loader处理图片
yarn add file-loader url-loader
修改webpack.config.js
//webpack.config.js ... rule:[ ... { test: /\.(png|jpg|gif)$/, use: [{ loader: 'url-loader',options: { limit: 8192 } }] } ... ] ...
yarn start
打包成功...
resolve,watchOptions,devServer
//webpack.config.js resolve: { //导入的时候不用写拓展名 extensions: [' ', '.js', '.json', '.vue', '.scss', '.css'] }, watchOptions: { ignored: /node_modules/, aggregateTimeout: 300,//防止重复保存频繁从新编译,300ms内重复保存不打包 poll: 1000 //每秒询问的文件变动的次数 }, devServer:{ inline: true, compress: true, host: '127.0.0.1', port: 2500, historyApiFallback: true }