Skip to content

Commit

Permalink
refactor: top level nextjs adapter function
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelstroschein committed Jan 24, 2025
1 parent 369ba45 commit f0dc9a2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 25 deletions.
24 changes: 7 additions & 17 deletions inlang/packages/paraglide/paraglide-next/example/next.config.mjs
Original file line number Diff line number Diff line change
@@ -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;
});
50 changes: 42 additions & 8 deletions inlang/packages/paraglide/paraglide-next/src/index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
}

0 comments on commit f0dc9a2

Please sign in to comment.