@inlang/core
Advanced tools
Comparing version 0.7.9 to 0.7.10
@@ -16,5 +16,5 @@ import { setupPlugins } from "../plugin/setupPlugins.js"; | ||
} | ||
const config = await args.module.defineConfig(args.env); | ||
await setupPlugins({ config, env: args.env }); | ||
let config = await args.module.defineConfig(args.env); | ||
config = await setupPlugins({ config, env: args.env }); | ||
return zConfig.passthrough().parse(config); | ||
} |
@@ -37,4 +37,4 @@ //! DON'T TOP-LEVEL IMPORT ESBUILD PLUGINS. USE DYNAMIC IMPORTS. | ||
Context: The inlang build config defines this property | ||
and thereby ensures that your plugin is built in a way | ||
Context: The inlang build config defines this property | ||
and thereby ensures that your plugin is built in a way | ||
that is compatible with inlang. | ||
@@ -69,5 +69,5 @@ `); | ||
ops.plugins.push( | ||
// @ts-expect-error - esbuild plugins have a wrong type | ||
// @ts-expect-error | ||
NodeModulesPolyfillPlugin()); | ||
return ops; | ||
} |
@@ -23,2 +23,5 @@ import { PluginSetupError } from "./errors/PluginSetupError.js"; | ||
} | ||
// Note: we can't use structuredClone because the object could contain functions | ||
// To have some sort of immutability (for the first level), we destructure it into a new object | ||
const mergedConfig = { ...args.config }; | ||
for (let i = 0; i < args.config.plugins.length; i++) { | ||
@@ -31,4 +34,4 @@ try { | ||
const plugin = args.config.plugins[i]; | ||
const config = await plugin?.config(); | ||
deepmergeInto(args.config, config); | ||
const config = await plugin?.config({ ...mergedConfig }); | ||
deepmergeInto(mergedConfig, config); | ||
} | ||
@@ -44,4 +47,4 @@ catch (error) { | ||
// remove duplicates from languages in case multiple plugins add the same language. | ||
args.config.languages = [...new Set(args.config.languages)]; | ||
return args.config; | ||
mergedConfig.languages = [...new Set(mergedConfig.languages)]; | ||
return mergedConfig; | ||
} |
@@ -19,3 +19,3 @@ import type { InlangConfig } from "../config/schema.js"; | ||
id: `${string}.${string}`; | ||
config(): MaybePromise<Partial<InlangConfig>>; | ||
config(config: Readonly<Partial<InlangConfig>>): MaybePromise<Partial<InlangConfig> & Record<string, unknown>>; | ||
}; | ||
@@ -22,0 +22,0 @@ type MaybePromise<T> = Promise<T> | T; |
{ | ||
"name": "@inlang/core", | ||
"type": "module", | ||
"version": "0.7.9", | ||
"version": "0.7.10", | ||
"publishConfig": { | ||
@@ -6,0 +6,0 @@ "access": "public" |
@@ -22,5 +22,5 @@ import type { InlangConfig, InlangConfigModule } from "./schema.js" | ||
} | ||
const config = await args.module.defineConfig(args.env) | ||
await setupPlugins({ config, env: args.env }) | ||
let config = await args.module.defineConfig(args.env) | ||
config = await setupPlugins({ config, env: args.env }) | ||
return zConfig.passthrough().parse(config) as InlangConfig | ||
} |
@@ -22,5 +22,23 @@ import { it, expect } from "vitest" | ||
expect(plugin.id).toEqual("samuelstroschein.plugin-json") | ||
expect(plugin.config()).toEqual({ | ||
expect(plugin.config({})).toEqual({ | ||
languages: ["en", "de"], | ||
}) | ||
}) | ||
it("config function should receive config object", () => { | ||
const myPlugin = createPlugin(() => { | ||
return { | ||
id: "inlang.identity", | ||
config: (config) => { | ||
return { | ||
referenceLanguage: config.referenceLanguage | ||
} | ||
}, | ||
} | ||
}) | ||
const plugin = myPlugin()({} as InlangEnvironment) | ||
expect(plugin.config({ referenceLanguage: 'it' })).toEqual({ | ||
referenceLanguage: 'it' | ||
}) | ||
}) |
@@ -46,4 +46,4 @@ import type { BuildOptions } from "esbuild" | ||
Context: The inlang build config defines this property | ||
and thereby ensures that your plugin is built in a way | ||
Context: The inlang build config defines this property | ||
and thereby ensures that your plugin is built in a way | ||
that is compatible with inlang. | ||
@@ -83,3 +83,3 @@ `) | ||
ops.plugins.push( | ||
// @ts-expect-error - esbuild plugins have a wrong type | ||
// @ts-expect-error | ||
NodeModulesPolyfillPlugin(), | ||
@@ -86,0 +86,0 @@ ) |
@@ -71,1 +71,60 @@ import { it, expect, vi } from "vitest" | ||
}) | ||
it("should merge config and pass to all plugins in sequence", async () => { | ||
let config1: Record<string, unknown> = {} | ||
let config2: Record<string, unknown> = {} | ||
let config3: Record<string, unknown> = {} | ||
const config: Record<string, unknown> = await setupPlugins({ | ||
config: { | ||
plugins: [ | ||
{ | ||
id: 'test.1', | ||
config(config) { | ||
config1 = config | ||
return { | ||
test1: true | ||
} | ||
} | ||
}, | ||
{ | ||
id: 'test.2', | ||
config(config) { | ||
config2 = config | ||
return { | ||
test2: true | ||
} | ||
} | ||
}, | ||
{ | ||
id: 'test.3', | ||
config(config) { | ||
config3 = config | ||
delete (config as Record<string, unknown>).test1 | ||
return { | ||
test3: true | ||
} | ||
} | ||
} | ||
], | ||
}, | ||
env: {} as any, | ||
}) | ||
expect(config.test1).toBe(true) | ||
expect(config.test2).toBe(true) | ||
expect(config.test3).toBe(true) | ||
expect(config1.test1).toBe(undefined) | ||
expect(config2.test1).toBe(true) | ||
expect(config2.test2).toBe(undefined) | ||
expect(config3.test1).toBe(undefined) | ||
expect(config3.test2).toBe(true) | ||
expect(config3.test3).toBe(undefined) | ||
}) |
@@ -34,2 +34,6 @@ import type { InlangConfig } from "../config/schema.js" | ||
} | ||
// Note: we can't use structuredClone because the object could contain functions | ||
// To have some sort of immutability (for the first level), we destructure it into a new object | ||
const mergedConfig = { ...args.config } | ||
for (let i = 0; i < args.config.plugins.length; i++) { | ||
@@ -42,4 +46,4 @@ try { | ||
const plugin = args.config.plugins[i] as Plugin | ||
const config = await plugin?.config() | ||
deepmergeInto(args.config, config) | ||
const config = await plugin?.config({ ...mergedConfig }) | ||
deepmergeInto(mergedConfig, config) | ||
} catch (error) { | ||
@@ -55,5 +59,7 @@ // continue with next plugin. | ||
} | ||
// remove duplicates from languages in case multiple plugins add the same language. | ||
args.config.languages = [...new Set(args.config.languages)] | ||
return args.config as ConfigWithSetupPlugins | ||
mergedConfig.languages = [...new Set(mergedConfig.languages)] | ||
return mergedConfig as ConfigWithSetupPlugins | ||
} |
@@ -30,5 +30,5 @@ import type { InlangConfig } from "../config/schema.js" | ||
id: `${string}.${string}` | ||
config(): MaybePromise<Partial<InlangConfig>> | ||
config(config: Readonly<Partial<InlangConfig>>): MaybePromise<Partial<InlangConfig> & Record<string, unknown>> | ||
} | ||
type MaybePromise<T> = Promise<T> | T |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
448632
5172