Skip to content

Commit

Permalink
feat: 🎸 add option to specify tailwindcss configuration
Browse files Browse the repository at this point in the history
Add `tailwindcssConfigPath`, `tailwindcssConfig` for formatter option.\n
Add `--tailwindcss-config-path` for CLI option.
  • Loading branch information
shufo committed Sep 18, 2022
1 parent 84a4ca2 commit f21b892
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ export default class Formatter {
return p2;
}

if (this.options.tailwindcssConfigPath) {
const options = { tailwindConfigPath: this.options.tailwindcssConfigPath };
return sortClasses(p2, options);
}

if (this.options.tailwindcssConfig) {
const options = { tailwindConfig: this.options.tailwindcssConfig };
return sortClasses(p2, options);
}

return sortClasses(p2);
});
}
Expand Down
33 changes: 33 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import _ from 'lodash';
import findConfig from 'find-config';
import Formatter from './formatter';
import * as util from './util';
import { Config as TailwindConfig } from 'tailwindcss/types/config';
import {
findRuntimeConfig,
readRuntimeConfig,
Expand All @@ -35,6 +36,8 @@ export interface FormatterOption {
endWithNewline?: boolean;
useTabs?: boolean;
sortTailwindcssClasses?: true;
tailwindcssConfigPath?: string;
tailwindcssConfig?: TailwindConfig;
sortHtmlAttributes?: SortHtmlAttributes;
noMultipleEmptyLines?: boolean;
}
Expand Down Expand Up @@ -82,6 +85,7 @@ class BladeFormatter {
async format(content: any, opts = {}) {
const options = this.options || opts;
await this.readIgnoreFile(process.cwd());
await this.findTailwindConfig(process.cwd());
await this.readRuntimeConfig(process.cwd());
return new Formatter(options).formatContent(content).catch((err) => {
throw new FormatError(err);
Expand Down Expand Up @@ -130,6 +134,30 @@ class BladeFormatter {
}
}

async findTailwindConfig(filePath: string) {
if (!this.options.sortTailwindcssClasses) {
return;
}

const configFilename = 'tailwind.config.js';

let configFilePath: string | null | undefined;

if (this.options.tailwindcssConfigPath) {
configFilePath = nodepath.resolve(this.options.tailwindcssConfigPath);
} else {
// lookup tailwind config
const workingDir = nodepath.dirname(filePath);
configFilePath = findConfig(configFilename, { cwd: workingDir });
}

if (!configFilePath) {
return;
}

this.options.tailwindcssConfigPath = configFilePath;
}

async readRuntimeConfig(filePath: string): Promise<RuntimeConfig | undefined> {
if (_.isEmpty(this.runtimeConfigCache)) {
this.options = _.merge(this.options, this.runtimeConfigCache);
Expand All @@ -151,6 +179,10 @@ class BladeFormatter {
const options = await readRuntimeConfig(configFile);
this.options = _.merge(this.options, options);
this.runtimeConfigCache = this.options;

if (this.options.sortTailwindcssClasses) {
await this.findTailwindConfig(filePath);
}
} catch (error: any) {
if (error instanceof SyntaxError) {
process.stdout.write(chalk.red.bold('\nBlade Formatter JSON Syntax Error: \n\n'));
Expand Down Expand Up @@ -213,6 +245,7 @@ class BladeFormatter {
}

async formatFile(path: any) {
await this.findTailwindConfig(path);
await this.readRuntimeConfig(path);

await util
Expand Down
2 changes: 2 additions & 0 deletions src/runtimeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface RuntimeConfig {
endWithNewline?: boolean;
useTabs?: boolean;
sortTailwindcssClasses?: boolean;
tailwindcssConfigPath?: string;
sortHtmlAttributes?: SortHtmlAttributes;
noMultipleEmptyLines?: boolean;
}
Expand Down Expand Up @@ -72,6 +73,7 @@ export async function readRuntimeConfig(filePath: string | null): Promise<Runtim
endWithNewline: { type: 'boolean', nullable: true },
useTabs: { type: 'boolean', nullable: true },
sortTailwindcssClasses: { type: 'boolean', nullable: true },
tailwindcssConfigPath: { type: 'string', nullable: true },
sortHtmlAttributes: {
type: 'string',
enum: ['none', 'alphabetical', 'code-guide', 'idiomatic', 'vuejs'],
Expand Down

0 comments on commit f21b892

Please sign in to comment.