webpack是一个静态模块打包器。当webpack处理应用程序时,它会递归地构建一个依赖关系图,其中包含应用程序需要的每个模块,然后将这些模块打包成一个或多个bundle。
const config = {
entry: './src/main.js'
};
module.exports = config;
const config = {
entry: {
pageOne: './src/pageOne/index.js',
pageTwo: './src/pageTwo/index.js',
pageThree: './src/pageThree/index.js'
}
};
const config = {
output: {
filename: 'bundle.js',
path: '/home/proj/public/assets'
}
};
module.exports = config;
const condig = {
entry: {
app: './src/app.js',
search: './src/search.js'
},
output: {
filename: '[name].js',
path: __dirname + '/dist'
}
};
1.配置:在webpack.config.js文件中指定loader(推荐用法)
const config = {
module: {
rules: [
{test: /\.css$/, use: [
{loader: 'style-loader'},
{loader: 'css-loader',
options: {
modules: true
}}
]}
]
}
}
import Styles from 'style-loader!css-loader?modules!./styles.css';
webpack --module-bind jade-loader --module-bind 'css-style-loader!css-loader'
const HtmlwebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');
const path = require('path');
const config = {
plugins: [
new webpack.optimize.UglifyJsPlugin(),
new HtmlwebpackPlugin({
template: './src/index.html'
})
]
}
module.exports = config;
module.exports = {
mode: 'production'
};
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin'); // 将文件从js中分离
var CommonChunkPlugin = webpack.optimize.CommonChunkPlugin; // 将公共模块抽离合成文件
var HtmlWebpackPlugin = require('html-webpack-plugin'); // 生成替换哈希文件名的HTML
var OccurrenceOrderPlugin = webpack.optimize.OccurrenceOrderPlugin; // 优化模块的ID分配,用的多的模块ID更小
var UglifyJsPlugin = webpack.optimize.UglifyJsPlugin; // 压缩JS代码
var cleanWebpackPlugin = require('clean-webpack-plugin'); // 清除文件
var path = require('path');
const config = {
// 上下文
context: path.resolve(__dirname, 'src'),
// 入口
entry: {
a: './a.js',
b: './b.js',
vendor: ['./c.js', 'd.js']
},
// 出口
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name]-[hash:16].js',
publicPath: 'https://cdn.example.com/xxx/',
sourceMapFilename: '[file].map'
},
devtool: '#source-map',
devServer: {
contentBase: path.resolve(__dirname, 'dist'),
inline: true,
hot: true,
host: '0.0.0.0',
port: '8884'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [{
loader: 'css-loader',
options: {
modules: true
}
}, {
loader: 'postcss-loader'
}]
})
},
{
test: /\.less$/,
use: ExtractTextPlugin.extract({
fallback: "style-loader",
use: [
{
loader: 'css-loader',
options: {
modules: true
}
}, {
loader: 'postcss-loader'
}, {
loader: 'less-loader'
}
]
})
},
{
test: /\.(png|jpg)$/,
loader: 'url-loader',
options: {
limit: 1024,
}
},
{
test: /\.vue$/,
loader: 'vue-loader'
},
{
test: /\.(woff|woff2|eot)$/,
loader: 'url-loader',
options: {
limit: 10000,
mintype: 'application/font-woff'
}
},
{
test: /\.ttf$/,
loader: 'url-loader',
options: {
limit: 10000,
mintype: 'application/font-woff'
}
},
{
test: /\.eot$/,
loader: 'file-loader'
},
{
test: /\.svg$/,
loader: 'url-loader',
options: {
limit: 10000,
mintype: 'image/svg+xml'
}
}
]
},
plugins: [
// 公共模块
new CommonChunkPlugin({
name: 'commons',
filename: 'commons.js',
chunks: ['a', 'b'] // 只使用这些入口chunk
}),
// 清除多余文件
new cleanWebpackPlugin('dist/*.*', {
root: __dirname,
verbose: true, // 打印日志
dry: false
}),
// 设置抽离出的css文件名
new ExtractTextPlugin({
filename: 'css/[name].[contenthash].css',
allChunks: true,
disable: false
}),
// 定义全局变量
new webpack.definePlugin({
__DEV__: JSON.stringify(JSON.parse(process.env.BUILD_DEV || 'false')),
__PRERELEASE__: JSON.stringify(JSON.parse(process.env.BUILD_PRERELEASE || 'false'))
}),
// 生成HTML文件,资源文件会被set进去
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/index.html',
inject: true
}),
// 热加载组件
new webpack.HotModuleReplacementPlugin(),
// 优化模块ID分配
new OccurrenceOrderPlugin(),
// 压缩js
new UglifyJsPlugin(),
],
// 定义不会被打包的依赖
externals: {
jquery: 'jQuery',
react: 'react'
}
};
module.exports = config;