From 2da41514cc0e66d2bb85097219fc901397dca71f Mon Sep 17 00:00:00 2001 From: j3rem1e Date: Mon, 29 Mar 2021 23:02:11 +0200 Subject: [PATCH] CLI: Compatibility with sveltekit --- lib/cli/src/generators/SVELTE/index.ts | 10 ++++++++++ lib/cli/src/generators/baseGenerator.ts | 3 +++ lib/cli/src/generators/configure.ts | 15 +++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/cli/src/generators/SVELTE/index.ts b/lib/cli/src/generators/SVELTE/index.ts index 1d6c6c00640f..f4f380870f4c 100644 --- a/lib/cli/src/generators/SVELTE/index.ts +++ b/lib/cli/src/generators/SVELTE/index.ts @@ -5,6 +5,7 @@ import { baseGenerator, Generator } from '../baseGenerator'; const generator: Generator = async (packageManager, npmOptions, options) => { let extraMain; + let commonJs = false; // svelte.config.js ? if (fse.existsSync('./svelte.config.js')) { logger.info("Configuring preprocessor from 'svelte.config.js'"); @@ -12,6 +13,14 @@ const generator: Generator = async (packageManager, npmOptions, options) => { extraMain = { svelteOptions: { preprocess: '%%require("../svelte.config.js").preprocess%%' }, }; + } else if (fse.existsSync('./svelte.config.cjs')) { + logger.info("Configuring preprocessor from 'svelte.config.cjs'"); + + commonJs = true; + + extraMain = { + svelteOptions: { preprocess: '%%require("../svelte.config.cjs").preprocess%%' }, + }; } else { // svelte-preprocess dependencies ? const packageJson = packageManager.retrievePackageJson(); @@ -29,6 +38,7 @@ const generator: Generator = async (packageManager, npmOptions, options) => { extraAddons: ['@storybook/addon-svelte-csf'], extensions: ['js', 'jsx', 'ts', 'tsx', 'svelte'], extraMain, + commonJs, }); }; diff --git a/lib/cli/src/generators/baseGenerator.ts b/lib/cli/src/generators/baseGenerator.ts index 94e91b6767a0..85a270f8235c 100644 --- a/lib/cli/src/generators/baseGenerator.ts +++ b/lib/cli/src/generators/baseGenerator.ts @@ -20,6 +20,7 @@ export interface FrameworkOptions { addESLint?: boolean; extraMain?: any; extensions?: string[]; + commonJs?: boolean; } export type Generator = ( @@ -38,6 +39,7 @@ const defaultOptions: FrameworkOptions = { addESLint: false, extraMain: undefined, extensions: undefined, + commonJs: false, }; export async function baseGenerator( @@ -105,6 +107,7 @@ export async function baseGenerator( configure(framework, { addons: [...addons, ...extraAddons], extensions, + commonJs: options.commonJs, ...mainOptions, }); if (addComponents) { diff --git a/lib/cli/src/generators/configure.ts b/lib/cli/src/generators/configure.ts index 3a3a7355ccd9..1cca8936ee07 100644 --- a/lib/cli/src/generators/configure.ts +++ b/lib/cli/src/generators/configure.ts @@ -4,6 +4,7 @@ import { SupportedFrameworks } from '../project_types'; interface ConfigureMainOptions { addons: string[]; extensions?: string[]; + commonJs?: boolean; /** * Extra values for main.js * @@ -19,6 +20,7 @@ interface ConfigureMainOptions { function configureMain({ addons, extensions = ['js', 'jsx', 'ts', 'tsx'], + commonJs = false, ...custom }: ConfigureMainOptions) { const prefix = fse.existsSync('./src') ? '../src' : '../stories'; @@ -35,10 +37,12 @@ function configureMain({ .replace(/['"]%%/g, '') .replace(/%%['"]/, '')}`; fse.ensureDirSync('./.storybook'); - fse.writeFileSync('./.storybook/main.js', stringified, { encoding: 'utf8' }); + fse.writeFileSync(`./.storybook/main.${commonJs ? 'cjs' : 'js'}`, stringified, { + encoding: 'utf8', + }); } -function configurePreview(framework: SupportedFrameworks) { +function configurePreview(framework: SupportedFrameworks, commonJs: boolean) { const parameters = ` export const parameters = { actions: { argTypesRegex: "^on[A-Z].*" }, @@ -60,11 +64,14 @@ setCompodocJson(docJson); ${parameters}` : parameters; - fse.writeFileSync('./.storybook/preview.js', preview, { encoding: 'utf8' }); + fse.writeFileSync(`./.storybook/preview.${commonJs ? 'cjs' : 'js'}`, preview, { + encoding: 'utf8', + }); } export function configure(framework: SupportedFrameworks, mainOptions: ConfigureMainOptions) { fse.ensureDirSync('./.storybook'); + configureMain(mainOptions); - configurePreview(framework); + configurePreview(framework, mainOptions.commonJs); }