diff --git a/inlang/packages/paraglide/paraglide-next/example/next.config.mjs b/inlang/packages/paraglide/paraglide-next/example/next.config.mjs index 2565f3d944..aa23b18140 100644 --- a/inlang/packages/paraglide/paraglide-next/example/next.config.mjs +++ b/inlang/packages/paraglide/paraglide-next/example/next.config.mjs @@ -1,23 +1,13 @@ -import { paraglideWebpackPlugin } from "@inlang/paraglide-js"; -import { ParaglideNext } from "@inlang/paraglide-next"; +import { withParaglideNext } from "@inlang/paraglide-next"; -/** @type {import("next").NextConfig} */ -const nextConfig = { +export default withParaglideNext({ + paraglide: { + outdir: "./src/paraglide", + project: "./project.inlang", + }, eslint: { // Warning: This allows production builds to successfully complete even if // your project has ESLint errors. ignoreDuringBuilds: true, }, - webpack: (config) => { - config.plugins.push( - paraglideWebpackPlugin({ - project: "./project.inlang", - outdir: "./src/paraglide", - adapter: ParaglideNext(), - }) - ); - return config; - }, -}; - -export default nextConfig; +}); diff --git a/inlang/packages/paraglide/paraglide-next/src/index.ts b/inlang/packages/paraglide/paraglide-next/src/index.ts index 667a7cd2a4..d25e469b21 100644 --- a/inlang/packages/paraglide/paraglide-next/src/index.ts +++ b/inlang/packages/paraglide/paraglide-next/src/index.ts @@ -1,5 +1,9 @@ -import type { Adapter } from "@inlang/paraglide-js"; import fs from "node:fs"; +import type { NextConfig } from "next"; +import { + paraglideWebpackPlugin, + type CompilerArgs, +} from "@inlang/paraglide-js"; /** * Reads a file from the file system and returns it as a string. @@ -8,11 +12,41 @@ const file = (path: string) => ({ [path]: fs.readFileSync(new URL(path, import.meta.url), "utf-8"), }); -export const ParaglideNext: () => Adapter = () => { - const files = { - ...file("adapter.js"), - ...file("adapter.provider.jsx"), - ...file("adapter.provider.client.jsx"), +/** + * Extends a Next.js configuration with Paraglide. + * + * @example + * // next.config.mjs + * import { withParaglideNext } from "@inlang/paraglide-next"; + * export default withParaglideNext({ + * paraglide: { + * project: "./project.inlang", + * outdir: "./src/lib/paraglide", + * }, + * // other Next.js configuration + * }); + */ +export function withParaglideNext( + config: NextConfig & { + paraglide: CompilerArgs; + } +): NextConfig { + const extendedConfig: NextConfig = { + ...config, + webpack: (webpackConfig) => { + webpackConfig.plugins.push( + paraglideWebpackPlugin({ + ...config.paraglide, + additionalFiles: { + ...file("adapter.js"), + ...file("adapter.provider.jsx"), + ...file("adapter.provider.client.jsx"), + }, + }) + ); + return webpackConfig; + }, }; - return { files }; -}; + delete extendedConfig.paraglide; + return extendedConfig; +}