Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: aik latest #20

Merged
merged 11 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tall-avocados-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrolicious/i18n": minor
---

Reworks internals to use Astro Integration Kit 0.13, this is non breaking
25 changes: 25 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build

on: [pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PNPM
run: corepack enable && pnpm -v

- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: 18.19.0
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Build
run: pnpm --filter @astrolicious/i18n build
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
dist
22 changes: 11 additions & 11 deletions demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/check": "^0.5.6",
"@astrojs/netlify": "^5.1.3",
"@astrojs/react": "^3.0.10",
"@astrojs/check": "^0.5.10",
"@astrojs/netlify": "^5.2.0",
"@astrojs/react": "^3.3.1",
"@astrojs/tailwind": "^5.1.0",
"@astrolicious/i18n": "workspace:*",
"@tailwindcss/forms": "^0.5.7",
"@types/react": "^18.2.64",
"@types/react-dom": "^18.2.21",
"astro": "^4.4.13",
"i18next": "^23.10.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwindcss": "^3.4.1",
"typescript": "^5.4.2"
"@types/react": "^18.3.1",
"@types/react-dom": "^18.3.0",
"astro": "^4.7.0",
"i18next": "^23.11.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"tailwindcss": "^3.4.3",
"typescript": "^5.4.5"
}
}
16 changes: 8 additions & 8 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/check": "^0.5.6",
"@astrojs/starlight": "^0.21.1",
"@astrojs/starlight-tailwind": "^2.0.1",
"@astrojs/check": "^0.5.10",
"@astrojs/starlight": "^0.21.5",
"@astrojs/starlight-tailwind": "^2.0.2",
"@astrojs/tailwind": "^5.1.0",
"@expressive-code/plugin-line-numbers": "^0.33.4",
"astro": "^4.3.5",
"sharp": "^0.32.5",
"@expressive-code/plugin-line-numbers": "^0.33.5",
"astro": "^4.7.0",
"sharp": "^0.33.3",
"tailwind": "^4.0.0",
"tailwindcss": "^3.4.1",
"typescript": "^5.3.3"
"tailwindcss": "^3.4.3",
"typescript": "^5.4.5"
}
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{
"name": "root",
"private": true,
"packageManager": "pnpm@9.0.1",
"packageManager": "pnpm@9.0.4",
"engines": {
"node": ">=18.19.0"
},
"scripts": {
"package:dev": "pnpm --filter @astrolicious/i18n dev",
"playground:dev": "pnpm --filter playground dev",
"docs:dev": "pnpm --filter docs dev",
"demo:dev": "pnpm --filter demo dev",
"dev": "pnpm --stream -r -parallel dev",
"changeset": "changeset",
"release": "node scripts/release.mjs",
"lint": "biome check .",
"lint:fix": "biome check --apply ."
},
"devDependencies": {
"@biomejs/biome": "1.6.4",
"@biomejs/biome": "1.7.1",
"@changesets/cli": "^2.27.1"
}
}
4 changes: 2 additions & 2 deletions package/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ Install dependencies using pnpm:
pnpm i --frozen-lockfile
```

Start the playground:
Start the playground and package watcher:

```bash
pnpm playground:dev
pnpm dev
```

You can now edit files in `package`. Please note that making changes to those files may require restarting the playground dev server.
Expand Down
2 changes: 1 addition & 1 deletion package/src/middleware.ts → package/assets/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineMiddleware } from "astro:middleware";
import { als } from "virtual:astro-i18n/als";
import { i18nextConfig, options, routes } from "virtual:astro-i18n/internal";
import { defineMiddleware } from "astro/middleware";

const extractLocaleFromUrl = (pathname: string) => {
for (const locale of options.locales) {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @returns {import("../types.js").I18nConfig}
* @returns {import("../../src/types.ts").I18nConfig}
*/
const _getConfig = () => "@@_CONFIG_@@";
/**
Expand Down
8 changes: 4 additions & 4 deletions package/src/env.d.ts → package/env.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/// <reference types="astro/client" />

declare module "virtual:astro-i18n/internal" {
export const options: import("./options.js").Options;
export const routes: Array<import("./types.js").Route>;
export const i18nextConfig: import("./types.js").I18nextConfig;
export const options: import("./src/options.js").Options;
export const routes: Array<import("./src/types.js").Route>;
export const i18nextConfig: import("./src/types.js").I18nextConfig;
export const clientId: string;
}

declare module "virtual:astro-i18n/als" {
export const als: import("node:async_hooks").AsyncLocalStorage<
import("./types.js").I18nConfig
import("./src/types.js").I18nConfig
>;
}

Expand Down
42 changes: 28 additions & 14 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,42 @@
},
"sideEffects": false,
"exports": {
".": "./src/index.ts",
"./internal": "./src/internal.ts",
"./content-collections": "./src/content-collections.ts",
"./components/I18nClient.astro": "./src/components/I18nClient.astro",
"./components/I18nHead.astro": "./src/components/I18nHead.astro"
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./internal": {
"types": "./dist/internal.d.ts",
"default": "./dist/internal.js"
},
"./content-collections": {
"types": "./dist/content-collections.d.ts",
"default": "./dist/content-collections.js"
},
"./components/I18nClient.astro": "./assets/components/I18nClient.astro",
"./components/I18nHead.astro": "./assets/components/I18nHead.astro"
},
"files": ["dist", "assets"],
"scripts": {
"dev": "tsup --watch",
"build": "tsup"
},
"scripts": {},
"type": "module",
"peerDependencies": {
"astro": "^4.4.11",
"i18next": "^23.10.0"
"i18next": "^23.0.0"
},
"dependencies": {
"@inox-tools/aik-route-config": "^0.1.1",
"astro-integration-kit": "^0.6.0",
"astro-pages": "^0.2.0",
"@inox-tools/aik-route-config": "^0.5.0",
"astro-integration-kit": "^0.13.1",
"astro-pages": "^0.3.0",
"sitemap": "^7.1.1",
"typescript": "^5.3.3",
"ufo": "^1.4.0",
"vite": "^5.1.4"
"typescript": "^5.4.5",
"ufo": "^1.5.3",
"vite": "^5.2.10"
},
"devDependencies": {
"@types/node": "^20.11.22"
"@types/node": "^20.12.7",
"tsup": "^8.0.2"
}
}
21 changes: 9 additions & 12 deletions package/src/i18next/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { join, relative } from "node:path";
import { fileURLToPath } from "node:url";
import type { HookParameters } from "astro";
import { watchIntegration } from "astro-integration-kit/utilities";
import { defineUtility, watchDirectory } from "astro-integration-kit";
import { normalizePath } from "vite";
import type { Options } from "../options.js";
import { getNamespaces } from "./namespaces.js";
import { getResources } from "./resources.js";
import { injectTypes } from "./types.js";

const getPaths = (
{ config }: HookParameters<"astro:config:setup">,
options: Options,
) => {
const getPaths = (root: URL, options: Options) => {
const localesDir = normalizePath(
fileURLToPath(new URL(options.localesDir, config.root)),
fileURLToPath(new URL(options.localesDir, root)),
);
const defaultLocalesDir = join(localesDir, options.defaultLocale);

Expand All @@ -25,12 +21,12 @@ const getPaths = (

const LOGGER_LABEL = "astro-i18n/i18next";

export const handleI18next =
(params: HookParameters<"astro:config:setup">) => (options: Options) => {
export const handleI18next = defineUtility("astro:config:setup")(
(params, options: Options) => {
const logger = params.logger.fork(LOGGER_LABEL);

const paths = getPaths(params, options);
watchIntegration({ ...params, dir: paths.localesDir });
const paths = getPaths(params.config.root, options);
watchDirectory(params, paths.localesDir);
logger.info(
`Registered watcher for "${normalizePath(
relative(fileURLToPath(params.config.root), paths.localesDir),
Expand All @@ -49,4 +45,5 @@ export const handleI18next =
namespaces,
resources,
};
};
},
);
3 changes: 2 additions & 1 deletion package/src/i18next/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export const getResources = (
const content = JSON.parse(readFileSync(path, "utf-8"));

resources[locale] ??= {};
resources[locale][basename(fileName, extname(fileName))] = content;
// biome-ignore lint/style/noNonNullAssertion: fallback is set above
resources[locale]![basename(fileName, extname(fileName))] = content;
} catch (err) {
logger.warn(`Can't parse "${path}", skipping.`);
}
Expand Down
35 changes: 18 additions & 17 deletions package/src/i18next/types.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { join, relative } from "node:path";
import { fileURLToPath } from "node:url";
import type { HookParameters } from "astro";
import { addDts } from "astro-integration-kit/utilities";
import { addDts, defineUtility } from "astro-integration-kit";
import { normalizePath } from "vite";
import type { Options } from "../options.js";
import type { getNamespaces } from "./namespaces.js";

export const injectTypes = (
{ logger, config }: HookParameters<"astro:config:setup">,
{ defaultNamespace }: Options,
importsData: ReturnType<typeof getNamespaces>["importsData"],
defaultLocalesDir: string,
) => {
const relativeLocalesPrefix = `${normalizePath(
relative(
fileURLToPath(new URL("./.astro/", config.root)),
defaultLocalesDir,
),
)}/`;
export const injectTypes = defineUtility("astro:config:setup")(
(
params,
{ defaultNamespace }: Options,
importsData: ReturnType<typeof getNamespaces>["importsData"],
defaultLocalesDir: string,
) => {
const relativeLocalesPrefix = `${normalizePath(
relative(
fileURLToPath(new URL("./.astro/", params.config.root)),
defaultLocalesDir,
),
)}/`;

const content = `
const content = `
declare module "i18next" {
interface CustomTypeOptions {
defaultNS: "${defaultNamespace}";
Expand All @@ -38,5 +38,6 @@ export const injectTypes = (
export {}
`;

addDts({ logger, ...config, name: "i18next", content });
};
addDts(params, { name: "i18next", content });
},
);
Loading
Loading