@andersundsehr/storybook-typo3
Version:
The one and only Storybook Renderer for TYPO3 Fluid Components
62 lines (61 loc) • 2.33 kB
JavaScript
import { glob } from 'node:fs/promises';
import { basename } from 'node:path';
import { vite404Plugin } from "./vite404Plugin.js";
async function isFeatureEnabled(name, envs) {
return ['true', '1'].includes(envs[name] || '0');
}
function addProxyPlugin(config, url) {
config.plugins = [...(config.plugins || []), vite404Plugin(url)];
return config;
}
export async function viteFinal(config, options) {
const envs = await options.presets.apply('env');
if (await isFeatureEnabled('STORYBOOK_TYPO3_WATCH_ONLY_STORIES', envs)) {
config = await watchOnlyStoriesConfig(config, options);
}
config = addAllowedHosts(config);
config = addProxyPlugin(config, envs.STORYBOOK_TYPO3_ENDPOINT);
return config;
}
async function watchOnlyStoriesConfig(config, options) {
const colorYellow = '\x1b[33m';
const colorReset = '\x1b[0m';
console.log(colorYellow + '@andersundsehr/storybook-typo3:' + colorReset + ' STORYBOOK_TYPO3_WATCH_ONLY_STORIES enabled, only watching stories files');
const defaultGlobPattern = '/**/*.@(mdx|stories.@(mdx|js|jsx|mjs|ts|tsx))';
const storiesGlob = await options.presets.apply('stories');
const storiesFiles = [];
for (let globs of storiesGlob) {
if (typeof globs !== 'string') {
globs = globs.directory + (globs.files || defaultGlobPattern);
}
for await (const entry of glob(globs)) {
storiesFiles.push(entry);
}
}
const alwaysWatch = ['.env'];
config.server = config.server || {};
config.server.watch = config.server.watch || {};
config.server.watch.ignored = (file) => {
const filename = basename(file);
if (alwaysWatch.includes(filename)) {
return false; // always watch these files
}
if (!filename.includes('.')) {
return false; // ignore directories
}
if (!storiesFiles.includes(file)) {
return true;
}
return false;
};
return config;
}
function addAllowedHosts(config) {
var _a;
config.server = config.server || {};
if (process.env.IS_DDEV_PROJECT && config.server.allowedHosts !== true) {
(_a = config.server).allowedHosts ?? (_a.allowedHosts = []);
config.server.allowedHosts.push('.ddev.site');
}
return config;
}