一、webpack基本配置
module.exports = {
context: path.resolve(__dirname, '../'),
entry: {
app: './src/main.js'
},
output: {
path: config.build.assetsRoot,
filename: '[name].js',
},
resolve: {
extensions: ['.js', '.vue', '.json'],
alias: {
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src'),
}
},
module: {
rules: [
{
test: /\.vue$/,
loader: 'vue-loader',
options: vueLoaderConfig
},
{
test: /\.js$/,
loader: 'babel-loader',
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
},
{
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('img/[name].[hash:7].[ext]')
}
},
{
test: /\.(mp4|webm|ogg|mp3|wav|flac|aac)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('media/[name].[hash:7].[ext]')
}
},
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
}
}
]
},
node: {
// prevent webpack from injecting useless setImmediate polyfill because Vue
// source contains it (although only uses it if it's native).
setImmediate: false,
// prevent webpack from injecting mocks to Node native modules
// that does not make sense for the client
dgram: 'empty',
fs: 'empty',
net: 'empty',
tls: 'empty',
child_process: 'empty',
},
plugins: [
new webpack.ProvidePlugin({
$: "jquery",
jQuery: 'jquery',
})
]
}
二、插件html-webpack-plugin
plugins使用。
module.exports = {
context: , // 上线环境配置
entry: {
main: './src/srcipt/main.js',
a: './src/scripts.js'
},
output: {
path: './dist/js',
filename: 'js/[name]-[chunkhash].js'
},
plugins: [
new htmlWebpackPlugin({
filename: 'index-[hash].html'
template: 'index.html' //路径为绝对路径,上线后的环境
inject: 'head' //将<script>标签嵌套在<head>标签内
minify: { //对html文件进行压缩
removeComments: true, // 去掉注释
}
})
]
}
通过htmlWebpackPlugin生成多页面应用。
module.exports = {
context: , // 上线环境配置
entry: {
main: './src/srcipt/main.js',
a: './src/a.js',
b: './src/b.js'
c: './src/c.js'
},
output: {
path: './dist/js',
filename: 'js/[name]-[chunkhash].js'
},
plugins: [
new htmlWebpackPlugin({
filename: 'a.html'
template: 'index.html' //路径为绝对路径,上线后的环境
inject: 'head' //将<script>标签嵌套在<head>标签内
chunks: ['main','a'] // 传参
}),
new htmlWebpackPlugin({
filename: 'b.html'
template: 'index.html' //路径为绝对路径,上线后的环境
inject: 'head' //将<script>标签嵌套在<head>标签内
chunks: ['b'] // 传参
}
}),
new htmlWebpackPlugin({
filename: 'c.html'
template: 'index.html' //路径为绝对路径,上线后的环境
inject: 'head' //将<script>标签嵌套在<head>标签内
excludeChunks: ['a','b'] // 传参除了a,b
})
]
}
三、Loader特性。
module: {
// 处理资源文件
loaders: [
{
test: /\.js$/,
loader: 'babel',
query: {
presets: ['latest']
}
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
},
{
test: /\.less$/,
loader: 'style!css!postcss!less'
},
{
test: /\.(png|jpg|svg|gif)$/i,
loader: 'file-loader'
}
] ,
},