代码拉取完成,页面将自动刷新
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const miniCssExtractPlugin = require('mini-css-extract-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const path = require('path');
const webpack = require('webpack');
const fs = require('fs');
/**
* 自制注入页首和页脚html模块代码的webpack插件
*/
function InjectBlogHtmlPlugin() {
console.info('html模块注入')
}
InjectBlogHtmlPlugin.prototype.apply = function (compiler) {
compiler.hooks.compilation.tap('InjectBlogHtmlPlugin', (compilation) => {
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tapAsync(
'InjectBlogHtmlPlugin',
function (data, callback) {
// 注释部分是原先js部分替换模板占位符,现在用空字符串替换,因为app.js中就引入了js以实现热更新,这里重复了
// const headerJsText = fs.readFileSync(`./src/js/header.js`).toString()
const headerHtmlText = fs.readFileSync(`./src/template/header.html`).toString()
// const footerJsText = fs.readFileSync(`./src/js/footer.js`).toString()
const footerHtmlText = fs.readFileSync(`./src/template/footer.html`).toString()
// data.html = data.html.replace('{{headerHtml}}', headerHtmlText.replace("{{jsContent}}", headerJsText))
// data.html = data.html.replace('{{footerHtml}}', footerHtmlText.replace("{{jsContent}}", footerJsText))
data.html = data.html.replace('{{headerHtml}}', headerHtmlText.replace("{{jsContent}}", ''))
data.html = data.html.replace('{{footerHtml}}', footerHtmlText.replace("{{jsContent}}", ''))
callback(null, data)
}
)
})
};
const htmlNames = [
'categoryList',
'index',
'read'
]
const config = {
entry: ['./app.js'],
output: {
path: path.resolve(__dirname, 'build'),
filename: '[name].js',
publicPath: process.env.NODE_ENV === 'production' ? 'http://images.cnblogs.com/cnblogs_com/vvjiang/996881/' : '/'
},
devServer: {
port: 8686,
open: true,
compress: true,
inline: true,
index: 'index.html',
},
module: {
rules: [
{
test: /\.(png|jpg|gif)$/i,
use: [
{
loader: 'file-loader',
options: {
name: '[name].[ext]'
}
}
]
}, {
test: /\.css$/,
use: [
miniCssExtractPlugin.loader,
'css-loader',
],
},
{
test: /\.less$/,
use: [
miniCssExtractPlugin.loader,
'css-loader',
'less-loader'
],
},
{
test: /\.(htm|html)$/,
use: [
'raw-loader'
]
}
],
},
optimization: {
minimizer: [
new OptimizeCSSAssetsPlugin({})
],
},
plugins: [
new miniCssExtractPlugin({ filename: '[name].css', allChunks: false }),
new webpack.NamedModulesPlugin(),
new webpack.HotModuleReplacementPlugin(),
new CleanWebpackPlugin(),
]
};
htmlNames.forEach((pageName) => {
const htmlPlugin = new HtmlWebpackPlugin({
filename: `${pageName}.html`,
template: `./template/${pageName}.html`,
hash: true,
inject: "head"
});
config.plugins.push(htmlPlugin);
})
config.plugins.push(new InjectBlogHtmlPlugin());
module.exports = config;
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。