From 6a0068902e3cd9eae1a406eaa7f07d05e9fcec94 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 29 Apr 2023 00:14:14 +0300 Subject: [PATCH 1/5] chore(deps): bump yaml from 2.2.1 to 2.2.2 (#648) Bumps [yaml](https://github.com/eemeli/yaml) from 2.2.1 to 2.2.2. - [Release notes](https://github.com/eemeli/yaml/releases) - [Commits](https://github.com/eemeli/yaml/compare/v2.2.1...v2.2.2) --- updated-dependencies: - dependency-name: yaml dependency-type: indirect ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9d99fc67..db7ee207 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11494,9 +11494,9 @@ "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" }, "yaml": { - "version": "2.2.1", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.2.1.tgz", - "integrity": "sha512-e0WHiYql7+9wr4cWMx3TVQrNwejKaEe7/rHNmQmqRjazfOP5W8PB6Jpebb5o6fIapbz9o9+2ipcaTM2ZwDI6lw==", + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.2.2.tgz", + "integrity": "sha512-CBKFWExMn46Foo4cldiChEzn7S7SRV+wqiluAb6xmueD/fGyRHIhX8m14vVGgeFWjN540nKCNVj6P21eQjgTuA==", "dev": true }, "yargs": { From 0a538202ef0011c28cef0ddcb6d3b58179dada4b Mon Sep 17 00:00:00 2001 From: Sachin Kumar Date: Sun, 7 May 2023 02:44:38 +0530 Subject: [PATCH 2/5] chore: add scripts to fix lint (#651) --- package.json | 3 +++ 1 file changed, 3 insertions(+) diff --git a/package.json b/package.json index 084722a0..1f8c661c 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,9 @@ "lint:js": "eslint --cache .", "lint:spelling": "cspell \"**/*.*\"", "lint": "npm-run-all -l -p \"lint:**\"", + "fix:js": "npm run lint:js -- --fix", + "fix:prettier": "npm run lint:prettier -- --write", + "fix": "npm-run-all -l fix:js fix:prettier", "test:only": "cross-env NODE_ENV=test jest", "test:watch": "npm run test:only -- --watch", "test:coverage": "npm run test:only -- --collectCoverageFrom=\"src/**/*.js\" --coverage", From a98d7d07c9e130414f25e0ecf4667506956061f2 Mon Sep 17 00:00:00 2001 From: Sachin Kumar Date: Fri, 12 May 2023 18:54:21 +0530 Subject: [PATCH 3/5] docs: add GitHub discussions badge (#652) --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 98140cc1..4af79a10 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ [![coverage][cover]][cover-url] [![size][size]][size-url] -Webpack chat: [![chat][chat]][chat-url] +Webpack discussion: [![discussion][discussion]][discussion-url] PostCSS chat: [![chat-postcss][chat-postcss]][chat-postcss-url] @@ -1167,8 +1167,8 @@ Please take a moment to read our contributing guidelines if you haven't yet done [tests-url]: https://github.com/webpack-contrib/postcss-loader/actions [cover]: https://codecov.io/gh/webpack-contrib/postcss-loader/branch/master/graph/badge.svg [cover-url]: https://codecov.io/gh/webpack-contrib/postcss-loader -[chat]: https://badges.gitter.im/webpack/webpack.svg -[chat-url]: https://gitter.im/webpack/webpack +[discussion]: https://img.shields.io/github/discussions/webpack/webpack +[discussion-url]: https://github.com/webpack/webpack/discussions [chat-postcss]: https://badges.gitter.im/postcss/postcss.svg [chat-postcss-url]: https://gitter.im/postcss/postcss [size]: https://packagephobia.now.sh/badge?p=postcss-loader From 65748ece396d0b38713783495d8a64f128d3992b Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Fri, 26 May 2023 04:01:12 +0300 Subject: [PATCH 4/5] fix: warning and error serialization --- src/Error.js | 39 ---------- src/Warning.js | 32 -------- src/index.js | 22 ++++-- src/utils.js | 67 ++++++++++++++--- .../__snapshots__/implementation.test.js.snap | 4 +- test/__snapshots__/loader.test.js.snap | 73 +++++++++++++------ 6 files changed, 120 insertions(+), 117 deletions(-) delete mode 100644 src/Error.js delete mode 100644 src/Warning.js diff --git a/src/Error.js b/src/Error.js deleted file mode 100644 index 45fa779c..00000000 --- a/src/Error.js +++ /dev/null @@ -1,39 +0,0 @@ -/** - * **PostCSS Syntax Error** - * - * Loader wrapper for postcss syntax errors - * - * @class SyntaxError - * @extends Error - * - * @param {Object} err CssSyntaxError - */ -class SyntaxError extends Error { - constructor(error) { - super(error); - - const { line, column, reason, plugin, file } = error; - - this.name = "SyntaxError"; - - this.message = `${this.name}\n\n`; - - if (typeof line !== "undefined") { - this.message += `(${line}:${column}) `; - } - - this.message += plugin ? `${plugin}: ` : ""; - this.message += file ? `${file} ` : " "; - this.message += `${reason}`; - - const code = error.showSourceCode(); - - if (code) { - this.message += `\n\n${code}\n`; - } - - this.stack = false; - } -} - -module.exports = SyntaxError; diff --git a/src/Warning.js b/src/Warning.js deleted file mode 100644 index 56e7684e..00000000 --- a/src/Warning.js +++ /dev/null @@ -1,32 +0,0 @@ -/** - * **PostCSS Plugin Warning** - * - * Loader wrapper for postcss plugin warnings (`root.messages`) - * - * @class Warning - * @extends Error - * - * @param {Object} warning PostCSS Warning - */ -class Warning extends Error { - constructor(warning) { - super(warning); - - const { text, line, column, plugin } = warning; - - this.name = "Warning"; - - this.message = `${this.name}\n\n`; - - if (typeof line !== "undefined") { - this.message += `(${line}:${column}) `; - } - - this.message += plugin ? `${plugin}: ` : ""; - this.message += `${text}`; - - this.stack = false; - } -} - -module.exports = Warning; diff --git a/src/index.js b/src/index.js index db014c1e..71ed8b1f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,7 @@ import path from "path"; -import { satisfies } from "semver"; import postcssPackage from "postcss/package.json"; -import Warning from "./Warning"; import schema from "./options.json"; import { loadConfig, @@ -14,6 +12,7 @@ import { findPackageJSONDir, getPostcssImplementation, reportError, + warningFactory, } from "./utils"; let hasExplicitDependencyOnPostCSS = false; @@ -40,9 +39,17 @@ export default async function loader(content, sourceMap, meta) { ? true : options.postcssOptions.config; - const postcssFactory = getPostcssImplementation(this, options.implementation); + let implementation; - if (!postcssFactory) { + try { + implementation = getPostcssImplementation(this, options.implementation); + } catch (error) { + callback(error); + + return; + } + + if (!implementation) { callback( new Error( `The Postcss implementation "${options.implementation}" not found` @@ -98,7 +105,8 @@ export default async function loader(content, sourceMap, meta) { meta && meta.ast && meta.ast.type === "postcss" && - satisfies(meta.ast.version, `^${postcssPackage.version}`) + // eslint-disable-next-line global-require + require("semver").satisfies(meta.ast.version, `^${postcssPackage.version}`) ) { ({ root } = meta.ast); } @@ -112,7 +120,7 @@ export default async function loader(content, sourceMap, meta) { let processor; try { - processor = postcssFactory(plugins); + processor = implementation(plugins); result = await processor.process(root || content, processOptions); } catch (error) { // Check postcss versions to avoid using PostCSS 7. @@ -175,7 +183,7 @@ export default async function loader(content, sourceMap, meta) { } for (const warning of result.warnings()) { - this.emitWarning(new Warning(warning)); + this.emitWarning(warningFactory(warning)); } for (const message of result.messages) { diff --git a/src/utils.js b/src/utils.js index d93e50c2..0c7423bb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -5,8 +5,6 @@ import Module from "module"; import { klona } from "klona/full"; import { cosmiconfig, defaultLoaders } from "cosmiconfig"; -import SyntaxError from "./Error"; - const parentModule = module; const stat = (inputFileSystem, filePath) => @@ -541,15 +539,8 @@ function getPostcssImplementation(loaderContext, implementation) { if (!implementation || typeof implementation === "string") { const postcssImplPkg = implementation || "postcss"; - try { - // eslint-disable-next-line import/no-dynamic-require, global-require - resolvedImplementation = require(postcssImplPkg); - } catch (error) { - loaderContext.emitError(error); - - // eslint-disable-next-line consistent-return - return; - } + // eslint-disable-next-line import/no-dynamic-require, global-require + resolvedImplementation = require(postcssImplPkg); } // eslint-disable-next-line consistent-return @@ -562,12 +553,63 @@ function reportError(loaderContext, callback, error) { } if (error.name === "CssSyntaxError") { - callback(new SyntaxError(error)); + callback(syntaxErrorFactory(error)); } else { callback(error); } } +function warningFactory(obj) { + let message = ""; + + if (typeof obj.line !== "undefined") { + message += `(${obj.line}:${obj.column}) `; + } + + if (typeof obj.plugin !== "undefined") { + message += `from "${obj.plugin}" plugin: `; + } + + message += obj.text; + + if (obj.node) { + message += `\n\nCode:\n ${obj.node.toString()}\n`; + } + + const warning = new Error(message); + + warning.stack = null; + + return warning; +} + +function syntaxErrorFactory(obj) { + let message = "\nSyntaxError\n\n"; + + if (typeof obj.line !== "undefined") { + message += `(${obj.line}:${obj.column}) `; + } + + if (typeof obj.plugin !== "undefined") { + message += `from "${obj.plugin}" plugin: `; + } + + message += obj.file ? `${obj.file} ` : " "; + message += `${obj.reason}`; + + const code = obj.showSourceCode(); + + if (code) { + message += `\n\n${code}\n`; + } + + const error = new Error(message); + + error.stack = null; + + return error; +} + export { loadConfig, getPostcssOptions, @@ -577,4 +619,5 @@ export { findPackageJSONDir, getPostcssImplementation, reportError, + warningFactory, }; diff --git a/test/__snapshots__/implementation.test.js.snap b/test/__snapshots__/implementation.test.js.snap index 19a6a28d..b6700296 100644 --- a/test/__snapshots__/implementation.test.js.snap +++ b/test/__snapshots__/implementation.test.js.snap @@ -3,9 +3,7 @@ exports[`"implementation" option should throw error when unresolved package: errors 1`] = ` [ "ModuleBuildError: Module build failed (from \`replaced original path\`): -Error: The Postcss implementation "unresolved" not found", - "ModuleError: Module Error (from \`replaced original path\`): -(Emitted value instead of an instance of Error) Error: Cannot find module 'unresolved' from 'src/utils.js'", +NonErrorEmittedError: (Emitted value instead of an instance of Error) Error: Cannot find module 'unresolved' from 'src/utils.js'", ] `; diff --git a/test/__snapshots__/loader.test.js.snap b/test/__snapshots__/loader.test.js.snap index 0eb2767d..3eaab607 100644 --- a/test/__snapshots__/loader.test.js.snap +++ b/test/__snapshots__/loader.test.js.snap @@ -106,53 +106,77 @@ exports[`loader should emit warning using the "messages" API: errors 1`] = `[]`; exports[`loader should emit warning using the "messages" API: warnings 1`] = ` [ "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(10:3) from "postcss-plugin" plugin: -(10:3) postcss-plugin: ", +Code: + color: green +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(14:3) from "postcss-plugin" plugin: -(14:3) postcss-plugin: ", +Code: + color: blue +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(18:3) from "postcss-plugin" plugin: -(18:3) postcss-plugin: ", +Code: + -x-border-color: blue blue * +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(19:3) from "postcss-plugin" plugin: -(19:3) postcss-plugin: ", +Code: + -x-color: * #fafafa +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(23:3) from "postcss-plugin" plugin: -(23:3) postcss-plugin: ", +Code: + -z-border-color: blue blue * +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(24:3) from "postcss-plugin" plugin: -(24:3) postcss-plugin: ", +Code: + -z-color: * #fafafa +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(29:5) from "postcss-plugin" plugin: -(29:5) postcss-plugin: ", +Code: + width: 500px +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(2:3) from "postcss-plugin" plugin: -(2:3) postcss-plugin: ", +Code: + color: black +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(32:7) from "postcss-plugin" plugin: -(32:7) postcss-plugin: ", +Code: + width: auto +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(36:7) from "postcss-plugin" plugin: -(36:7) postcss-plugin: ", +Code: + color: white +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(41:5) from "postcss-plugin" plugin: -(41:5) postcss-plugin: ", +Code: + display: block +", "ModuleWarning: Module Warning (from \`replaced original path\`): -Warning +(6:3) from "postcss-plugin" plugin: -(6:3) postcss-plugin: ", +Code: + color: red +", ] `; @@ -214,6 +238,7 @@ exports[`loader should reuse PostCSS AST: warnings 1`] = `[]`; exports[`loader should throw an error on invalid syntax: errors 1`] = ` [ "ModuleBuildError: Module build failed (from \`replaced original path\`): + SyntaxError (1:3) /test/fixtures/css/style.css Unnecessary curly bracket From 7e5eaeb621dfa62e97364da96202c35fd6599ce4 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Fri, 26 May 2023 04:06:54 +0300 Subject: [PATCH 5/5] chore(release): 7.3.1 --- CHANGELOG.md | 7 +++++++ package-lock.json | 2 +- package.json | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6c85d2a..cb3ed280 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [7.3.1](https://github.com/webpack-contrib/postcss-loader/compare/v7.3.0...v7.3.1) (2023-05-26) + + +### Bug Fixes + +* warning and error serialization ([65748ec](https://github.com/webpack-contrib/postcss-loader/commit/65748ece396d0b38713783495d8a64f128d3992b)) + ## [7.3.0](https://github.com/webpack-contrib/postcss-loader/compare/v7.2.4...v7.3.0) (2023-04-28) diff --git a/package-lock.json b/package-lock.json index db7ee207..4ee3dc57 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "postcss-loader", - "version": "7.3.0", + "version": "7.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 1f8c661c..3a620a29 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "postcss-loader", - "version": "7.3.0", + "version": "7.3.1", "description": "PostCSS loader for webpack", "license": "MIT", "repository": "webpack-contrib/postcss-loader",