UNPKG

time-analytics-webpack-plugin

Version:
131 lines 5.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isWebpackPlugin = exports.TimeAnalyticsPlugin = void 0; const analyzer_1 = require("./analyzer"); const ProxyPlugin_1 = require("./ProxyPlugin"); const loaderHelper_1 = require("./loaderHelper"); const utils_1 = require("./utils"); require("./sideEffects/hackWeakMap"); class TimeAnalyticsPlugin { apply(compiler) { compiler.hooks.compile.tap(TimeAnalyticsPlugin.name, () => { analyzer_1.analyzer.initilize(); analyzer_1.analyzer.collectWebpackInfo({ hookType: analyzer_1.WebpackMetaEventType.Compiler_compile, kind: analyzer_1.AnalyzeInfoKind.webpackMeta, time: (0, utils_1.now)(), }); }); compiler.hooks.done.tap(TimeAnalyticsPlugin.name, () => { analyzer_1.analyzer.collectWebpackInfo({ hookType: analyzer_1.WebpackMetaEventType.Compiler_done, kind: analyzer_1.AnalyzeInfoKind.webpackMeta, time: (0, utils_1.now)(), }); analyzer_1.analyzer.output({ filePath: this.option?.outputFile, dangerTimeLimit: this.option?.dangerTimeLimit ?? 8000, warnTimeLimit: this.option?.warnTimeLimit ?? 3000, ignoredLoaders: this.option?.loader?.exclude ?? [], groupLoaderByPath: this.option?.loader?.groupedByAbsolutePath ?? false, }); }); } constructor(option) { this.option = option; this.option = option; } static wrap(webpackConfigOrFactory, options) { if (options?.enable === false) { return webpackConfigOrFactory; } const timeAnalyticsPlugin = new TimeAnalyticsPlugin(options); if (typeof webpackConfigOrFactory === 'function') { return (...args) => wrapConfigurationCore.call(timeAnalyticsPlugin, webpackConfigOrFactory(...args)); } return wrapConfigurationCore.call(timeAnalyticsPlugin, webpackConfigOrFactory); } get isLoaderEnabled() { switch (typeof this.option?.enable) { case 'boolean': return this.option.enable; case 'object': return this.option.enable.loader; case 'undefined': return true; default: (0, utils_1.fail)('TS has a strange error here. We could not use assertNever, use fail instead.'); } } get isPluginEnabled() { switch (typeof this.option?.enable) { case 'boolean': return this.option.enable; case 'object': return this.option.enable.plugin; case 'undefined': return true; default: (0, utils_1.fail)('TS has a strange error here. We could not use assertNever, use fail instead.'); } } } exports.TimeAnalyticsPlugin = TimeAnalyticsPlugin; function wrapConfigurationCore(config) { const newConfig = { ...config }; if (this.isPluginEnabled && newConfig.plugins) { newConfig.plugins = newConfig.plugins.map((plugin) => { const pluginName = plugin.constructor.name; if (this.option?.plugin?.exclude?.includes(pluginName)) { return plugin; } return wrapPluginCore(plugin); }); newConfig.plugins = [this, ...newConfig.plugins]; } if (this.isPluginEnabled && newConfig.optimization?.minimizer) { newConfig.optimization.minimizer = newConfig.optimization.minimizer .map((minimizer) => { const pluginName = minimizer.constructor.name; if (this.option?.plugin?.exclude?.includes(pluginName)) { return minimizer; } return wrapMinimizer(minimizer); }); } if (this.isLoaderEnabled && newConfig.module) { newConfig.module = injectModule(newConfig.module); } return newConfig; } /** * Fancy hack to judge whether an object is a Webpack plugin or function. */ function isWebpackPlugin(p) { return typeof p.apply === 'function' && p.apply !== Object.apply; } exports.isWebpackPlugin = isWebpackPlugin; function wrapMinimizer(minimizer) { if (isWebpackPlugin(minimizer)) { return wrapPluginCore(minimizer); } utils_1.ConsoleHelper.warn(`could not handle function-like minimizer, please convert it to the plugin-like form.`); return minimizer; } function wrapPluginCore(plugin) { return new ProxyPlugin_1.ProxyPlugin(plugin); } function injectModule(moduleOptions) { const newModuleOptions = { ...moduleOptions }; if (newModuleOptions.rules) { if (!isRuleObjectArray(newModuleOptions.rules)) { (0, utils_1.fail)('There is plain string "..." in "module.rules", why do you need this? Please submit an issue.'); } newModuleOptions.rules = (0, loaderHelper_1.normalizeRules)(newModuleOptions.rules); } return newModuleOptions; function isRuleObjectArray(rules) { return rules.every(rule => rule !== '...'); } } //# sourceMappingURL=TimeAnalyticsPlugin.js.map