Skip to content

Commit

Permalink
feat: load tailwindcss config use jiti
Browse files Browse the repository at this point in the history
  • Loading branch information
caohuilin committed Jan 23, 2025
1 parent b69c8fa commit 7d685e3
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 79 deletions.
1 change: 0 additions & 1 deletion packages/cli/plugin-tailwind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"test": "jest --passWithNoTests"
},
"dependencies": {
"@modern-js/node-bundle-require": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"@modern-js/utils": "workspace:*",
"@swc/helpers": "0.5.13",
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/plugin-tailwind/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getTwinMacroMajorVersion,
template,
} from './macro';
import type { TailwindConfig } from './types';
import { getTailwindPath, getTailwindVersion } from './utils';

export const tailwindcssPlugin = (
Expand Down Expand Up @@ -72,7 +73,7 @@ export const tailwindcssPlugin = (

if (
tailwindVersion === '3' &&
userTailwindConfig.content &&
(userTailwindConfig as { content: TailwindConfig }).content &&
!modernConfig?.tools?.tailwindcss &&
!modernConfig?.source?.designSystem
) {
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/plugin-tailwind/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path';
import { bundleRequire } from '@modern-js/node-bundle-require';
import { loadConfigContent } from '@modern-js/utils';
import { fs, applyOptionsChain, findExists } from '@modern-js/utils';
import { cloneDeep } from '@modern-js/utils/lodash';
import type {
Expand Down Expand Up @@ -43,10 +43,10 @@ export async function loadConfigFile(appDirectory: string) {
const configFile = findExists(configs);

if (configFile) {
const mod = await bundleRequire(configFile);
const content = await loadConfigContent<TailwindConfig>(configFile);
return {
path: configFile,
content: mod.default || mod,
content,
};
}

Expand Down
1 change: 0 additions & 1 deletion packages/toolkit/plugin-v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
"test": "jest"
},
"dependencies": {
"jiti": "1.21.7",
"@modern-js/utils": "workspace:*",
"@modern-js/runtime-utils": "workspace:*",
"@swc/helpers": "0.5.13"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import path from 'path';
import {
fs,
CONFIG_FILE_EXTENSIONS,
chalk,
findExists,
getCommand,
getNodeEnv,
isDevCommand,
Expand Down
37 changes: 1 addition & 36 deletions packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Stats } from 'fs';
import path from 'path';
import { fs, CONFIG_CACHE_DIR, globby } from '@modern-js/utils';
import jiti from 'jiti';
import { fs, globby, loadConfigContent } from '@modern-js/utils';
/**
* Get user config from package.json.
* @param appDirectory - App root directory.
Expand Down Expand Up @@ -56,40 +55,6 @@ export const clearFilesOverTime = async (
}
};

/**
* Load a configuration file dynamically
* @param {string} configFile - Path to the configuration file (absolute or relative)
* @returns {any} - The loaded configuration object
*/
function loadConfigContent<T>(configFile: string): T {
// Create a jiti instance
const _require = jiti(__filename, {
esmResolve: true,
// disable require cache to support restart CLI and read the new config
requireCache: false,
interopDefault: true,
});
// Check if the file exists
if (!fs.existsSync(configFile)) {
throw new Error(`Configuration file does not exist: ${configFile}`);
}

try {
// Dynamically load the configuration file using jiti
const config = _require(configFile);

// If the file exports as ESM, access the `default` property
return config.default || config;
} catch (e: any) {
if (e instanceof Error) {
e.message = `Get Error while loading config file: ${configFile}, please check it and retry.\n${
e.message || ''
}`;
}
throw e;
}
}

/**
* Parse and load user config file, support extensions like .ts, mjs, js, ejs.
* @param appDirectory - App root directory, from which start search user config file.
Expand Down
3 changes: 2 additions & 1 deletion packages/toolkit/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@
"@swc/helpers": "0.5.13",
"caniuse-lite": "^1.0.30001520",
"lodash": "^4.17.21",
"rslog": "^1.1.0"
"rslog": "^1.1.0",
"jiti": "1.21.7"
},
"devDependencies": {
"@modern-js/types": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/toolkit/utils/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ export * from './config';
export * from './action';
export * from './version';
export * from './route';
export * from './loadConfig';
35 changes: 35 additions & 0 deletions packages/toolkit/utils/src/cli/loadConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import fs from 'fs';
import jiti from 'jiti';
/**
* Load a configuration file dynamically
* @param {string} configFile - Path to the configuration file (absolute or relative)
* @returns {any} - The loaded configuration object
*/
export function loadConfigContent<T>(configFile: string): T {
// Create a jiti instance
const _require = jiti(__filename, {
esmResolve: true,
// disable require cache to support restart CLI and read the new config
requireCache: false,
interopDefault: true,
});
// Check if the file exists
if (!fs.existsSync(configFile)) {
throw new Error(`Configuration file does not exist: ${configFile}`);
}

try {
// Dynamically load the configuration file using jiti
const config = _require(configFile);

// If the file exports as ESM, access the `default` property
return config.default || config;
} catch (e: any) {
if (e instanceof Error) {
e.message = `Get Error while loading config file: ${configFile}, please check it and retry.\n${
e.message || ''
}`;
}
throw e;
}
}
37 changes: 3 additions & 34 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7d685e3

Please sign in to comment.