diff --git a/.changeset/giant-emus-know.md b/.changeset/giant-emus-know.md new file mode 100644 index 0000000..2a412ab --- /dev/null +++ b/.changeset/giant-emus-know.md @@ -0,0 +1,5 @@ +--- +"kopflos": minor +--- + +Allow plugins to be defined using paths relative to config path diff --git a/packages/cli/lib/config.ts b/packages/cli/lib/config.ts index b0ad9c8..ced6b82 100644 --- a/packages/cli/lib/config.ts +++ b/packages/cli/lib/config.ts @@ -1,3 +1,4 @@ +import { resolve, dirname } from 'node:path' import type { CosmiconfigResult } from 'cosmiconfig' import { cosmiconfig } from 'cosmiconfig' import type { KopflosConfig } from '@kopflos-cms/core' @@ -44,6 +45,14 @@ export async function prepareConfig({ mode, config, watch, variable }: PrepareCo const watchedPaths = loadedConfig.watch || [] + loadedConfig.plugins = Object.fromEntries(Object.entries(loadedConfig.plugins || {}).map(([plugin, options]) => { + if (plugin.startsWith('.')) { + return [resolve(dirname(configPath), plugin), options] + } + + return [plugin, options] + })) + return { mode, ...loadedConfig, diff --git a/packages/cli/test/fixtures/config.with-relative.json b/packages/cli/test/fixtures/config.with-relative.json new file mode 100644 index 0000000..c15a2ba --- /dev/null +++ b/packages/cli/test/fixtures/config.with-relative.json @@ -0,0 +1,6 @@ +{ + "baseIri": "https://example.com/", + "plugins": { + "./foo/bar.js": {} + } +} diff --git a/packages/cli/test/lib/config.test.ts b/packages/cli/test/lib/config.test.ts index 4e11d45..16d409f 100644 --- a/packages/cli/test/lib/config.test.ts +++ b/packages/cli/test/lib/config.test.ts @@ -65,5 +65,23 @@ describe('kopflos/lib/config.js', function () { // then expect(config.watch).to.contain.all.members([configPath, 'lib']) }) + + it('rebase relative plugin paths to config path', async () => { + // given + const configPath = url.fileURLToPath(new URL('../fixtures/config.with-relative.json', import.meta.url)) + + // when + const config = await prepareConfig({ + config: configPath, + mode: 'development', + watch: true, + variable: {}, + }) + + // then + expect(config.plugins).to.contain.keys([ + url.fileURLToPath(new URL('../fixtures/foo/bar.js', import.meta.url)), + ]) + }) }) })