From a3d628af930be1b2de61c15db310f1275e8f3052 Mon Sep 17 00:00:00 2001 From: Brad Cornes Date: Tue, 25 Aug 2020 16:59:13 +0100 Subject: [PATCH] On tailwind error prevent crash and show message (#156) --- src/class-names/index.js | 55 ++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/src/class-names/index.js b/src/class-names/index.js index 83a15b1..c456d5b 100644 --- a/src/class-names/index.js +++ b/src/class-names/index.js @@ -17,17 +17,6 @@ import { getUtilityConfigMap } from './getUtilityConfigMap' import glob from 'fast-glob' import normalizePath from 'normalize-path' -function TailwindConfigError(error) { - Error.call(this) - Error.captureStackTrace(this, this.constructor) - - this.name = this.constructor.name - this.message = error.message - this.stack = error.stack -} - -util.inherits(TailwindConfigError, Error) - function arraysEqual(arr1, arr2) { return ( JSON.stringify(arr1.concat([]).sort()) === @@ -98,23 +87,34 @@ export default async function getClassNames( try { config = __non_webpack_require__(configPath) } catch (error) { - throw new TailwindConfigError(error) + hook.unwatch() + hook.unhook() + throw error } + hook.unwatch() - const [base, components, utilities] = await Promise.all( - [ - semver.gte(version, '0.99.0') ? 'base' : 'preflight', - 'components', - 'utilities', - ].map((group) => - postcss([tailwindcss(configPath)]).process(`@tailwind ${group};`, { - from: undefined, - }) - ) - ) + let postcssResult - hook.unhook() + try { + postcssResult = await Promise.all( + [ + semver.gte(version, '0.99.0') ? 'base' : 'preflight', + 'components', + 'utilities', + ].map((group) => + postcss([tailwindcss(configPath)]).process(`@tailwind ${group};`, { + from: undefined, + }) + ) + ) + } catch (error) { + throw error + } finally { + hook.unhook() + } + + const [base, components, utilities] = postcssResult if (typeof userSeperator !== 'undefined') { dset(config, sepLocation, userSeperator) @@ -180,12 +180,7 @@ export default async function getClassNames( try { result = await run() } catch (error) { - if (error instanceof TailwindConfigError) { - onChange({ error }) - } else { - unwatch() - onChange(null) - } + onChange({ error }) return } const newDeps = [result.configPath, ...result.dependencies]