[英]How to configure VueJS + PostCss + Tailwind with Storybook
任何人都成功地使用 VueJS、PostCss 和 Tailwind 建立了一个项目,并在 Storybook 中进行了组件开发?
我已经走了这么远:
vue
项目 ( vue-cli 3.0.5
)<style lang="postcss"> ... </style>
创建组件@apply
将实用程序类应用到组件我遇到的问题是,我为使用lang="postcss"
创建故事的任何组件在运行故事书时都在编译期间失败。
我尝试添加一个自定义webpack
配置来阻止错误,但我的组件都没有样式。
const path = require('path')
module.exports = {
module: {
rules: [
{
test: /\.postcss$/,
loaders: ["style-loader", "css-loader", "postcss-loader"],
include: path.resolve(__dirname, '../')
}
]
}
}
我也试着输入我的app.postcss
(其中进口顺风)的内stories.js
文件本身无济于事。 任何帮助,将不胜感激。
我在这个 github repo https://github.com/jerriclynsjohn/svelte-storybook-tailwind 中有一个完整的 Svelte + TailwindCSS + Storybook 示例
但是 Integrating 应该非常相似:
.storybook
文件夹中添加自定义webpack.config.js
并添加以下内容:const path = require('path');
module.exports = ({ config, mode }) => {
config.module.rules.push({
test: /\.css$/,
loaders: [
{
loader: 'postcss-loader',
options: {
sourceMap: true,
config: {
path: './.storybook/',
},
},
},
],
include: path.resolve(__dirname, '../storybook/'),
});
return config;
};
.storybook
文件夹中创建一个postcss.config.js
,内容如下:var tailwindcss = require('tailwindcss');
module.exports = {
plugins: [
require('postcss-import')(),
tailwindcss('./tailwind.config.js'), //This refers to your tailwind config
require('autoprefixer'),
],
};
utils.css
文件@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
<>.stories.js
引用你的 CSS 文件:import '../../css/utils.css';
我真的建议你参考 github repo 中的实现,它也有特定于 Storybook 的东西。 ( GitHub )
我以前从未使用过 tailwindCSS 或 postCSS(明确地),所以我决定借此机会学习设置/配置这两者。
您可以在此处的故事书中找到带有 Tailwind 样式组件的完整示例: https : //github.com/gurupras/vuejs-postcss-tailwind-with-storybook
vue create vue-postcss-tailwind-storybook
npm install tailwindcss
./node_modules/.bin/tailwind init
(生成tailwind.config.js
)module.exports = {
plugins: [
require('tailwindcss')('tailwind.config.js'),
require('autoprefixer')()
]
}
vue add storybook
src/style/tailwind.css
)@tailwind base;
@tailwind components;
@tailwind utilities;
import '@/style/tailwind.css'
main.js
import '@/style/tailwind.css'
到您的main.js
以确保它们可用于您的应用程序的所有部分src/components/alert.vue
/* eslint-disable import/no-extraneous-dependencies */
import { storiesOf } from '@storybook/vue'
// You need to import this once
import '@/style/tailwind.css'
import Alert from '@/components/alert.vue'
storiesOf('Alert', module)
.add('with text', () => ({
components: { Alert },
template: '<Alert text="Test alert" :show-close="true"/>'
}))
npm run storybook:serve
希望这对您的设置有所帮助。 同时,我将阅读并了解 TailwindCSS 如何帮助我创建更好的组件!
我不知道您为什么在lang <style lang="postcss"> ... </style>
添加了postcss
您应该只添加<style lang="css"> ... </style>
postcss不是一种语言,它是一个解析CSS的webpack插件
在过去的几个月里,我在用 Vue 配置 Storybook 时遇到了问题,但是我已经解决了这些问题,并在这里分享了一个工作样板项目存储库,其中包含您的特定要求和一些额外内容。
为这个问题提供的赏金要求提供最新的解决方案。 几天前刚刚进入测试版的最新 Storybook 版本 5.2 和 5.3 的事情是,即将推出两种新的故事语法格式: 组件故事格式 (CSF)和MDX 语法。
Storybook 5.3 终于为这些格式带来了多框架支持,以及期待已久的Storybook Docs插件的初始版本。
但是,作为选择加入的格式/功能,这些当前未在 repo 中配置。 如果需要,我可以在不同的分支中提供额外的设置。
所以这是使用 Tailwind CSS 的 Storybook 5.3 预发布测试版的工作样板项目存储库。
该项目使用Vue CLI 4和TypeScript以及Composition API功能组件格式进行配置,作为即将于 2020 年第一季度末发布的 Vue 3.0 版本的未来证明。
问题设置的主要错误在于PostCSS 不是一种语言,而是一种用 JavaScript 转换 CSS 的工具,并且Vue CLI 已经在内部使用 PostCSS 进行了配置。
此外,问题和之前的答案中还缺少样式,不仅需要在应用程序的main.js
/ main.ts
文件中导入样式,还需要在 Storybooks 的主config.js
文件中导入样式。
# 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
其余所做的更改可以从 repo's commit history 中查找。
要使用 Vue 设置 Tailwind,我建议您关注 Markus Oberlehner 关于该主题的精彩文章系列:
使用 PurgeCSS 从 Tailwind 中删除未使用的 CSS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.