Skip to content

Commit

Permalink
feat(config): allow different setupFile for server-side
Browse files Browse the repository at this point in the history
  • Loading branch information
Akryum committed Oct 3, 2022
1 parent 49546dc commit 3103374
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
15 changes: 14 additions & 1 deletion docs/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export default defineConfig({

## `setupFile`

`string`
`string | { browser: string, server: string }`

Setup file exporting a default function executed when setting up each story preview.

Expand All @@ -147,6 +147,19 @@ export default defineConfig({
})
```

If you need a different version for the NodeJS server (while collecting stories), you can use an object:

```ts
export default defineConfig({
setupFile: {
browser: '/src/histoire-setup.ts',
server: '/src/histoire-setup.server.ts',
},
})
```

This can be useful if you need to exclude some imported libraries that only works in the browser.

## `responsivePresets`

`Array`
Expand Down
11 changes: 10 additions & 1 deletion packages/histoire-shared/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,16 @@ export interface HistoireConfig {
*
* Example: `'/src/histoire-setup.ts'`
*/
setupFile?: string
setupFile?: string | {
/**
* Only loaded in the browser client.
*/
browser: string
/**
* Only loaded while collecting stories in the node server.
*/
server: string
}
/**
* Setup code created by plugins
*/
Expand Down
13 changes: 11 additions & 2 deletions packages/histoire/src/node/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,17 @@ export async function getViteConfigWithPlugins (isServer: boolean, ctx: Context)
return RESOLVED_STORIES_ID
}
if (id.startsWith(SETUP_ID)) {
if (ctx.config.setupFile) {
return this.resolve(resolve(ctx.root, ctx.config.setupFile), importer, {
const setupFileConfig = ctx.config.setupFile
if (setupFileConfig) {
let file: string
if (typeof setupFileConfig === 'string') {
file = setupFileConfig
} else if (isServer) {
file = setupFileConfig.server
} else {
file = setupFileConfig.browser
}
return this.resolve(resolve(ctx.root, file), importer, {
skipSelf: true,
})
} else {
Expand Down

0 comments on commit 3103374

Please sign in to comment.