From 2bc0d18dfa196c1b036b6cbdf161ba76ee99a1c5 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Wed, 17 Apr 2024 09:25:13 +0200 Subject: [PATCH 1/3] Add support for meta.config.ts files --- .../webpack-config/src/utils/get-config.js | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/packages/webpack-config/src/utils/get-config.js b/packages/webpack-config/src/utils/get-config.js index ab745f6..70c4811 100644 --- a/packages/webpack-config/src/utils/get-config.js +++ b/packages/webpack-config/src/utils/get-config.js @@ -1,27 +1,40 @@ -import path from 'node:path'; +import { resolve, dirname, isAbsolute } from 'node:path'; +import { createHash } from 'node:crypto'; +import { readFileSync, writeFileSync, mkdirSync } from 'node:fs'; import { findUp } from 'find-up'; +import esbuild from 'esbuild'; import extendBrowsersync from './extend-browsersync-config.js'; import extendWebpack from './extend-webpack-config.js'; /** * Get config from meta.config.js file. - * * @param {{ analyze: boolean, mode: 'development'|'production' }} [options] CLI Options. * @returns {import('../index').MetaConfig} */ export default async function getConfig({ analyze = false, mode = 'production' } = {}) { - const configPath = await findUp(['meta.config.js', 'meta.config.mjs']); + let configPath = await findUp(['meta.config.js', 'meta.config.mjs', 'meta.config.ts']); if (!configPath) { throw new Error( [ 'Could not find a config file.', 'Is there a meta.config.js file up in the folder tree?', - ].join('\n') + ].join('\n'), ); } - const { default: config } = await import(configPath); + if (configPath.endsWith('.ts')) { + const configContent = readFileSync(configPath); + const hash = createHash('md5').update(configContent).digest('hex'); + const result = await esbuild.transform(configContent, { + loader: 'ts', + }); + configPath = resolve(dirname(process.env._), `../.cache/meta.config-${hash}.mjs`); + mkdirSync(dirname(configPath), { recursive: true }); + writeFileSync(configPath, result.code); + } + + const config = await import(configPath).then((mod) => mod.default); const isDev = mode !== 'production'; if (analyze) { @@ -31,39 +44,35 @@ export default async function getConfig({ analyze = false, mode = 'production' } config.PATH = configPath; if (!config.context) { - config.context = path.dirname(configPath); + config.context = dirname(configPath); } - if (!path.isAbsolute(config.context)) { - config.context = path.resolve(process.cwd(), config.context); + if (!isAbsolute(config.context)) { + config.context = resolve(process.cwd(), config.context); } if (!config.dist) { - config.dist = path.resolve(config.context, './dist'); + config.dist = resolve(config.context, './dist'); } if (Array.isArray(config.presets) && config.presets.length) { console.log('Applying presets...'); - // eslint-disable-next-line no-restricted-syntax for (let preset of config.presets) { if (typeof preset === 'function') { preset = preset(isDev); } if (!preset) { - // eslint-disable-next-line no-continue continue; } if (!preset.name && typeof preset.handler !== 'function') { console.log('Preset misconfigured.', preset); - // eslint-disable-next-line no-continue continue; } const start = performance.now(); - // eslint-disable-next-line no-await-in-loop await preset.handler(config, { extendBrowsersync, extendWebpack, From efcd629761cb3608a469fe345f3004d2bd450e8c Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Wed, 17 Apr 2024 09:25:55 +0200 Subject: [PATCH 2/3] Update demo --- packages/demo/{meta.config.js => meta.config.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename packages/demo/{meta.config.js => meta.config.ts} (100%) diff --git a/packages/demo/meta.config.js b/packages/demo/meta.config.ts similarity index 100% rename from packages/demo/meta.config.js rename to packages/demo/meta.config.ts From 7d16622200d906a0d8cbdea45765139614251452 Mon Sep 17 00:00:00 2001 From: Titouan Mathis Date: Mon, 29 Apr 2024 14:13:50 +0200 Subject: [PATCH 3/3] Lint files --- packages/webpack-config/src/presets/tailwindcss.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/webpack-config/src/presets/tailwindcss.js b/packages/webpack-config/src/presets/tailwindcss.js index 23e2bd7..bbc3831 100644 --- a/packages/webpack-config/src/presets/tailwindcss.js +++ b/packages/webpack-config/src/presets/tailwindcss.js @@ -22,7 +22,7 @@ export default function tailwindcss(options = {}) { path: require.resolve('tailwindcss', { paths: [configPath] }), configViewerPath: '/_tailwind', }, - options + options, ); if (isDev) { @@ -30,7 +30,6 @@ export default function tailwindcss(options = {}) { await extendBrowsersync(config, async (bsConfig) => { const tailwindConfigViewerServer = createServer({ tailwindConfigProvider: () => - // eslint-disable-next-line import/no-dynamic-require require(findUpSync(['tailwind.config.js', 'tailwind.config.cjs'])), }).asMiddleware(); @@ -44,8 +43,8 @@ export default function tailwindcss(options = {}) { bsConfig.infos.push( (url) => `Tailwind Viewer runnning at ${chalk.blue( - withTrailingSlash(url + opts.configViewerPath) - )}` + withTrailingSlash(url + opts.configViewerPath), + )}`, ); }); } @@ -76,7 +75,7 @@ export default function tailwindcss(options = {}) { } const postcssIndex = rule.use.findIndex( - (use) => use === 'postcss-loader' || use.loader === 'postcss-loader' + (use) => use === 'postcss-loader' || use.loader === 'postcss-loader', ); if (postcssIndex > -1) {