UNPKG

stylelint-webpack-plugin

Version:
71 lines (63 loc) 2.63 kB
"use strict"; function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } /** @typedef {import('./getStylelint').Stylelint} Stylelint */ /** @typedef {import('./getStylelint').LinterOptions} StylelintOptions */ /** @typedef {import('./getStylelint').LintResult} LintResult */ /** @typedef {import('./options').Options} Options */ /** @type {string} */ let stylelintPath = "stylelint"; /** @type {Partial<StylelintOptions>} */ let linterOptions; /** @type {Promise<Stylelint> | null} */ let stylelintPromise = null; /** * Lazily load stylelint on first use * @returns {Promise<Stylelint>} stylelint instance */ async function getStylelint() { if (!stylelintPromise) { stylelintPromise = (async () => { const mod = await (specifier => new Promise(r => r(`${specifier}`)).then(s => _interopRequireWildcard(require(s))))(stylelintPath); // Handle both CJS (v13-v16) and ESM (v17) exports return mod.default || mod; })(); } return stylelintPromise; } /** * @param {Options} options the worker options * @param {Partial<StylelintOptions>} stylelintOptions the stylelint options */ function setup(options, stylelintOptions) { stylelintPath = options.stylelintPath || "stylelint"; linterOptions = stylelintOptions; // Reset cached stylelint in case path changed stylelintPromise = null; } /** * @param {string | string[]} files files * @returns {Promise<LintResult[]>} results */ async function lintFiles(files) { const stylelint = await getStylelint(); const { results } = await stylelint.lint({ ...linterOptions, files, quietDeprecationWarnings: true }); // Reset result to work with worker return results.map(result => ({ source: result.source, errored: result.errored, ignored: result.ignored, warnings: result.warnings, deprecations: result.deprecations, invalidOptionWarnings: result.invalidOptionWarnings, parseErrors: result.parseErrors })); } module.exports.getStylelint = getStylelint; module.exports.lintFiles = lintFiles; module.exports.setup = setup;