mdxlayer
Version:
Transform your MDX content into typed, JSON-powered data with flexible schema validation.
54 lines (51 loc) • 1.58 kB
JavaScript
import fs from 'node:fs';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { build } from 'esbuild';
import { cache } from '../cache/index.js';
import { cliOutDir } from '../utils/args.js';
import { configPath } from './path.js';
const externalizePackagesPlugin = (configPath2) => ({
name: "externalize-packages",
setup: (build2) => {
const filter = /^[^./]/;
build2.onResolve({ filter }, (args) => {
if (args.path.includes(configPath2)) {
return { external: false, path: args.path };
}
return { external: true, path: args.path };
});
}
});
const getUserConfig = async () => {
const newFilename = "compiled-config.js";
const outfile = path.resolve(
process.cwd(),
`${cliOutDir}/cache`,
newFilename
);
const compiledConfig = async () => {
await build({
bundle: true,
entryPoints: [configPath],
format: "esm",
logLevel: "silent",
metafile: true,
outfile,
platform: "node",
plugins: [externalizePackagesPlugin(configPath)],
sourcemap: true
});
};
let isChanged = false;
const exists = fs.existsSync(outfile);
if (exists) {
const content = fs.readFileSync(outfile, "utf8");
isChanged = cache.changed(content, newFilename);
}
if (isChanged || !exists) await compiledConfig();
const configModule = await import(`${pathToFileURL(outfile).href}?t=${Date.now()}`);
return { ...configModule.default, changed: isChanged };
};
const defineConfig = (config) => config;
export { defineConfig, getUserConfig };