import FStream from 'fs'; /** * 版本信息生成插件 * @author phpdragon@qq.com * @param options * @constructor */ export class VersionPlugin { constructor(private dir_path: string, private version: string) { } // apply方法是必须要有的,因为当我们使用一个插件时(new somePlugins({})),webpack会去寻找插件的apply方法并执行 apply(compiler: { plugin: (arg0: string, arg1: { (params: any): void; (stats: any): void; }) => void; }) { compiler.plugin("compile", (params: any) => { FStream.stat(this.dir_path, (err, stats) => { this.writeVersion(this.dir_path + "\\ver.json"); }); }); //编译器'对'所有任务已经完成'这个事件的监听 compiler.plugin("done", (stats: any) => { console.log("应用编译完成!"); }); } writeVersion = (versionFile: string) => { console.log("\n当前版本号:" + this.version); //写入文件 FStream.writeFile(versionFile, this.version, (err: any) => { if (err) throw err; console.log("版本信息写入成功!"); }); }; }