代码拉取完成,页面将自动刷新
const { resolve } = require('path');
const path = require('path');
//打包html资源
const HtmlWebpackPlugin = require('html-webpack-plugin');
//提取css文件成单独文件
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
//压缩css代码
const OptimizeCssAssetsWebpackPlugin = require('optimize-css-assets-webpack-plugin');
//自动清除上次打包的文件
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
//打包favicon.icon资源
const CopyWebpackPlugin = require('copy-webpack-plugin');
//打包第三方库并让html自动引入单独打包的第三方库
const AddAssetHtmlWebpackPlugin = require('add-asset-html-webpack-plugin')
const webpack = require('webpack');
//css兼容性处理
const CssCompatible = [
MiniCssExtractPlugin.loader,
'css-loader',
//解决兼容性问题
{
loader: 'postcss-loader',
options: {
postcssOptions: {
ident: 'postcss',
plugins: [
require('postcss-preset-env')
]
}
}
}
];
//直接复制打包的文件
var copyFile = new CopyWebpackPlugin({
patterns: [
{
from: './src/commonimages/icon/favicon.ico',
to: resolve(__dirname, './build/images/icon/')
},
{
from: './src/zjmm.json',
to: resolve(__dirname, './build/')
},
]
});
//处理路径的库
const glob = require('glob');
// process.env.NODE_ENV = 'development';
let webpackconfig = {
entry: addEntry(),
output: {
filename: 'js/[name]/[name].[contenthash:10].js',
path: resolve(__dirname, 'build'),
// publicPath:'../',
chunkFilename: '[name]_chunk.js',//非入口chunk的名称
},
module: {
rules: [
//处理css资源
{
oneOf: [
{
test: /\.css$/,
use: [...CssCompatible]
},
{
test: /\.less$/,
use: [...CssCompatible, 'less-loader']
},
//处理css中的图片资源
{
test: /\.(jpg|png|gif|icon)$/,
loader: 'url-loader',
options: {
limit: 6 * 1024,
name: '[name].[hash:10].[ext]',
outputPath: './images',
publicPath: '../images'// 对页面引入资源的补充,比如img标签引入
},
},
//处理html中的图片资源
{
test: /\.html$/,
loader: 'html-loader'
},
//打包其他资源
{
exclude: /\.(html|css|js|json|jpg|png|gif)$/,
loader: 'file-loader',
options: {
name: '[hash:10].[ext]',
outputPath: './media',
publicPath: '../media'
}
},
//js兼容性处理
{
test: /\.js$/,
exclude: /node_modules/,
use: [
{
loader: 'thread-loader',
options: {
workers: 2,//进程2
}
},
{
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env',
{
useBuiltIns: 'usage',//按需加载
corejs: {
version: 3 //指定corejs版本
},
targets: {
chrome: '60',
firefox: '60',
ie: '9',
safari: '50',
edge: '17'
}
}]
],
cacheDirectory: true,//开启bebal缓存,第2次构建时,会读取之前的缓存
}
}
]
}
]
}
]
},
plugins: [
//抽离css成单独文件
new MiniCssExtractPlugin({
filename: 'css/[name].[contenthash].css',
// publicPath:'../'
}),
//清楚上一次打包的文件
new CleanWebpackPlugin(),
//直接复制打包的文件
copyFile,
//告诉webapck哪些库不参与打包,同时使用的名称也得变
new webpack.DllReferencePlugin({
manifest: resolve(__dirname, './dll/manifest.json'),
}),
new AddAssetHtmlWebpackPlugin({
filepath: resolve(__dirname, './dll/jquery.js'),
publicPath: '../'
}),
new AddAssetHtmlWebpackPlugin({
filepath: resolve(__dirname, './dll/echarts.js'),
publicPath: '../'
}),
//压缩css代码
new OptimizeCssAssetsWebpackPlugin(),
],
mode: 'production',
// mode: 'development',
devServer: {
contentBase: resolve(__dirname, 'build'),
//启动gizp压缩
compress: true,
port: 3000,
open: true,
watchContentBase: true,//监视contentBase目录下的所有文件,一旦文件发生变化就会reload
watchOptions: {
ignored: /node_modules/,//忽略文件
},
clientLogLevel: 'none',//不要显示启动服务器日志信息
quiet: true,//除了一次额基本启动信息以外,其他内容都不要显示
overlay: false,//如果出错了,不要全屏提示
},
devtool: 'cheap-module-eval-source-map',
// devtool: 'source-map',
resolve: {
extensions: ['.js', '.json'],//提高效率
modules: [resolve(__dirname, './node_modules'), 'node_modules']
}
};
function getEntry() {
let globPath = 'src/*/html/*.html';
let pathDir = 'src(\/|\\\\)(.*?)(\/|\\\\)html';
let files = glob.sync(globPath);
let dirname, entries = [];
for (let i = 0; i < files.length; i++) {
dirname = path.dirname(files[i]);
entries.push(dirname.replace(new RegExp('^' + pathDir), '$2'))
}
// console.log(entries)
return entries;
}
function addEntry() {
let entryObj = {};
getEntry().forEach(item => {
entryObj[item] = resolve(__dirname, 'src', item, 'index.js')
})
// console.log(entryObj)
return entryObj;
}
getEntry().forEach(pathname => {
// console.log(pathname)
let conf = {
filename: path.join(pathname, "index") + '.html',
template: path.join(__dirname, 'src', pathname, 'html', 'index.html'),
chunks: [pathname],
//压缩html文件
minify: {
collapseWhitespace: true,
removeComments: true,
}
}
// console.log(copyFile)
// console.log(conf.filename)
webpackconfig.plugins.push(new HtmlWebpackPlugin(conf))
})
// entry: {
// main: './src/main/js/index.js',
// ship: './src/ship/js/indexship.js',
// commodity: './src/commodity/js/indexcom.js',
// hazardous: './src/hazardous/js/indexhaz.js',
// productoil: './src/productoil/js/indexproductoil.js',
// },
function getFilePage() {
let globPath = 'src/filepage/**/*.html';
let pathDir = 'src(\/|\\\\)(.*?)(\/|\\\\)html';
let files = glob.sync(globPath);
let dirname, entries = [];
for (let i = 0; i < files.length; i++) {
dirname = path.dirname(files[i]);
// console.log("dirname"+dirname);
entries.push(dirname.replace(new RegExp('^' + pathDir), '$2'))
}
// console.log('entries:'+entries);
return entries;
}
getFilePage().forEach(pathname => {
// console.log(pathname)
let conf = {
from: path.join(__dirname, pathname, 'index.html'),
to: path.join(__dirname, 'build', pathname.replace(/src/, ''), 'index') + '.html'
}
// console.log(conf);
webpackconfig.plugins[2].patterns.push(conf);
})
module.exports = webpackconfig;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。