How to configure VueJS + PostCss + Tailwind with Storybook

后端 未结 3 1134
自闭症患者
自闭症患者 2021-02-08 00:38

Anyone have success setting up a project using VueJS, PostCss, and Tailwind with component development in Storybook?

I\'ve gotten this far:

  1. New vue
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-08 01:28

    Working solution as a boilerplate project repository

    During the past few months I've had problems configuring Storybook with Vue, however I've got past those problems and am sharing here a working boilerplate project repository with your specific requirements plus some extras.

    The bounty offered to this question ask for an up-to-date solution. The thing with the latest Storybook versions 5.2 and 5.3, which entered beta just a few days ago, is that there have been two new story syntax formats coming up soon: Component Story Format (CSF) and MDX Syntax.

    Storybook 5.3 finally brings multi-framework support for these formats, as well as an initial release of the long anticipated Storybook Docs addon.

    However, as opt-in formats / features these are NOT configured in the repo currently. I can supply additional setups in separate branches if needed.

    So here is the working boilerplate project repository with Storybook 5.3 pre-release beta version using Tailwind CSS.

    The project is configured with Vue CLI 4 and TypeScript along with Composition API functional component format, as future-proofing for the upcoming Vue 3.0 release targeted at the end Q1/2020.

    Note about PostCSS and importing styles

    The main thing wrong with the question's setup is that PostCSS is not a language but rather a tool for transforming CSS with JavaScript, and Vue CLI is already configured using PostCSS internally.

    Also what was missing from the question and the previous answers is that styles needs to be imported not only in the app's main.js / main.ts file, but also in Storybooks's main config.js file.

    Initial setup steps

    # Check if Vue CLI is globally installed
    vue --version
    
    # If so, and if it's version is 3.x, uninstall it
    npm uninstall -g @vue/cli
    # OR if it was installed with Yarn
    yarn global remove @vue/cli
    
    # Need to use NPM insted of Yarn because in a later step
    # we are installing a forked package from Github repo
    # and it was not possible or as straightforward with Yarn.
    
    # Install currently newest Vue CLI version 4
    npm install -g @vue/cli
    
    # Create app
    vue create boilerplate-ts-vue-storybook-tailwind-postcss --packageManager npm
    
    # Project bootstrapping options chosen for the project
     ◉ Babel
     ◉ TypeScript
     ◯ Progressive Web App (PWA) Support
     ◯ Router
     ◯ Vuex
     ◉ CSS Pre-processors
     ◉ Linter / Formatter
     ◉ Unit Testing
     ◯ E2E Testing
    
    Vue CLI v4.0.5
    ? Please pick a preset: Manually select features
    ? Check the features needed for your project: Babel, TS, CSS Pre-processors, Linter, Unit
    ? Use class-style component syntax? No
    ? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? No
    ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with node-sass)
    ? Pick a linter / formatter config: Prettier
    ? Pick additional lint features: Lint on save
    ? Pick a unit testing solution: Jest
    ? Where do you prefer placing config for Babel, PostCSS, ESLint, etc.? In dedicated config files
    
    #  Go into the directory just created
    cd boilerplate-typescript-vue-storybook-tailwind-postcss
    
    # Add Vue Cli Storybook plugin from a forked repo with Storybook 5.3.0-beta.2
    npm install -D git+https://git@github.com/ux-engineer/vue-cli-plugin-storybook.git#v0.6.2
    
    # Run the plugin's installer
    vue add storybook --packageManager npm
    

    Project configurations and the rest of the steps

    Rest of the changes made can be looked up from the repo's commit history.

    Resources

    For setting up Tailwind with Vue I'd recommend following Markus Oberlehner's fine article series on the topic:

    Setting up Tailwind CSS with Vue.js

    Removing unused CSS from Tailwind with PurgeCSS

    Reusable Functional Vue.js Components with Tailwind CSS

    Thoughts about Utility-First CSS Frameworks

    Adam Wathan - Tailwind CSS Best Practice Patterns

提交回复
热议问题