UNPKG

rsbuild-plugin-publint

Version:

Run [publint](https://github.com/bluwy/publint) to lint npm packages after the build.

63 lines (62 loc) 4.43 kB
import * as __WEBPACK_EXTERNAL_MODULE_node_fs_promises_153e37e0__ from "node:fs/promises"; import * as __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__ from "node:path"; import * as __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__ from "@rsbuild/core"; import * as __WEBPACK_EXTERNAL_MODULE_picocolors__ from "picocolors"; const pluginPublint = (options = {})=>({ name: 'plugin-publint', setup (api) { const { enable = true, throwOn = 'error' } = options; if (!enable) return; api.onAfterBuild({ handler: async ({ isFirstCompile })=>{ if (!isFirstCompile) return; const { publint } = await import("publint"); const { formatMessage } = await import("publint/utils"); const mergedOptions = { pkgDir: api.context.rootPath, ...options.publintOptions }; const { messages } = await publint(mergedOptions); const pkg = JSON.parse(await __WEBPACK_EXTERNAL_MODULE_node_fs_promises_153e37e0__["default"].readFile((0, __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__.join)(mergedOptions.pkgDir, 'package.json'), 'utf8')); const formatted = { errors: [], warnings: [], suggestions: [] }; for (const message of messages)if ('error' === message.type) formatted.errors.push(formatMessage(message, pkg)); else if ('warning' === message.type) formatted.warnings.push(formatMessage(message, pkg)); else formatted.suggestions.push(formatMessage(message, pkg)); if (0 === formatted.errors.length && 0 === formatted.warnings.length && 0 === formatted.suggestions.length) __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.logger.info(__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].green('Publint passed.')); if (formatted.errors.length > 0) { __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.logger.error(__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].red(`Publint found ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].bold(formatted.errors.length)} errors:`)); for (const message of formatted.errors)if (message) __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.logger.error(message); console.log(); } if (formatted.warnings.length > 0) { __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.logger.warn(__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].yellow(`Publint found ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].bold(formatted.warnings.length)} warnings:`)); for (const message of formatted.warnings)if (message) __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.logger.warn(message); console.log(); } if (formatted.suggestions.length > 0) { __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.logger.info(__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].cyan(`Publint found ${__WEBPACK_EXTERNAL_MODULE_picocolors__["default"].bold(formatted.suggestions.length)} suggestions:`)); for (const message of formatted.suggestions)if (message) __WEBPACK_EXTERNAL_MODULE__rsbuild_core_1b356efc__.logger.info(message); } const shouldThrow = ()=>{ switch(throwOn){ case 'suggestion': return formatted.suggestions.length > 0 || formatted.warnings.length > 0 || formatted.errors.length > 0; case 'warning': return formatted.warnings.length > 0 || formatted.errors.length > 0; case 'error': return formatted.errors.length > 0; case 'never': return false; } }; if (shouldThrow()) throw new Error('Publint failed!'); }, order: 'post' }); } }); export { pluginPublint };