Open6
Nuxt Bridge + Vuetify2 + Storybook (2023-05)
素のNuxtをインストール (v2.15.8)
$ npx create-nuxt-app hello
create-nuxt-app v4.0.0
✨ Generating Nuxt.js project in hello
? Project name: hello
? Programming language: JavaScript
? Package manager: Npm
? UI framework: Vuetify.js
? Nuxt.js modules: Axios - Promise based HTTP client
? Linting tools: ESLint, Prettier
? Testing framework: None
? Rendering mode: Single Page App
? Deployment target: Static (Static/Jamstack hosting)
? Development tools: jsconfig.json (Recommended for VS Code if you're not using typescript)
? Continuous integration: None
? Version control system: Git
Nuxt Bridge に移行
package.json
- "nuxt": "^2.15.0"
+ "nuxt": "^2.16.0"
-
package-lock.json
を削除
$ npm install
$ npm run dev
- Nuxt v2.16 にて動作確認
- Nuxt Bridge をインストール
$ npm install -D @nuxt/bridge@npm:@nuxt/bridge-edge
package.json
{
"scripts": {
- "dev": "nuxt",
+ "dev": "nuxi dev",
- "build": "nuxt build",
+ "build": "nuxi generate", ※ for SPA
- "start": "nuxt start",
+ "start": "nuxi preview"
}
}
-
nuxt.config.js
の更新
nuxt.config.js
import { defineNuxtConfig } from '@nuxt/bridge'
export default defineNuxtConfig({
// Your existing configuration
})
defu エラー対処
file:///D:/try/hello/.nuxt/dev/index.mjs:15
import defu, { defuFn } from 'file://D:/try/hello/node_modules/defu/dist/defu.mjs';
^^^^^^
SyntaxError: The requested module 'file://D:/try/hello/node_modules/defu/dist/defu.mjs' does not provide an export named 'defuFn'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)
ERROR [worker reload] [worker init] The requested module 'file://D:/try/hello/node_modules/defu/dist/defu.mjs' does not provide an export named 'defuFn'
import defu, { defuFn } from 'D:/try/hello/node_modules/defu/dist/defu.mjs';
^^^^^^
SyntaxError: The requested module 'D:/try/hello/node_modules/defu/dist/defu.mjs' does not provide an export named 'defuFn'
at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)
-
defu
をインストールして対処
$ npm i -D defu
- 動作確認
$ npm run dev
Storybook (nuxt/storybook) インストール
$ npm i -D @nuxtjs/storybook
-
.gitignore
に追記
.gitignore
# Vim swap files
*.swp
+# nuxt/storybook
+.nuxt-storybook
+storybook-static
-
nuxt.config.js
に追記
nuxt.config.js
+ storybook: {
+ stories: ['~/stories/**/*.stories.js'],
+ },
- 動作確認のためサンプルを用意
-
stories/NuxtLogo.stories.js
を作成して記述
-
stories/NuxtLogo.stories.js
import NuxtLogo from '@/components/NuxtLogo.vue'
export default {
title: 'NuxtLogo',
component: NuxtLogo,
}
export const Default = () => '<NuxtLogo />'
- Storybook 起動・ビルドスクリプト
package.json
"scripts": {
"dev": "nuxi dev",
"build": "nuxi generate",
"start": "nuxi preview",
+ "storybook:run": "npx nuxt storybook",
+ "storybook:build": "npx nuxt storybook build",
"lint:js": "eslint --ext \".js,.vue\" --ignore-path .gitignore .",
"lint:prettier": "prettier --check .",
"lint": "npm run lint:js && npm run lint:prettier",
"lintfix": "prettier --write --list-different . && npm run lint:js -- --fix"
},
ESLint エラーの対処
▼
-
https://eslint.vuejs.org/user-guide/#does-not-work-well-with-script-setup
-
vue-eslint-parser をインストール
$ npm i -D vue-eslint-parser
.eslintrc.js
module.exports = {
root: true,
env: {
browser: true,
node: true,
},
parser: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 2020,
},
extends: [
'@nuxtjs',
'plugin:nuxt/recommended',
'eslint:recommended',
'prettier',
],
plugins: [],
// add your custom rules here
rules: {},
}
※ parserOptions
は オプショナルチェーン など新しい記法のために追記
-
ref
や{ useRoute, useRouter }
は NuxtBridge に含まれていないので、参照先を示すことでエラーを解消することができる。
jsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"~/*": ["./*"],
"@/*": ["./*"],
"~~/*": ["./*"],
"@@/*": ["./*"],
+ "#imports": [".nuxt/imports"],
+ "@nuxtjs/composition-api": [
+ "node_modules/@nuxt/bridge/dist/runtime/capi.legacy"
+ ]
}
},
"exclude": ["node_modules", ".nuxt", "dist"]
}
vueファイル記述参考
<script setup>
import { useRoute, useRouter } from '#imports'
// ...
ベースディレクトリの指定
GitHub Pages のようにデプロイ先がサブディレクトリの場合は以下のように設定する。
nuxt.config.js
import { defineNuxtConfig } from '@nuxt/bridge'
import colors from 'vuetify/es5/util/colors'
export default defineNuxtConfig({
router: {
base: '/sub/',
},
app: {
buildAssetsDir: '/_nuxt/',
baseURL: '/sub/',
},