webpack 快速打包配置
阿新 • • 發佈:2018-11-10
功能:自動打包(npm run dev
);相容 es6 的高階語法;相容 scss、less、css、字型圖示、常見圖片格式
1.轉到對應目錄下並初始化
命令:cd /d E:\......
查詢對應目錄下的檔案:dir
初始化命令:npm init -y
2.安裝包
全域性安裝:cnpm i webpack webpack-cli -g
(安裝過了就不用了)
-S
安裝:cnpm i webpack webpack-cli -S
-D
安裝:cnpm i webpack-dev-server html-webpack-plugin style-loader css-loader less-loader less sass-loader node-sass url-loader file-loader babel-core
[email protected] babel-plugin-transform-runtime babel-preset-env babel-preset-stage-0 -D
3.各個檔案的配置
package.json
{
"name": "kuaixuewu",
"version": "1.0.0",
"description": "",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev" : "webpack-dev-server --open --port 3000 --contentBase src --hot"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-plugin-transform-runtime" : "^6.23.0",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^1.0.1",
"file-loader": "^2.0.0",
"html-webpack-plugin": "^3.2.0",
"less": "^3.8.1",
"less-loader": "^4.1.0",
"node-sass": "^4.10.0",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"url-loader": "^1.1.2",
"webpack-dev-server": "^3.1.10"
}
}
webpack.config.js
const path=require('path');
const htmlWebpackPlugin=require('html-webpack-plugin');
module.exports={
mode:'development',
entry:path.join(__dirname,'src/main.js'),
output:{
path:path.join(__dirname,'dist'),
filename:'bundle.js'
},
plugins:[
new htmlWebpackPlugin({
template:path.join(__dirname,'./src/index.html'),
filename:'index.html'
})
],
module:{
rules:[
{test:/\.css$/,use:['style-loader','css-loader']},
{test:/\.less$/,use:['style-loader','css-loader','less-loader']},
{test:/\.scss$/,use:['style-loader','css-loader','sass-loader']},
{test:/\.(jpg|png|gif|bmp|jpeg)$/,use:['url-loader']},
{test:/\.(ttf|eot|svg|woff|woff2)$/,use:['url-loader']},
{test:/\.js$/,use:['babel-loader'],exclude:/node_modules/},
]
}
};
.babelrc
{
"presets": [
"env",
"stage-0"
],
"plugins": [
"transform-runtime"
]
}