official plugin API #301
-
ProblemDefining ProposalChange it from this: /**
* @type {import("@inlang/core/config").DefineConfig}
*/
export async function defineConfig(env) {
const plugin = await env.$import(
"https://cdn.jsdelivr.net/gh/ivanhofer/inlang-plugin-typesafe-i18n/dist/index.js"
)
return {
referenceLanguage: 'en',
languages: ['en', 'de'],
readResources: (args) =>
plugin.readResources({ ...args, ...env }),
writeResources: (args) =>
plugin.writeResources({ ...args, ...env }),
}
} to this: /**
* @type {import("@inlang/core/config").DefineConfig}
*/
export async function defineConfig(env) {
const plugin = await env.$import(
"https://cdn.jsdelivr.net/gh/ivanhofer/inlang-plugin-typesafe-i18n/dist/index.js"
)
return {
referenceLanguage: 'en',
languages: ['en', 'de'],
plugin: plugin(/* optional configuration of plugin */),
} Additional informationThis would be a breaking change, because the export of the plugin would slightly change. export default plugin = (config: PluginOptions) => {
// do some initialization steps
return {
readResources: ({ $fs, $import }) => /* implementation */,
writeResources: ({ $fs, $import }) => /* implementation */,
}
} This could open up the possibility for the plugin to provide useful information e.g. a library knows best what locales are set up in that repo. Currently you would need to mange two list of supported locales. One for the library and one for the inlang config. For /**
* @type {import("@inlang/core/config").DefineConfig}
*/
export async function defineConfig(env) {
/**
* @type {import("typesafe-i18n/config")}
*/
const { getLocaleInformation } = await env.$import("https://cdn.jsdelivr.net/npm/[email protected]/config/index.mjs")
// initialize the plugin
const plugin = await env.$import(
"https://cdn.jsdelivr.net/gh/ivanhofer/inlang-plugin-typesafe-i18n/dist/index.js"
)
// get the locale information from `typesafe-i18n`
const { base, locales } = await getLocaleInformation(env.$fs)
return {
referenceLanguage: base,
languages: locales,
readResources: (args) =>
plugin.readResources({ ...args, ...env }),
writeResources: (args) =>
plugin.writeResources({ ...args, ...env }),
}
} But the plugin could also return /**
* @type {import("@inlang/core/config").DefineConfig}
*/
export async function defineConfig(env) {
const plugin = await env.$import(
"https://cdn.jsdelivr.net/gh/ivanhofer/inlang-plugin-typesafe-i18n/dist/index.js"
)
return {
plugin: plugin()
}
} The current downside of this is, that someone is not able to modify the behaviour of the plugin, but |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 6 replies
-
The plugin API is purposely non-existent right now. I intend to collect feedback, like this issue, to design a plugin API that covers use cases that we might not foresee right now.
I believe that you are referring to the plugin template. This would not be a breaking change for inlang. As plugin owner, you are (for now) free to choose whatever API you'd like. Granted, that will lead to plugins likely having different configuration APIs but gives inlang the possibility to "nail" an official plugin API based on best practices/different use cases that emerge from plugins. So keep the feedback coming! |
Beta Was this translation helpful? Give feedback.
-
You could do the following for typesafe-i18n: /**
* @type {import("@inlang/core/config").DefineConfig}
*/
export async function defineConfig(env) {
const typesafeI18nPlugin = await env.$import(
"https://cdn.jsdelivr.net/gh/ivanhofer/inlang-plugin-typesafe-i18n/dist/index.js"
)
// default export or named export. named export seems clearer to me
const pluginConfig = typesafeI18nPlugin.defineConfig({ // your plugin config })
return pluginConfig But, that approach will fail once the user needs/wants to define custom properties. Merging objects with functions is not possible without external libs. Another alternative could be wrapping the config like so: /**
* @type {import("@inlang/core/config").DefineConfig}
*/
export async function defineConfig(env) {
const typesafeI18nPlugin = await env.$import(
"https://cdn.jsdelivr.net/gh/ivanhofer/inlang-plugin-typesafe-i18n/dist/index.js"
)
// default export or named export. named export seems clearer to me
const withTypesafeI18n = typesafeI18nPlugin.defineConfig({ // your plugin config })
return withTypesafeI18n({
// custom config
}) |
Beta Was this translation helpful? Give feedback.
-
I'll try to make the But I think it would be best if all plugins would have the same API at the end. Makes developing new plugins easier and switching between plugins will be really easy. |
Beta Was this translation helpful? Give feedback.
-
Implemented :) |
Beta Was this translation helpful? Give feedback.
Implemented :)