Skip to content

Commit

Permalink
fix: 🐛 fallback to default config if tailwind config has error
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Sep 20, 2022
1 parent 78bcf37 commit 830e124
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,17 @@ export function activate(context: ExtensionContext) {

const runtimeConfig = readRuntimeConfig(document.uri.fsPath);

let tailwindConfig, tailwindConfigPath;

if (runtimeConfig?.sortTailwindcssClasses) {
tailwindConfigPath = resolveTailwindConfig(document.uri.fsPath, runtimeConfig?.tailwindcssConfigPath ?? '');
tailwindConfig = requireUncached(tailwindConfigPath);
runtimeConfig.tailwindcssConfigPath = '';
const tailwindConfigPath = resolveTailwindConfig(document.uri.fsPath, runtimeConfig?.tailwindcssConfigPath ?? '');
runtimeConfig.tailwindcssConfigPath = tailwindConfigPath;

try {
requireUncached(tailwindConfigPath);
} catch (error) {
// @ts-ignore
// fallback to default config
runtimeConfig.tailwindcssConfigPath = __non_webpack_require__.resolve('tailwindcss/lib/public/default-config');
}
}

const options = {
Expand All @@ -89,7 +94,6 @@ export function activate(context: ExtensionContext) {
wrapAttributes: extConfig.wrapAttributes,
useTabs: extConfig.useTabs,
sortTailwindcssClasses: extConfig.sortTailwindcssClasses,
tailwindcssConfig: tailwindConfig,
sortHtmlAttributes: extConfig.sortHtmlAttributes ?? 'none',
noMultipleEmptyLines: extConfig.noMultipleEmptyLines,
...runtimeConfig, // override all settings by runtime config
Expand Down
2 changes: 2 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
declare module 'blade-formatter';
declare module 'tailwindcss/lib/public/default-config';

1 change: 1 addition & 0 deletions src/tailwind.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import findConfig from 'find-config';
import path from 'path';
import { findConfigFile } from './runtimeConfig';
import { requireUncached } from './util';

const __config__ = 'tailwind.config.js';

Expand Down
1 change: 1 addition & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ export function requireUncached(moduleName: string) {
// @ts-ignore
return __non_webpack_require__(moduleName);
} catch (err: any) {
throw err;
}
}

0 comments on commit 830e124

Please sign in to comment.