const fs = require("fs");
class MarkdownWebpackPlugin {
constructor(options) {
this.options = options;
}
apply(compiler) {
compiler.hooks.emit.tapAsync('MarkdownWebpackPlugin', (compilation, callback) => {
// 获取Markdown内容
const markdownContent = this.options.templateContent ? this.options.templateContent : this.options.template && fs.readFileSync(this.options.template, 'utf8');
if(!markdownContent) {
throw new Error('template or templateContent is required');
}
// 生成Markdown文件
compilation.assets[this.options.filename]= {source: () => markdownContent};
callback();
});
}
}
module.exports = {
// 其他配置...
plugins: [
new MarkdownWebpackPlugin({
filename: 'test.md', // 生成的Markdown文件名
templateContent: `# 标题 \n Markdwon内容`
}),
new MarkdownWebpackPlugin({
filename: 'test.md', // 生成的Markdown文件名
template: path.resolve(__dirname, 'docs/test.md')
})
],
};