UNPKG

module-federation-exposes-webpack-plugin

Version:
64 lines (63 loc) 3.05 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const generateExposes_1 = require("./generateExposes"); const fs_extra_1 = __importDefault(require("fs-extra")); class ModuleFederationExposesPlugin { constructor(options) { const { exportFile, generateDir, filetype, transform } = options; this.exportFile = exportFile; this.generateDir = generateDir; this.filetype = filetype; this.transform = transform; this.prevTimestamps = {}; } apply(compiler) { // 在触发新编译之后但在编译实际开始之前,在监视模式下执行插件 compiler.hooks.watchRun.tapAsync(this.constructor.name, (compiler, callback) => __awaiter(this, void 0, void 0, function* () { const changedFiles = this.getChangedFiles(compiler); // 当没有generateDir目录时,或exportFile文件改变时 if (!fs_extra_1.default.existsSync(this.generateDir) || changedFiles.includes(this.exportFile)) { yield (0, generateExposes_1.generateExposes)({ exportFile: this.exportFile, generateDir: this.generateDir, filetype: this.filetype, transform: this.transform, }); } callback(); })); } getChangedFiles(compiler) { // 获取 compiler 对象中文件的时间戳 const fileTimestamps = Array.from(compiler.fileTimestamps || []); const changedFiles = []; // 检查哪些文件发生了变化 for (const [key, value] of fileTimestamps) { const timestamp = value === null || value === void 0 ? void 0 : value.timestamp; if ( // 文件有时间戳 timestamp && // 文件首次出现 文件时间戳变化 (!this.prevTimestamps[key] || timestamp > this.prevTimestamps[key])) { // 更新文件的时间戳 this.prevTimestamps[key] = timestamp; changedFiles.push(key); } } return changedFiles; } } exports.default = ModuleFederationExposesPlugin;