Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add support for config in TypeScript #150

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
9 changes: 4 additions & 5 deletions packages/webpack-config/src/presets/tailwindcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,14 @@ export default function tailwindcss(options = {}) {
path: require.resolve('tailwindcss', { paths: [configPath] }),
configViewerPath: '/_tailwind',
},
options
options,
);

if (isDev) {
const { default: createServer } = await import('tailwind-config-viewer/server/index.js');
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();

Expand All @@ -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),
)}`,
);
});
}
Expand Down Expand Up @@ -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) {
Expand Down
35 changes: 22 additions & 13 deletions packages/webpack-config/src/utils/get-config.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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,
Expand Down