diff --git a/packages/cli/plugin-tailwind/package.json b/packages/cli/plugin-tailwind/package.json index 035adafaa0b1..5b363b9b61e6 100644 --- a/packages/cli/plugin-tailwind/package.json +++ b/packages/cli/plugin-tailwind/package.json @@ -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", diff --git a/packages/cli/plugin-tailwind/src/cli.ts b/packages/cli/plugin-tailwind/src/cli.ts index 122b07f1df43..da9de9b8dcf2 100644 --- a/packages/cli/plugin-tailwind/src/cli.ts +++ b/packages/cli/plugin-tailwind/src/cli.ts @@ -10,6 +10,7 @@ import { getTwinMacroMajorVersion, template, } from './macro'; +import type { TailwindConfig } from './types'; import { getTailwindPath, getTailwindVersion } from './utils'; export const tailwindcssPlugin = ( @@ -72,7 +73,7 @@ export const tailwindcssPlugin = ( if ( tailwindVersion === '3' && - userTailwindConfig.content && + (userTailwindConfig as { content: TailwindConfig }).content && !modernConfig?.tools?.tailwindcss && !modernConfig?.source?.designSystem ) { diff --git a/packages/cli/plugin-tailwind/src/config.ts b/packages/cli/plugin-tailwind/src/config.ts index 594244a13210..9b1a3e6d61c1 100644 --- a/packages/cli/plugin-tailwind/src/config.ts +++ b/packages/cli/plugin-tailwind/src/config.ts @@ -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 { @@ -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(configFile); return { path: configFile, - content: mod.default || mod, + content, }; } diff --git a/packages/toolkit/plugin-v2/package.json b/packages/toolkit/plugin-v2/package.json index f3936fed46d3..228ecd360960 100644 --- a/packages/toolkit/plugin-v2/package.json +++ b/packages/toolkit/plugin-v2/package.json @@ -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" diff --git a/packages/toolkit/plugin-v2/src/cli/run/config/createLoadedConfig.ts b/packages/toolkit/plugin-v2/src/cli/run/config/createLoadedConfig.ts index f5662e675df6..6c0349be7f27 100644 --- a/packages/toolkit/plugin-v2/src/cli/run/config/createLoadedConfig.ts +++ b/packages/toolkit/plugin-v2/src/cli/run/config/createLoadedConfig.ts @@ -1,9 +1,7 @@ -import path from 'path'; import { fs, CONFIG_FILE_EXTENSIONS, chalk, - findExists, getCommand, getNodeEnv, isDevCommand, diff --git a/packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts b/packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts index bc6e12f4ec59..4c23a6b28c11 100644 --- a/packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts +++ b/packages/toolkit/plugin-v2/src/cli/run/config/loadConfig.ts @@ -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. @@ -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(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. diff --git a/packages/toolkit/utils/package.json b/packages/toolkit/utils/package.json index e468c9966e05..9b2fe8252e67 100644 --- a/packages/toolkit/utils/package.json +++ b/packages/toolkit/utils/package.json @@ -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:*", diff --git a/packages/toolkit/utils/src/cli/index.ts b/packages/toolkit/utils/src/cli/index.ts index 685f5704f4d9..6f094c41046b 100644 --- a/packages/toolkit/utils/src/cli/index.ts +++ b/packages/toolkit/utils/src/cli/index.ts @@ -21,3 +21,4 @@ export * from './config'; export * from './action'; export * from './version'; export * from './route'; +export * from './loadConfig'; diff --git a/packages/toolkit/utils/src/cli/loadConfig.ts b/packages/toolkit/utils/src/cli/loadConfig.ts new file mode 100644 index 000000000000..8a5a6e85e711 --- /dev/null +++ b/packages/toolkit/utils/src/cli/loadConfig.ts @@ -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(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; + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 57812dc9b2a4..21fbacc456dc 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -530,9 +530,6 @@ importers: packages/cli/plugin-tailwind: dependencies: - '@modern-js/node-bundle-require': - specifier: workspace:* - version: link:../../toolkit/node-bundle-require '@modern-js/runtime-utils': specifier: workspace:* version: link:../../toolkit/runtime-utils @@ -4021,9 +4018,6 @@ importers: '@swc/helpers': specifier: 0.5.13 version: 0.5.13 - jiti: - specifier: 1.21.7 - version: 1.21.7 devDependencies: '@modern-js/types': specifier: workspace:* @@ -4185,6 +4179,9 @@ importers: caniuse-lite: specifier: ^1.0.30001520 version: 1.0.30001566 + jiti: + specifier: 1.21.7 + version: 1.21.7 lodash: specifier: ^4.17.21 version: 4.17.21 @@ -8111,42 +8108,36 @@ packages: engines: {node: '>= 10'} cpu: [arm64] os: [linux] - libc: [glibc] '@ast-grep/napi-linux-arm64-gnu@0.33.1': resolution: {integrity: sha512-4rvHBtq/0Ziwr93Mp86GQPMMySNHCMXnSIdJqJjTikt/LhJNdxmXtEVadashwxbBGWvcIy8dL6OCBHblzY/3ZQ==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] - libc: [glibc] '@ast-grep/napi-linux-arm64-musl@0.33.1': resolution: {integrity: sha512-+vTHYYP8iRG9lZHhcpQRxAuD8CBYXJHFXgsevmnurS/R53r0YjNtrtj6W33e7RYXY5hehmey2Cz/5h6OhdLcJw==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] - libc: [musl] '@ast-grep/napi-linux-x64-gnu@0.16.0': resolution: {integrity: sha512-QjiY45TvPI50I2UxPlfPuoeDeEYJxGDyLegqYfrLsxtdv+wX2Jdgjew6myiMXCVG9oJWgtmp/z28zpl7H8YLPA==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - libc: [glibc] '@ast-grep/napi-linux-x64-gnu@0.33.1': resolution: {integrity: sha512-Qm42//ZHIi2XvyvHboRPaNt32v143feG2aCqxZ2qhKJCI33abtH8pW8MF90Ix85d927xYtTwZX/ovOmJ4bghFQ==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - libc: [glibc] '@ast-grep/napi-linux-x64-musl@0.33.1': resolution: {integrity: sha512-+ye9d8nwgi+f9yhA0NEv5lDcpfIF7xhCcF9FerIKpksX57oI68QWNz1bOWHOuebaf9Wc0hgEtfai7lzcDWcsnA==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - libc: [musl] '@ast-grep/napi-win32-arm64-msvc@0.16.0': resolution: {integrity: sha512-4OCpEf44h63RVFiNA2InIoRNlTB2XJUq1nUiFacTagSP5L3HwnZQ4URC1+fdmZh1ESedm7KxzvhgByqGeUyzgA==} @@ -9072,28 +9063,24 @@ packages: engines: {node: '>=14.21.3'} cpu: [arm64] os: [linux] - libc: [musl] '@biomejs/cli-linux-arm64@1.9.4': resolution: {integrity: sha512-fJIW0+LYujdjUgJJuwesP4EjIBl/N/TcOX3IvIHJQNsAqvV2CHIogsmA94BPG6jZATS4Hi+xv4SkBBQSt1N4/g==} engines: {node: '>=14.21.3'} cpu: [arm64] os: [linux] - libc: [glibc] '@biomejs/cli-linux-x64-musl@1.9.4': resolution: {integrity: sha512-gEhi/jSBhZ2m6wjV530Yy8+fNqG8PAinM3oV7CyO+6c3CEh16Eizm21uHVsyVBEB6RIM8JHIl6AGYCv6Q6Q9Tg==} engines: {node: '>=14.21.3'} cpu: [x64] os: [linux] - libc: [musl] '@biomejs/cli-linux-x64@1.9.4': resolution: {integrity: sha512-lRCJv/Vi3Vlwmbd6K+oQ0KhLHMAysN8lXoCI7XeHlxaajk06u7G+UsFSO01NAs5iYuWKmVZjmiOzJ0OJmGsMwg==} engines: {node: '>=14.21.3'} cpu: [x64] os: [linux] - libc: [glibc] '@biomejs/cli-win32-arm64@1.9.4': resolution: {integrity: sha512-tlbhLk+WXZmgwoIKwHIHEBZUwxml7bRJgk0X2sPyNR3S93cdRq6XulAZRQJ17FYGGzWne0fgrXBKpl7l4M87Hg==} @@ -10371,28 +10358,24 @@ packages: engines: {node: '>= 10'} cpu: [arm64] os: [linux] - libc: [glibc] '@nx/nx-linux-arm64-musl@17.3.2': resolution: {integrity: sha512-yko3Xsezkn4tjeudZYLjxFl07X/YB84K+DLK7EFyh9elRWV/8VjFcQmBAKUS2r9LfaEMNXq8/vhWMOWYyWBrIA==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] - libc: [musl] '@nx/nx-linux-x64-gnu@17.3.2': resolution: {integrity: sha512-RiPvvQMmlZmDu9HdT6n6sV0+fEkyAqR5VocrD5ZAzEzFIlh4dyVLripFR3+MD+QhIhXyPt/hpri1kq9sgs4wnw==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - libc: [glibc] '@nx/nx-linux-x64-musl@17.3.2': resolution: {integrity: sha512-PWfVGmFsFJi+N1Nljg/jTKLHdufpGuHlxyfHqhDso/o4Qc0exZKSeZ1C63WkD7eTcT5kInifTQ/PffLiIDE3MA==} engines: {node: '>= 10'} cpu: [x64] os: [linux] - libc: [musl] '@nx/nx-win32-arm64-msvc@17.3.2': resolution: {integrity: sha512-O+4FFPbQz1mqaIj+SVE02ppe7T9ELj7Z5soQct5TbRRhwjGaw5n5xaPPBW7jUuQe2L5htid1E82LJyq3JpVc8A==} @@ -10438,42 +10421,36 @@ packages: engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] - libc: [glibc] '@parcel/watcher-linux-arm-musl@2.5.0': resolution: {integrity: sha512-6uHywSIzz8+vi2lAzFeltnYbdHsDm3iIB57d4g5oaB9vKwjb6N6dRIgZMujw4nm5r6v9/BQH0noq6DzHrqr2pA==} engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] - libc: [musl] '@parcel/watcher-linux-arm64-glibc@2.5.0': resolution: {integrity: sha512-BfNjXwZKxBy4WibDb/LDCriWSKLz+jJRL3cM/DllnHH5QUyoiUNEp3GmL80ZqxeumoADfCCP19+qiYiC8gUBjA==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] - libc: [glibc] '@parcel/watcher-linux-arm64-musl@2.5.0': resolution: {integrity: sha512-S1qARKOphxfiBEkwLUbHjCY9BWPdWnW9j7f7Hb2jPplu8UZ3nes7zpPOW9bkLbHRvWM0WDTsjdOTUgW0xLBN1Q==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] - libc: [musl] '@parcel/watcher-linux-x64-glibc@2.5.0': resolution: {integrity: sha512-d9AOkusyXARkFD66S6zlGXyzx5RvY+chTP9Jp0ypSTC9d4lzyRs9ovGf/80VCxjKddcUvnsGwCHWuF2EoPgWjw==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] - libc: [glibc] '@parcel/watcher-linux-x64-musl@2.5.0': resolution: {integrity: sha512-iqOC+GoTDoFyk/VYSFHwjHhYrk8bljW6zOhPuhi5t9ulqiYq1togGJB5e3PwYVFFfeVgc6pbz3JdQyDoBszVaA==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] - libc: [musl] '@parcel/watcher-win32-arm64@2.5.0': resolution: {integrity: sha512-twtft1d+JRNkM5YbmexfcH/N4znDtjgysFaV9zvZmmJezQsKpkfLYJ+JFV3uygugK6AtIM2oADPkB2AdhBrNig==} @@ -11847,25 +11824,21 @@ packages: resolution: {integrity: sha512-lZlO/rAJSeozi+qtVLkGSXfe+riPawCwM4FsrflELfNlvvEXpANwtrdJ+LsaNVXcgvhh50ZX2KicTdmx9G2b6Q==} cpu: [arm64] os: [linux] - libc: [glibc] '@rspack/binding-linux-arm64-musl@1.1.8': resolution: {integrity: sha512-bX7exULSZwy8xtDh6Z65b6sRC4uSxGuyvSLCEKyhmG6AnJkg0gQMxk3hoO0hWnyGEZgdJEn+jEhk0fjl+6ZRAQ==} cpu: [arm64] os: [linux] - libc: [musl] '@rspack/binding-linux-x64-gnu@1.1.8': resolution: {integrity: sha512-2Prw2USgTJ3aLdLExfik8pAwAHbX4MZrACBGEmR7Vbb56kLjC+++fXkciRc50pUDK4JFr1VQ7eNZrJuDR6GG6Q==} cpu: [x64] os: [linux] - libc: [glibc] '@rspack/binding-linux-x64-musl@1.1.8': resolution: {integrity: sha512-bnVGB/mQBKEdzOU/CPmcOE3qEXxGOGGW7/i6iLl2MamVOykJq8fYjL9j86yi6L0r009ja16OgWckykQGc4UqGw==} cpu: [x64] os: [linux] - libc: [musl] '@rspack/binding-win32-arm64-msvc@1.1.8': resolution: {integrity: sha512-u+na3gxhzeksm4xZyAzn1+XWo5a5j7hgWA/KcFPDQ8qQNkRknx4jnQMxVtcZ9pLskAYV4AcOV/AIximx7zvv8A==} @@ -12289,28 +12262,24 @@ packages: engines: {node: '>=10'} cpu: [arm64] os: [linux] - libc: [glibc] '@swc/core-linux-arm64-musl@1.10.7': resolution: {integrity: sha512-gp5Un3EbeSThBIh6oac5ZArV/CsSmTKj5jNuuUAuEsML3VF9vqPO+25VuxCvsRf/z3py+xOWRaN2HY/rjMeZog==} engines: {node: '>=10'} cpu: [arm64] os: [linux] - libc: [musl] '@swc/core-linux-x64-gnu@1.10.7': resolution: {integrity: sha512-k/OxLLMl/edYqbZyUNg6/bqEHTXJT15l9WGqsl/2QaIGwWGvles8YjruQYQ9d4h/thSXLT9gd8bExU2D0N+bUA==} engines: {node: '>=10'} cpu: [x64] os: [linux] - libc: [glibc] '@swc/core-linux-x64-musl@1.10.7': resolution: {integrity: sha512-XeDoURdWt/ybYmXLCEE8aSiTOzEn0o3Dx5l9hgt0IZEmTts7HgHHVeRgzGXbR4yDo0MfRuX5nE1dYpTmCz0uyA==} engines: {node: '>=10'} cpu: [x64] os: [linux] - libc: [musl] '@swc/core-win32-arm64-msvc@1.10.7': resolution: {integrity: sha512-nYAbi/uLS+CU0wFtBx8TquJw2uIMKBnl04LBmiVoFrsIhqSl+0MklaA9FVMGA35NcxSJfcm92Prl2W2LfSnTqQ==}