Skip to content

Commit

Permalink
Merge pull request #3370 from opral/parjs-340-change-adapter-to-addit…
Browse files Browse the repository at this point in the history
…ionalfiles-in-compiler-api

change adapter to additionalFiles in compiler API
  • Loading branch information
samuelstroschein authored Jan 24, 2025
2 parents a78c156 + f0dc9a2 commit 262d640
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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": "<script>console.log('hello')</script>",
"adapter.js": "console.log('hello')",
},
test("emits additional files", async () => {
const additionalFiles = {
"adapter/component.svelte": "<script>console.log('hello')</script>",
"adapter.js": "console.log('hello')",
};

const project = await loadProjectInMemory({
Expand All @@ -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");
Expand Down
17 changes: 6 additions & 11 deletions inlang/packages/paraglide/paraglide-js/src/compiler/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import {
saveLocalAccount,
} from "../services/account/index.js";

export type Adapter = {
/**
* Files to emit to the output directory.
*/
files: Record<string, string>;
};

export type CompilerArgs = {
/**
* The path to the project to compile.
Expand All @@ -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<string, string>;
/**
* Additional compiler options.
*/
Expand Down Expand Up @@ -90,7 +85,7 @@ export async function compile(args: CompilerArgs): Promise<void> {
});

for (const [filename, content] of Object.entries(
args.adapter?.files ?? {}
args.additionalFiles ?? {}
)) {
output[filename] = content;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./.svelte-kit/tsconfig.json",
"compilerOptions": {
"allowJs": true,
"target": "ES2022",
"checkJs": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
})
]
});
28 changes: 20 additions & 8 deletions inlang/packages/paraglide/paraglide-sveltekit/src/index.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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,
},
});

0 comments on commit 262d640

Please sign in to comment.