安装基础的webpack组件:
mkdir webpack-demo
cd webpack-demo
npm init -y
npm install webpack webpack-cli --save-dev
常用目录参考:
webpack-demo
|- /node_modelues
|- /dist(存放最终输出的文件)
|- /asset(存放HTML模板以及favoicon)
|- /src(存放源码)
|- index.js(入口文件)
|- /js
|- /css
|- /img
|- package.json
|- package-lock.json
|- webpack.common.js(webpack通用配置项)
|- webpack.dev.js(webpack开发环境配置项)
|- webpack.prod.js(webpack生产环境配置项)
npm install clean-webpack-plugin html-webpack-plugin webpack-dev-server webpack-merge -D
npm install sass-loader node-sass style-loader css-loader file-loader -D
简单介绍:
{
"name": "app-name",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config webpack.prod.js",
"start": "webpack-dev-server --open --config webpack.dev.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
... ...
},
"dependencies": {
... ...
}
}
webpack 的基础配置文件,将开发环境和生产环境的配置同样的项目写在该文件下:
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin')
const webpack = require('webpack');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
plugins: [
new CleanWebpackPlugin(),
new HtmlWebpackPlugin({
title: 'Title',
template: './asset/index.html' // HtmlWebpackPlugin 自动生成 HTML 文件的模板
}),
new webpack.HotModuleReplacementPlugin()
],
module: {
rules: [
// 图片加载器
{
test: /\.(png|svg|jpg|gif)$/,
use: [{
loader: 'file-loader',
options: {
name: '[hash].[ext]', // 将图片以哈希值命名,如果需要使用原名则将 [hash] 改为 [name]
outputPath: 'images' // 将读取的图片放置在 dist/images/ 目录下
}
}]
}
// css加载器
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
}
// scss加载器
{
test: /\.scss$/,
use: [
"style-loader",
"css-loader",
"sass-loader"
]
}
]
}
};
开发环境配置,主要配置热更新以及源代码映射:
const merge = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'development',
devtool: 'inline-source-map',
devServer: {
contentBase: './dist',
hot: true
}
})
生产环境配置:
const merge = require('webpack-merge');
const common = require('./webpack.common.js');
module.exports = merge(common, {
mode: 'production'
})