Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: warning serilization
  • Loading branch information
alexander-akait committed May 26, 2023
commit 015a6580b374e71897d6910483180c7aad2fe99e
32 changes: 0 additions & 32 deletions src/Warning.js

This file was deleted.

22 changes: 15 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -14,6 +12,7 @@ import {
findPackageJSONDir,
getPostcssImplementation,
reportError,
warningFactor,
} from "./utils";

let hasExplicitDependencyOnPostCSS = false;
Expand All @@ -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`
Expand Down Expand Up @@ -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);
}
Expand All @@ -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.
Expand Down Expand Up @@ -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(warningFactor(warning));
}

for (const message of result.messages) {
Expand Down
36 changes: 27 additions & 9 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,15 +541,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
Expand All @@ -568,6 +561,30 @@ function reportError(loaderContext, callback, error) {
}
}

function warningFactor(obj) {
let message = "\nWARNING ";

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;
}

export {
loadConfig,
getPostcssOptions,
Expand All @@ -577,4 +594,5 @@ export {
findPackageJSONDir,
getPostcssImplementation,
reportError,
warningFactor,
};