# 初始化npm
npm init -y
# install webpack
yarn add -D webpack webpack-cli
複製代碼
module.exports = {
mode: 'production',
entry: './src/index.js'
}
複製代碼
console.log('Hello Webpack')
複製代碼
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"webpack": "^4.33.0",
"webpack-cli": "^3.3.3"
}
}
複製代碼
yarn build
複製代碼
在當前目錄下生成 dist/main.js
文件,即爲編譯後的index.jsjavascript
利用 html-webpack-plugin
加載html文件css
yarn add -D html-webpack-plugin
複製代碼
const HtmlWepackPlugin = require('html-webpack-plugin')
module.exports = {
mode: 'production',
entry: './src/index.js',
plugins: [
new HtmlWepackPlugin({
template: './src/index.html'
})
]
}
複製代碼
src
目錄下建立 index.html
在當前目錄下生成 dist/index.html
文件html
利用 style-loader
css-loader
加載css文件vue
style-loader
、 css-loader
yarn add -D style-loader css-loader
複製代碼
webpack.config.js
const HtmlWepackPlugin = require('html-webpack-plugin')
module.exports = {
mode: 'production',
entry: './src/index.js',
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' }
]
}
]
},
plugins: [
new HtmlWepackPlugin({
template: './src/index.html'
})
]
}
複製代碼
src
目錄下建立 global.css
src/index.js
引入 global.css
import './global.css'
console.log('Hello World')
複製代碼
編譯完成後, dist/index.html
中會引入 global.css
的內容java
利用 less-loader
加載lesswebpack
less-loader
less
yarn add -D less-loader less
複製代碼
webpack.config.js
const HtmlWepackPlugin = require('html-webpack-plugin')
module.exports = {
mode: 'production',
entry: './src/index.js',
module: {
rules: [
{
test: /\.css$/,
use: [
{ loader: 'style-loader' },
{ loader: 'css-loader' }
]
}, {
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'less-loader'
]
}
]
},
plugins: [
new HtmlWepackPlugin({
template: './src/index.html'
})
]
}
複製代碼
index.less
.content {
p {
color: aquamarine;
}
}
複製代碼
src/index.js
中引入 index.less
import './index.less'
複製代碼
編譯完成後, dist/index.html
中會引入 index.less 的內容web
利用 mini-css-extract-plugin
進行css文件的獨立,避免將css打包進js文件中shell
mini-css-extract-plugin
yarn add -D mini-css-extract-plugin
複製代碼
webpack.config.js
const HtmlWepackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
mode: 'production',
entry: './src/index.js',
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
hmr: process.env.NODE_ENV === 'development'
}
},
{ loader: 'css-loader' }
]
}, {
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'less-loader'
]
}
]
},
plugins: [
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
}),
new HtmlWepackPlugin({
template: './src/index.html'
})
]
}
複製代碼
dist/main.css
文件webpack-dev-server
package.json
添加dev腳本{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server",
"build": "webpack --config webpack.config.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"css-loader": "^2.1.1",
"html-webpack-plugin": "^3.2.0",
"less": "^3.9.0",
"less-loader": "^5.0.0",
"mini-css-extract-plugin": "^0.7.0",
"style-loader": "^0.23.1",
"webpack": "^4.33.0",
"webpack-cli": "^3.3.3",
"webpack-dev-server": "^3.7.1"
}
}
複製代碼
yarn dev
複製代碼
此時會打印 Project is running at http://localhost:8081/
訪問http://localhost:8081/ 會顯示index.html的內容,當咱們進行修改後,無需從新編譯, webpack-dev-server
會自動刷新頁面npm
vue-loader
、 vue-template-compiler
、 vue
yarn add -D vue-loader vue-template-compiler
yarn add -D vue
複製代碼
webpack.config.js
const HtmlWepackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
module.exports = {
mode: 'production',
entry: './src/index.js',
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.css$/,
use: [
{
loader: MiniCssExtractPlugin.loader,
options: {
publicPath: '../',
hmr: process.env.NODE_ENV === 'development'
}
},
{ loader: 'css-loader' }
]
}, {
test: /\.less$/,
use: [
'style-loader',
'css-loader',
'less-loader'
]
}
]
},
plugins: [
new VueLoaderPlugin(), // 這個插件是必須的,它的職責是將定義過的其餘規則複製並應用到.vue文件裏相應語音的塊
new MiniCssExtractPlugin({
filename: '[name].css',
chunkFilename: '[id].css'
}),
new HtmlWepackPlugin({
template: './src/index.html'
})
]
}
複製代碼
在 src
目錄下建立 App.vue
文件json
<template>
<div>
<h1>Hello Vue</h1>
</div>
</template>
複製代碼
import Vue from 'vue'
import App from './App.vue'
new Vue({
el: '#app',
render: (h) => h(App)
})
複製代碼
<!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 Demos</title>
</head>
<body>
<div id="app">
</div>
</body>
</html>
複製代碼