Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update example config for TypeScript compatibility #3

Merged
merged 1 commit into from
Dec 8, 2022

Conversation

blimmer
Copy link
Contributor

@blimmer blimmer commented Dec 7, 2022

The example config specified in the README:

// .storybook/main.js

const turbosnap = require('vite-plugin-turbosnap');

module.exports = {
  core: { builder: '@storybook/builder-vite' },
  async viteFinal(config, { configType }) {
    // Turbosnap is only useful when building for production
    if (configType === 'PRODUCTION') {
      config.plugins.push(turbosnap({ rootDir: config.root }));
    }

    // ...And any other config you need to change...

    // return the customized config
    return config;
  },
}

Produces errors in a .storybook/main.ts file.

  1. config.plugins is possibly undefined.
  2. config.root is possibly undefined.

Screen Shot 2022-12-07 at 15 24 55

This PR updates the example to be compatible with JS and TS.

@IanVS
Copy link
Owner

IanVS commented Dec 8, 2022

Thanks, @blimmer. This is actually what I do myself, what do you think of it?

import type { ViteFinal } from '@storybook/builder-vite';
import { mergeConfig } from 'vite';
import turbosnap from 'vite-plugin-turbosnap';

export const viteFinal: ViteFinal = async (config, { configType }) => {
  return mergeConfig(config, {
    plugins: configType === 'PRODUCTION' ? [turbosnap({ rootDir: config.root ?? process.cwd() })] : [],
  });
};

@blimmer
Copy link
Contributor Author

blimmer commented Dec 8, 2022

Ah, sure, since mergeConfig does a deep merge already, that seems reasonable. process.cwd() also seems like a reasonable fallback (I think that, in practice, it's pretty much always set anyway...).

I'll update the PR!

@IanVS
Copy link
Owner

IanVS commented Dec 8, 2022

Great, thanks!

@IanVS IanVS merged commit 8afda3e into IanVS:main Dec 8, 2022
@blimmer blimmer deleted the patch-1 branch December 8, 2022 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants