-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
119 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { defineConfig } from 'histoire' | ||
|
||
export default defineConfig({ | ||
// outDir: 'hdist', | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,89 @@ | ||
import path from 'pathe' | ||
import fs from 'fs' | ||
import defu from 'defu' | ||
import { createServer } from 'vite' | ||
import { ViteNodeServer } from 'vite-node/server' | ||
import { ViteNodeRunner } from 'vite-node/client' | ||
import pc from 'picocolors' | ||
|
||
export interface HistoireConfig { | ||
sourceDir: string | ||
outDir: string | ||
storyMatch: string[] | ||
} | ||
|
||
export function getDefaultConfig (): HistoireConfig { | ||
return { | ||
sourceDir: 'src', | ||
outDir: 'histoire-dist', | ||
storyMatch: ['**/*.story.vue'], | ||
} | ||
} | ||
|
||
export const configFileNames = [ | ||
'histoire.config.ts', | ||
'histoire.config.js', | ||
'.histoire.ts', | ||
'.histoire.js', | ||
] | ||
|
||
export function resolveConfigFile (cwd: string = process.cwd()): string { | ||
const { root } = path.parse(cwd) | ||
let dir = cwd | ||
|
||
while (dir !== root) { | ||
for (const fileName of configFileNames) { | ||
const searchPath = path.join(dir, fileName) | ||
if (fs.existsSync(searchPath)) { | ||
return searchPath | ||
} | ||
} | ||
dir = path.dirname(dir) | ||
} | ||
|
||
return null | ||
} | ||
|
||
export async function loadConfigFile (configFile: string): Promise<Partial<HistoireConfig>> { | ||
try { | ||
const server = await createServer() | ||
await server.pluginContainer.buildStart({}) | ||
const node = new ViteNodeServer(server) | ||
const runner = new ViteNodeRunner({ | ||
root: path.dirname(configFile), | ||
fetchModule (id) { | ||
return node.fetchModule(id) | ||
}, | ||
}) | ||
const result: { default: Partial<HistoireConfig> } = await runner.executeFile(configFile) | ||
await server.close() | ||
if (!result.default) { | ||
throw new Error(`Expected default export in ${configFile}`) | ||
} | ||
return result.default | ||
} catch (e) { | ||
console.error(pc.red(`Error while loading ${configFile}`)) | ||
throw e | ||
} | ||
} | ||
|
||
export async function resolveConfig (cwd: string = process.cwd()): Promise<HistoireConfig> { | ||
let result: Partial<HistoireConfig> | ||
const configFile = resolveConfigFile(cwd) | ||
if (configFile) { | ||
result = await loadConfigFile(configFile) | ||
} else { | ||
result = {} | ||
} | ||
return processConfig(defu(result, getDefaultConfig())) | ||
} | ||
|
||
export function processConfig (config: HistoireConfig): HistoireConfig { | ||
config.sourceDir = path.resolve(process.cwd(), config.sourceDir) | ||
config.outDir = path.resolve(process.cwd(), config.outDir) | ||
return config | ||
} | ||
|
||
export function defineConfig (config: Partial<HistoireConfig>) { | ||
return config | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './config.js' |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.