next-mdxlayer
Version:
Next.js plugin to integrate mdxlayer CLI and generated content
57 lines (54 loc) • 1.5 kB
JavaScript
import { spawn } from 'node:child_process';
import path from 'node:path';
let buildExecuted = false;
const isDev = process.argv.includes("dev");
const isStart = process.argv.includes("start");
const isBuild = process.env.NODE_ENV === "production";
const withMdxlayer = (nextConfig = {}) => {
if (isBuild && !isStart && !buildExecuted) {
buildExecuted = true;
void (async () => {
try {
const { builder } = await import('mdxlayer/builder');
await builder();
} catch (error) {
console.error("❌ MDX production build failed:", error);
process.exit(1);
}
})();
}
if (isDev) {
if (!global.__mdxlayerDevProcess) {
global.__mdxlayerDevProcess = spawn(
"node",
["-e", "import('mdxlayer/watcher').then(m => m.watcher())"],
{
detached: true,
stdio: "inherit"
}
);
["exit", "SIGINT", "SIGTERM"].forEach((event) => {
process.on(event, () => {
global.__mdxlayerDevProcess?.kill();
global.__mdxlayerDevProcess = void 0;
});
});
}
}
return {
...nextConfig,
turbopack: {
resolveAlias: {
"mdxlayer/generated": path.join(process.cwd(), ".mdxlayer/generated")
}
},
webpack(config) {
config.resolve.alias = {
...config.resolve.alias,
"mdxlayer/generated": path.join(process.cwd(), ".mdxlayer/generated")
};
return config;
}
};
};
export { withMdxlayer };