From 1d1a7a0c851ef306d129d7ca0a3333c986f081a2 Mon Sep 17 00:00:00 2001 From: Samuel Stroschein <35429197+samuelstroschein@users.noreply.github.com> Date: Thu, 23 Jan 2025 18:23:29 -0500 Subject: [PATCH 1/4] refactor: remove adapter prop for additional files part of https://github.com/opral/inlang-paraglide-js/issues/338 --- .../paraglide-js/src/compiler/compile.test.ts | 14 ++++++-------- .../paraglide-js/src/compiler/compile.ts | 17 ++++++----------- .../paraglide-js/src/compiler/index.ts | 2 +- 3 files changed, 13 insertions(+), 20 deletions(-) diff --git a/inlang/packages/paraglide/paraglide-js/src/compiler/compile.test.ts b/inlang/packages/paraglide/paraglide-js/src/compiler/compile.test.ts index a30c1418e0..6ec05b039c 100644 --- a/inlang/packages/paraglide/paraglide-js/src/compiler/compile.test.ts +++ b/inlang/packages/paraglide/paraglide-js/src/compiler/compile.test.ts @@ -5,7 +5,7 @@ import { } from "@inlang/sdk"; import { memfs } from "memfs"; import { test, expect, vi } from "vitest"; -import { compile, type Adapter } from "./compile.js"; +import { compile } from "./compile.js"; import { getAccountFilePath } from "../services/account/index.js"; test("loads a project and compiles it", async () => { @@ -166,12 +166,10 @@ test("multiple compile calls do not interfere with each other", async () => { expect(outputDir).not.toContain("subdir"); }); -test("emits files of an adapter", async () => { - const adapter: Adapter = { - files: { - "adapter/component.svelte": "", - "adapter.js": "console.log('hello')", - }, +test("emits additional files", async () => { + const additionalFiles = { + "adapter/component.svelte": "", + "adapter.js": "console.log('hello')", }; const project = await loadProjectInMemory({ @@ -196,7 +194,7 @@ test("emits files of an adapter", async () => { project: "/project.inlang", outdir: "/output", fs: fs, - adapter, + additionalFiles, }); const outputDir = await fs.promises.readdir("/output"); diff --git a/inlang/packages/paraglide/paraglide-js/src/compiler/compile.ts b/inlang/packages/paraglide/paraglide-js/src/compiler/compile.ts index 55208c8759..011f4ad39d 100644 --- a/inlang/packages/paraglide/paraglide-js/src/compiler/compile.ts +++ b/inlang/packages/paraglide/paraglide-js/src/compiler/compile.ts @@ -8,13 +8,6 @@ import { saveLocalAccount, } from "../services/account/index.js"; -export type Adapter = { - /** - * Files to emit to the output directory. - */ - files: Record; -}; - export type CompilerArgs = { /** * The path to the project to compile. @@ -31,12 +24,14 @@ export type CompilerArgs = { */ outdir: string; /** - * The adapter to use. + * Additional files that should be emmited in the outdir. * * @example - * adapter: ParaglideSveltekit + * additionalFiles: { + * "custom-file.js": "console.log('Hello, world!')" + * } */ - adapter?: Adapter; + additionalFiles?: Record; /** * Additional compiler options. */ @@ -90,7 +85,7 @@ export async function compile(args: CompilerArgs): Promise { }); for (const [filename, content] of Object.entries( - args.adapter?.files ?? {} + args.additionalFiles ?? {} )) { output[filename] = content; } diff --git a/inlang/packages/paraglide/paraglide-js/src/compiler/index.ts b/inlang/packages/paraglide/paraglide-js/src/compiler/index.ts index a107c2b295..9081c51fbe 100644 --- a/inlang/packages/paraglide/paraglide-js/src/compiler/index.ts +++ b/inlang/packages/paraglide/paraglide-js/src/compiler/index.ts @@ -1,4 +1,4 @@ -export type { CompilerArgs, Adapter } from "./compile.js"; +export type { CompilerArgs } from "./compile.js"; export type { CompilerOptions } from "./compile-project.js"; export type { MessageBundleFunction, MessageFunction } from "./types.js"; export type { Runtime } from "./runtime/type.js"; From 6e03f58b3fb0713fc155c2bf5fdb7e04593a4206 Mon Sep 17 00:00:00 2001 From: Samuel Stroschein <35429197+samuelstroschein@users.noreply.github.com> Date: Thu, 23 Jan 2025 18:45:14 -0500 Subject: [PATCH 2/4] refactor: top level paraglide sveltekit function part of https://github.com/opral/inlang-paraglide-js/issues/338 --- .../paraglide-sveltekit/example/tsconfig.json | 1 + .../example/vite.config.ts | 8 ++---- .../paraglide-sveltekit/src/index.ts | 28 +++++++++++++------ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/inlang/packages/paraglide/paraglide-sveltekit/example/tsconfig.json b/inlang/packages/paraglide/paraglide-sveltekit/example/tsconfig.json index 98e2f06a33..cd4c7fb834 100644 --- a/inlang/packages/paraglide/paraglide-sveltekit/example/tsconfig.json +++ b/inlang/packages/paraglide/paraglide-sveltekit/example/tsconfig.json @@ -2,6 +2,7 @@ "extends": "./.svelte-kit/tsconfig.json", "compilerOptions": { "allowJs": true, + "target": "ES2022", "checkJs": true, "esModuleInterop": true, "forceConsistentCasingInFileNames": true, diff --git a/inlang/packages/paraglide/paraglide-sveltekit/example/vite.config.ts b/inlang/packages/paraglide/paraglide-sveltekit/example/vite.config.ts index e6e4b39d44..2ba3a9e340 100644 --- a/inlang/packages/paraglide/paraglide-sveltekit/example/vite.config.ts +++ b/inlang/packages/paraglide/paraglide-sveltekit/example/vite.config.ts @@ -1,15 +1,13 @@ import { sveltekit } from '@sveltejs/kit/vite'; import { defineConfig } from 'vite'; -import { paraglideVitePlugin } from '@inlang/paraglide-js'; -import { ParaglideSveltekitAdapter } from '@inlang/paraglide-sveltekit'; +import { paraglideSvelteKit } from '@inlang/paraglide-sveltekit'; export default defineConfig({ plugins: [ sveltekit(), - paraglideVitePlugin({ + paraglideSvelteKit({ project: './project.inlang', - outdir: './src/lib/paraglide', - adapter: ParaglideSveltekitAdapter() + outdir: './src/lib/paraglide' }) ] }); diff --git a/inlang/packages/paraglide/paraglide-sveltekit/src/index.ts b/inlang/packages/paraglide/paraglide-sveltekit/src/index.ts index a5474dc6b2..a41ee7ca6f 100644 --- a/inlang/packages/paraglide/paraglide-sveltekit/src/index.ts +++ b/inlang/packages/paraglide/paraglide-sveltekit/src/index.ts @@ -1,5 +1,5 @@ -import type { Adapter } from "@inlang/paraglide-js"; import fs from "node:fs"; +import { paraglideVitePlugin, type CompilerArgs } from "@inlang/paraglide-js"; /** * Reads a file from the file system and returns it as a string. @@ -8,10 +8,22 @@ const file = (path: string) => ({ [path]: fs.readFileSync(new URL(path, import.meta.url), "utf-8"), }); -export const ParaglideSveltekitAdapter: () => Adapter = () => { - const files = { - ...file("adapter.provider.svelte"), - ...file("adapter.js"), - }; - return { files }; -}; +/** + * A Vite plugin that compiles the inlang project and emits output with + * additional files for SvelteKit. + * + * @example + * paraglideSvelteKit({ + * project: './project.inlang', + * outdir: './src/lib/paraglide', + * }) + */ +export const paraglideSvelteKit = (args: CompilerArgs) => + paraglideVitePlugin({ + ...args, + additionalFiles: { + ...file("adapter.js"), + ...file("adapter.provider.svelte"), + ...args.additionalFiles, + }, + }); From 369ba459bc57108c755148c5c9b1b946d55c9ef6 Mon Sep 17 00:00:00 2001 From: Samuel Stroschein <35429197+samuelstroschein@users.noreply.github.com> Date: Thu, 23 Jan 2025 18:46:14 -0500 Subject: [PATCH 3/4] align sveltekit name --- .../paraglide/paraglide-sveltekit/example/vite.config.ts | 4 ++-- inlang/packages/paraglide/paraglide-sveltekit/src/index.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/inlang/packages/paraglide/paraglide-sveltekit/example/vite.config.ts b/inlang/packages/paraglide/paraglide-sveltekit/example/vite.config.ts index 2ba3a9e340..7463680657 100644 --- a/inlang/packages/paraglide/paraglide-sveltekit/example/vite.config.ts +++ b/inlang/packages/paraglide/paraglide-sveltekit/example/vite.config.ts @@ -1,11 +1,11 @@ import { sveltekit } from '@sveltejs/kit/vite'; import { defineConfig } from 'vite'; -import { paraglideSvelteKit } from '@inlang/paraglide-sveltekit'; +import { paraglideSveltekit } from '@inlang/paraglide-sveltekit'; export default defineConfig({ plugins: [ sveltekit(), - paraglideSvelteKit({ + paraglideSveltekit({ project: './project.inlang', outdir: './src/lib/paraglide' }) diff --git a/inlang/packages/paraglide/paraglide-sveltekit/src/index.ts b/inlang/packages/paraglide/paraglide-sveltekit/src/index.ts index a41ee7ca6f..7cde500c66 100644 --- a/inlang/packages/paraglide/paraglide-sveltekit/src/index.ts +++ b/inlang/packages/paraglide/paraglide-sveltekit/src/index.ts @@ -13,12 +13,12 @@ const file = (path: string) => ({ * additional files for SvelteKit. * * @example - * paraglideSvelteKit({ + * paraglideSveltekit({ * project: './project.inlang', * outdir: './src/lib/paraglide', * }) */ -export const paraglideSvelteKit = (args: CompilerArgs) => +export const paraglideSveltekit = (args: CompilerArgs) => paraglideVitePlugin({ ...args, additionalFiles: { From f0dc9a258c1a8c776a7acbdf979dfcbbeaa40c85 Mon Sep 17 00:00:00 2001 From: Samuel Stroschein <35429197+samuelstroschein@users.noreply.github.com> Date: Thu, 23 Jan 2025 19:26:12 -0500 Subject: [PATCH 4/4] refactor: top level nextjs adapter function part of https://github.com/opral/inlang-paraglide-js/issues/338 --- .../paraglide-next/example/next.config.mjs | 24 +++------ .../paraglide/paraglide-next/src/index.ts | 50 ++++++++++++++++--- 2 files changed, 49 insertions(+), 25 deletions(-) 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; +}