vue3-ts-vuetify-sb
Version:
Template for Vue 3, TypeScript, Vuetify and Storybook
339 lines (257 loc) • 7.82 kB
Markdown
Template for Vue 3, TypeScript, Vuetify and Storybook.
Use [degit](https://github.com/Rich-Harris/degit) to install this template.
```
npx degit manuelgarciacr/vue3-ts-vuetify-sb project-name
```
The steps to create this template were:
```
vue create project-name
Vue CLI v5.0.4
(*) Babel
(*) TypeScript
(*) Progressive Web App (PWA) Support
(*) Router
(*) Vuex
(*) CSS Pre-processors
(*) Linter / Formatter
(*) Unit Testing
(*) E2E Testing
```
### Check the features needed for the project
```
? Choose a version of Vue.js that you want to start the project with 3.x
? Use class-style component syntax? Yes
? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? Yes
? Use history mode for router? (Requires proper server setup for index fallback in production) Yes
? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass)
? Pick a linter / formatter config: Basic
? Pick additional lint features: Lint on save
? Pick a unit testing solution: Jest
? Pick an E2E testing solution: Cypress
? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config
```
## 2) Vue settings
### Configuring eslint to not warn about vue macros
```
// .eslintrc.js
module.exports = {
...,
env: {
...,
'vue/setup-compiler-macros': true
},
...
}
```
```
vue add vuetify
Choose a preset: Vuetify 3 Preview (Vuetify 3)
? Would you like to install Vuetify 3 nightly build? (WARNING: Nightly builds are intended for development testing and may include bugs or other issues.) Yes
```
```
// src/shims-vuetify.d.ts
declare module 'vuetify'
declare module 'vuetify/lib/components'
declare module 'vuetify/lib/directives'
declare module 'vuetify/lib/util/colors'
declare module 'vuetify/lib/styles/generic'
...
```
```
// src/plugins/vuetify.ts
...
import colors from 'vuetify/lib/util/colors'
const light = {
dark: false,
colors: {
background: '#FFFFFF',
surface: '#FFFFFF',
primary: colors.deepPurple.base,
'primary-lighten-4': colors.deepPurple.lighten4,
secondary: colors.teal.base,
error: colors.red.base,
info: colors.cyan.base,
success: colors.green.base,
warning: colors.amber.base,
}
}
export default createVuetify({
theme: {
options: {
customProperties: true,
},
variations: {
colors: ['primary'],
lighten: 2,
darken: 2,
},
defaultTheme: 'light',
themes: {
light,
dark: {
dark: true,
colors: {
primary: colors.red.darken1, // #E53935
secondary: colors.red.lighten4, // #FFCDD2
accent: colors.indigo.base, // #3F51B5
}
},
...
},
...
},
...
})
```
### Vuetify types in tsconfig
```
// tsconfig.json
{
...
"compilerOptions": {
...
"types": [
...
"vuetify",
]
}
}
```
```
// src/App.vue
<template>
<v-app>
<v-main>
<nav>
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>
</nav>
<router-view />
</v-main>
</v-app>
</template>
<style lang="scss">
nav {
padding: 30px;
text-align: center;
a {
font-weight: bold;
color:
&.router-link-exact-active {
color:
}
}
}
</style>
```
```
src/components/GroupCard.vue
Added to:
src/components/HelloWorld.vue
```
```
npx sb init
Do you want to run the 'eslintPlugin' migration on your project? ... yes
✅ Adding dependencies: eslint-plugin-storybook
```
<br/>
Stackoverflow error fix: [Webpack error adding storybook to create-react-app. Answer](https://stackoverflow.com/a/70827263/10204768)
```
npm remove @storybook/builder-webpack4 @storybook/manager-webpack4
npm install @storybook/builder-webpack5 @storybook/manager-webpack5 --save-dev
// .storybook/main.js
module.exports = {
...
"core": {
"builder": 'webpack5',
},
...
};
```
Storybook uses its own application entry point, bypassing the 'src/main.ts' file. We must configure our environment within the configuration files in the '.storybook' folder.
<br/>
Storybook webpack configuration: [Extending Storybook’s Webpack config](https://storybook.js.org/docs/react/builders/webpack#extending-storybooks-webpack-config)
```
// .storybook/main.js
const path = require('path');
module.exports = {
...,
webpackFinal: async (config, { configType }) => {
// `configType` has a value of 'DEVELOPMENT' or 'PRODUCTION'
// You can change the configuration based on that.
// 'PRODUCTION' is used when building the static version of storybook.
// Make whatever fine-grained changes you need
config.module.rules.push({
test: /\.scss$/,
use: ['style-loader', 'css-loader', 'sass-loader'],
include: path.resolve(__dirname, '../'),
});
// Return the altered config
return config;
},
...
};
```
```
// .storybook/preview.js
...
import { app } from '@storybook/vue3'
import vuetify from '../src/plugins/vuetify'
import { VApp } from 'vuetify/components';
app.use(vuetify).component("VApp", VApp)
export const decorators = [story => ({
components: { story },
template: '<v-app><story /></v-app>',
})]
...
```
```
// .storybook/main.js
...
const { VuetifyPlugin } = require('webpack-plugin-vuetify');
module.exports = {
...,
webpackFinal: async (config, { configType }) => {
...;
config.plugins.push(new VuetifyPlugin({ autoImport: true }));
...
},
...
}
```
```
// .storybook/preview.js
...
import { loadFonts } from '../src/plugins/webfontloader';
loadFonts();
...
```