UNPKG

dependency-injection-cat

Version:

DI Cat is a truly clean DI-container, which allows you not to pollute your business logic with decorators from DI/IOC libraries!

107 lines (106 loc) 5.61 kB
"use strict"; var __read = (this && this.__read) || function (o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); var CompilationContext_1 = require("../../compilation-context/CompilationContext"); var SourceFilesCache_1 = require("../../core/ts-helpers/source-files-cache/SourceFilesCache"); var PathResolverCache_1 = require("../../core/ts-helpers/path-resolver/PathResolverCache"); var ContextRepository_1 = require("../../core/context/ContextRepository"); var RebuildStatusRepository_1 = require("./RebuildStatusRepository"); var BeanRepository_1 = require("../../core/bean/BeanRepository"); var reportDIErrorsHook = function (compilation) { var globalContextsCount = Array.from(ContextRepository_1.ContextRepository.globalContexts.values()).length; if (globalContextsCount > 0) { compilation.warnings .push(buildWebpackError('You have Defined Global Cat Context, Currently, DI Cat does not support hot reloading of them')); } var message = CompilationContext_1.CompilationContext.getErrorMessage(); if (message === null) { SourceFilesCache_1.SourceFilesCache.clearCache(); PathResolverCache_1.PathResolverCache.clearCache(); return; } PathResolverCache_1.PathResolverCache.clearCache(); SourceFilesCache_1.SourceFilesCache.clearCache(); compilation.errors.push(buildWebpackError(message)); }; var PLUGIN_NAME = 'DI-Cat Webpack Plugin'; var DICatWebpackPlugin = /** @class */ (function () { function DICatWebpackPlugin() { DICatWebpackPlugin.isErrorsHandledByWebpack = true; } DICatWebpackPlugin.prototype.apply = function (compiler) { compiler.hooks.afterEmit.tap(PLUGIN_NAME, reportDIErrorsHook); compiler.hooks.done.tap(PLUGIN_NAME, function () { RebuildStatusRepository_1.RebuildStatusRepository.clear(); }); compiler.hooks.compilation.tap(PLUGIN_NAME, function (compilation) { var _a, _b, _c, _d; var webpack4ChangedFiles = Object.keys((_c = (_b = (_a = compiler.watchFileSystem) === null || _a === void 0 ? void 0 : _a.watcher) === null || _b === void 0 ? void 0 : _b.mtimes) !== null && _c !== void 0 ? _c : {}); var changedFiles = webpack4ChangedFiles.length > 0 ? new Set(webpack4ChangedFiles) : new Set(Array.from((_d = compiler.modifiedFiles) !== null && _d !== void 0 ? _d : [])); var tbeanContextPaths = Array.from(ContextRepository_1.ContextRepository.contextNameToTBeanNodeSourceDescriptor.values()) .filter(function (it) { return changedFiles.has(it.nodeSourceDescriptor.path); }) .map(function (it) { return it.contextDescriptor.absolutePath; }); var dependenciesContextPaths = Array.from(BeanRepository_1.BeanRepository.beanIdToBeanDescriptorMap.values()) .filter(function (it) { return it.beanSourceLocation && changedFiles.has(it.beanSourceLocation); }) .map(function (it) { return it.contextDescriptor.absolutePath; }); var contextsToRebuild = new Set(__spreadArray(__spreadArray([], __read(tbeanContextPaths), false), __read(dependenciesContextPaths), false)); compilation.hooks.finishModules.tapAsync(PLUGIN_NAME, function (modules, callback) { var contextModules = Array.from(modules) .filter(function (it) { return contextsToRebuild.has(it.resource); }); if (contextsToRebuild.size !== contextModules.length) { callback(buildWebpackError('Not all contexts found in webpack modules')); return; } if (contextModules.length === 0) { callback(); return; } RebuildStatusRepository_1.RebuildStatusRepository.setCallback(callback); RebuildStatusRepository_1.RebuildStatusRepository.registerStartRebuild(contextModules.map(function (it) { return it.resource; })); contextsToRebuild.clear(); contextModules.forEach(function (module) { compilation.rebuildModule(module, function (err) { if (err) { compilation.errors.push(err); } else { RebuildStatusRepository_1.RebuildStatusRepository.registerFileRebuildEnd(module.resource); } }); }); }); }); }; DICatWebpackPlugin.isErrorsHandledByWebpack = false; return DICatWebpackPlugin; }()); exports.default = DICatWebpackPlugin; function buildWebpackError(message) { return new Error(message); }