UNPKG

@sprit/pretty-console-webpack-plugin

Version:

## Usage ```typescript import { PrettyConsoleWebpackPlugin } from '@sprit/pretty-console-webpack-plugin'; ```

223 lines (208 loc) 7.6 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var safeRequire = _interopDefault(require('safe-require')); var child_process = require('child_process'); var path = _interopDefault(require('path')); /** 插件名 */ var PLUGIN_NAME = 'pretty-console-webpack-plugin'; /** 默认配置 */ var DEFAULT_OPTION = { NODE_ENV: true }; /** * 打印日志 */ var log = function log(msg) { return console.log("%c [" + PLUGIN_NAME + "] " + JSON.stringify(msg), 'color: red'); }; // 默认键背景色 var defaultKeyBgColor = '#363636'; // 默认值背景色 var defaultValBgColor = '#175e9c'; // 默认配置项 var defaultOption = { keyBgColor: defaultKeyBgColor, valBgColor: defaultValBgColor }; /** * 获取 console 信息 */ var getConsole = function getConsole(key, val, option) { if (option === void 0) { option = defaultOption; } var _option = option, _option$keyBgColor = _option.keyBgColor, keyBgColor = _option$keyBgColor === void 0 ? '#363636' : _option$keyBgColor, _option$valBgColor = _option.valBgColor, valBgColor = _option$valBgColor === void 0 ? '#175e9c' : _option$valBgColor; return ["%c " + key + " %c" + val, "padding: 2px 4px;font-size: 12px;background:" + keyBgColor + ";color:white;border-radius: 4px 0 0 4px;", "padding: 2px 4px;font-size: 12px;background:" + valBgColor + ";color:white;border-radius: 0 4px 4px 0;"]; }; var removeEmptyLines = function removeEmptyLines(str) { return str.replace(/[\s\r\n]+$/, ''); }; function runGitCommand (gitWorkTree, command, callback) { var gitCommand = gitWorkTree ? ['git', '--git-dir=' + path.join(gitWorkTree, '.git'), '--work-tree=' + gitWorkTree, command].join(' ') : ['git', command].join(' '); if (callback) { child_process.exec(gitCommand, function (err, stdout) { if (err) { return callback(err, ''); } callback(null, removeEmptyLines(stdout)); }); return null; } else { try { var r = removeEmptyLines("" + child_process.execSync(gitCommand)); return r; } catch (error) { console.error(error.message); return '-'; } } } var COMMITHASH_COMMAND = 'rev-parse HEAD'; var VERSION_COMMAND = 'describe --always'; var BRANCH_COMMAND = 'rev-parse --abbrev-ref HEAD'; var LASTCOMMITDATETIME_COMMAND = 'log -1 --format=%cI'; var Git = /*#__PURE__*/function () { function Git(options) { var _options; if (options === void 0) { options = {}; } this.gitWorkTree = options.gitWorkTree; this.commithashCommand = options.commithashCommand || COMMITHASH_COMMAND; this.versionCommand = options.versionCommand || VERSION_COMMAND + ((_options = options) != null && _options.lightweightTags ? ' --tags' : ''); this.createBranchFile = options.branch || false; this.branchCommand = options.branchCommand || BRANCH_COMMAND; this.lastCommitDateTimeCommand = options.lastCommitDateTimeCommand || LASTCOMMITDATETIME_COMMAND; if (options.versionCommand && options.lightweightTags) { throw new Error("lightweightTags can't be used together versionCommand"); } } var _proto = Git.prototype; _proto.commithash = function commithash() { return runGitCommand(this.gitWorkTree, this.commithashCommand); }; _proto.version = function version() { return runGitCommand(this.gitWorkTree, this.versionCommand); }; _proto.branch = function branch() { return runGitCommand(this.gitWorkTree, this.branchCommand); }; _proto.lastcommitdatetime = function lastcommitdatetime() { return runGitCommand(this.gitWorkTree, this.lastCommitDateTimeCommand); }; return Git; }(); /** * 空值显示字符 */ var EMPTY_STR = '-'; /** * 获取环境变量 */ var getEnv = function getEnv(key) { return process.env[key]; }; /** * nil 时输出 空值字符 */ var defaultToEmpty = function defaultToEmpty(value) { return value != null ? value : EMPTY_STR; }; var collectConsole = (function (option) { var consoleArr = []; // NODE_ENV if (option.NODE_ENV) { var NODE_ENV = getEnv('NODE_ENV'); consoleArr.push(getConsole('NODE_ENV', defaultToEmpty(NODE_ENV), { keyBgColor: '#20e014' })); } // git if (option.git) { var git = option.git; var gitIns = new Git(); // 分支 if (git.branch) { var branch = gitIns.branch(); consoleArr.push(getConsole('git 分支', defaultToEmpty(branch))); } // 哈希 if (git.hash) { var hash = gitIns.commithash(); consoleArr.push(getConsole('git hash', defaultToEmpty(hash == null ? void 0 : hash.slice(0, git.hash)))); } // 版本 if (git.version) { var version = gitIns.version(); consoleArr.push(getConsole('git 版本', defaultToEmpty(version))); } // 最近提交时间 if (git.lastCommitDateTime) { var lastCommitTime = gitIns.lastcommitdatetime(); consoleArr.push(getConsole('git 最近提交时间', defaultToEmpty(lastCommitTime))); } } // 自定义 if (option.custom) { var map = option.custom(); for (var _i = 0, _Object$entries = Object.entries(map); _i < _Object$entries.length; _i++) { var _Object$entries$_i = _Object$entries[_i], k = _Object$entries$_i[0], v = _Object$entries$_i[1]; consoleArr.push(getConsole(k, defaultToEmpty(v))); } } return consoleArr; }); // @ts-ignore safe-requre 无类型文件 var HtmlWebpackPlugin = /*#__PURE__*/safeRequire('html-webpack-plugin'); // 判断 插件 主版本号 var htmlWebpackPluginMajarVer = HtmlWebpackPlugin.version; // 打印版本号 log("\u68C0\u6D4B\u5230 html-webpack-plugin \u4E3B\u7248\u672C\u53F7: " + htmlWebpackPluginMajarVer); /** * webpack 美化打印 插件 */ var PrettyConsoleWebpackPlugin = /*#__PURE__*/function () { function PrettyConsoleWebpackPlugin(option) { this.option = Object.assign(DEFAULT_OPTION, option); } var _proto = PrettyConsoleWebpackPlugin.prototype; _proto.getCompilationHook = function getCompilationHook(compilation, name) { return compilation.hooks[name]; }; _proto.apply = function apply(compiler) { var _this = this; compiler.hooks.compilation.tap(PLUGIN_NAME, function (compilation) { var _ref; /** * 兼容旧版本 */ var hooksV1 = _this.getCompilationHook(compilation, 'htmlWebpackPluginBeforeHtmlProcessing'); var hooksV2 = _this.getCompilationHook(compilation, 'html-webpack-plugin-before-html-processing'); var alterAssetTagGroupsHook = (_ref = hooksV1 != null ? hooksV1 : hooksV2) != null ? _ref : HtmlWebpackPlugin.getHooks(compilation).alterAssetTagGroups; if (alterAssetTagGroupsHook) { alterAssetTagGroupsHook.tap(PLUGIN_NAME, function (data) { var consoleArr = collectConsole(_this.option); var consoleCode = "\n ;" + consoleArr.map(function (item) { return "console.log.apply(this, [" + item.map(function (f) { return "'" + f + "'"; }) + "])"; }).join(';') + ";\n "; var scriptContent = HtmlWebpackPlugin.createHtmlTagObject('script', undefined, consoleCode); data.headTags.unshift(scriptContent); }); } }); }; return PrettyConsoleWebpackPlugin; }(); exports.Git = Git; exports.PrettyConsoleWebpackPlugin = PrettyConsoleWebpackPlugin; exports.getConsole = getConsole; //# sourceMappingURL=pretty-console-webpack-plugin.cjs.development.js.map