Skip to content

Commit

Permalink
feat: relative plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Jan 21, 2025
1 parent 5b6b1ab commit 11cc57c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-emus-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"kopflos": minor
---

Allow plugins to be defined using paths relative to config path
9 changes: 9 additions & 0 deletions packages/cli/lib/config.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/test/fixtures/config.with-relative.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"baseIri": "https://example.com/",
"plugins": {
"./foo/bar.js": {}
}
}
18 changes: 18 additions & 0 deletions packages/cli/test/lib/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
])
})
})
})

0 comments on commit 11cc57c

Please sign in to comment.