1 Star 4 Fork 0

韩子卢/cnblogs-skin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
webpack.config.js 3.82 KB
一键复制 编辑 原始数据 按行查看 历史
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;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/vvjiang/cnblogs-skin.git
git@gitee.com:vvjiang/cnblogs-skin.git
vvjiang
cnblogs-skin
cnblogs-skin
master

搜索帮助