# 初始化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>
复制代码