histoire
Version:
A fast story book powered by Vite
30 lines (29 loc) • 1.2 kB
JavaScript
import { generateStoryVitestShim } from './story-vitest-shim/index.js';
import { isGeneratedVitestSpec, isVitestRuntimeImporter } from './vitest-collect-stub.js';
/** Owned shim identity is also trusted by non-executing dependency analysis. */
export const STORY_VITEST_SHIM_ID = '\0virtual:$histoire-story-vitest';
/**
* Serves the browser-compatible `vitest` shim to story files.
*
* Components, setup modules and imported helpers can also register story
* tests. Vitest's own browser runtime retains its real exports.
* @param ctx The histoire context.
* @param paths Project module paths the shim re-exports from.
*/
export function createStoryVitestShimPlugin(ctx, paths) {
return {
name: 'histoire:story-vitest-shim',
enforce: 'pre',
resolveId(id, importer) {
if (id === 'vitest' && importer && importer !== STORY_VITEST_SHIM_ID && !isVitestRuntimeImporter(importer) && !isGeneratedVitestSpec(importer)) {
return STORY_VITEST_SHIM_ID;
}
},
load(id) {
if (id !== STORY_VITEST_SHIM_ID) {
return;
}
return generateStoryVitestShim(paths);
},
};
}