3.webpack配置

Posted haines

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3.webpack配置相关的知识,希望对你有一定的参考价值。

1.项目目录

2.配置代码
const path = require(‘path‘);
const htmlWebpackPlugin = require(‘html-webpack-plugin‘);
const ExtractTextPlugin = require(‘extract-text-webpack-plugin‘);
const webpack = require(‘webpack‘);
module.exports = {
entry: ‘./src/app.jsx‘,
output: {
path: path.resolve(__dirname, ‘dist‘),
filename: ‘js/app.js‘,
publicPath: "/dist/"
},
module: {
rules: [
//jsx
{
test: /.jsx$/,
exclude: /(node_modules)/,
use: {
loader: ‘babel-loader‘,
options: {
presets: [‘env‘,‘react‘]//根据环境打包(浏览器,node)
}
}
},
//css
{
test: /.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: "css-loader"
})
},
//sass
{
test: /.scss$/,
use: ExtractTextPlugin.extract({
fallback: ‘style-loader‘,
use: [‘css-loader‘, ‘sass-loader‘]
})
},
//图片的配置
{
test: /.(png|jpg|gif)$/i,
use: [
{
loader: ‘url-loader‘,
options: {
limit: 8192,
name:‘resource/[name].[ext]‘
}
}
]
},
//字体的配置
{
test: /.(otf|eot|svg|ttf|woff|woff2)$/i,
use: [
{
loader: ‘url-loader‘,
options: {
limit: 8192,
name:‘resource/[name].[ext]‘
}
}
]
}
]
},
plugins: [
//处理css文件
new HtmlWebpackPlugin({
template:‘./src/index.html‘
}),
//独立css
new ExtractTextPlugin("css/[name].css"),//参数为生成css的位置
//提取公共模块
new webpack.optimize.CommonsChunkPlugin({//webpack自带插件,不用安装
name:‘common‘,
filename:‘js/base.js‘
})
],
devServer: {
// contentBase: path.join(__dirname, ‘dist‘),
port: 9999
}
};
























































































以上是关于3.webpack配置的主要内容,如果未能解决你的问题,请参考以下文章

webpack源码讲解

初始webpack

webpack

「offer来了」从基础配置到高级配置,16大知识点带你巩固webpack知识体系

「offer来了」从基础配置到高级配置,16大知识点带你巩固webpack知识体系

关闭 css 的单独块,vue cli 3 webpack 4