diff --git a/src/index.ts b/src/index.ts index 0927a39..b268054 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,6 +19,8 @@ function unpluginFactory(userOpts: PluginOptions): UnpluginOptions { }, transform(code, id) { + CSSModuleError.filename = id; + let plugins: PluginItem[] = ["@babel/plugin-syntax-jsx", Plugin]; const isTSX = /\.tsx$/i.test(id); @@ -33,7 +35,7 @@ function unpluginFactory(userOpts: PluginOptions): UnpluginOptions { }); if (!result || !result.code) { - throw new CSSModuleError(`Could not transform ${id}`); + throw new CSSModuleError("Failed to transform " + id); } return { code: result.code, map: result.map }; diff --git a/src/plugin.ts b/src/plugin.ts index 0c38d6f..7b69fb2 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -7,14 +7,14 @@ import { transformClassNames, transformImport } from "./transforms.js"; import { CSSModuleError } from "./utils.js"; function ImportDeclaration(path: NodePath, { pluginState }: PluginPass) { + // saving node for error messages + CSSModuleError.node = path.node; + // we're only interested in scss/sass/css imports if (!/.module.(s[ac]ss|css)(:.*)?$/iu.test(path.node.source.value)) { return; } - // saving path for error messages - CSSModuleError.path = path; - // 1. Transform import declaration const idGenerator = (hint: string) => path.scope.generateUidIdentifier(hint); const res = transformImport(path.node, idGenerator); @@ -44,6 +44,9 @@ function ImportDeclaration(path: NodePath, { pluginState }: } function JSXAttribute(path: NodePath, { pluginState }: PluginPass) { + // saving node for error messages + CSSModuleError.node = path.node; + const firstNamedModule = getFirstNamedModule(pluginState.modules.namedModules); // we only support className attribute having a string value @@ -56,9 +59,6 @@ function JSXAttribute(path: NodePath, { pluginState }: PluginPas return; } - // saving path for error messages - CSSModuleError.path = path; - // if no default modules is available, make the first modules as default if (!pluginState.modules.defaultModule) { if (firstNamedModule) { diff --git a/src/utils.ts b/src/utils.ts index 2fd57ba..afde4c1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,24 +1,14 @@ -import { NodePath } from "@babel/core"; +import type { Node } from "@babel/core"; import chalk from "chalk"; export class CSSModuleError extends Error { - errorMessage: string; - static path: NodePath | undefined; + static filename?: string; + static node?: Node; constructor(errorMessage: string) { super(); - this.errorMessage = errorMessage; this.name = chalk.red("CSSModuleError"); - this.message = `at (${CSSModuleError.path?.node.loc?.start.line}:${CSSModuleError.path?.node.loc?.start.column}): - ${this.errorMessage.replace(/ +/g, " ")} - `.replace(/ +/g, " "); - } - - static cls(cls: string) { - return `'${chalk.cyan(cls)}'`; - } - - static mod(mod: string) { - return `'${chalk.cyan(mod)}'`; + this.message = errorMessage; + this.message += "\n at " + (CSSModuleError.filename ?? "") + ":" + CSSModuleError.node?.loc?.start.line + ":" + CSSModuleError.node?.loc?.start.column; } }