Skip to content

Instantly share code, notes, and snippets.

@nasum
Last active December 25, 2018 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasum/0936c3631cc4af3c535635954239b5f8 to your computer and use it in GitHub Desktop.
Save nasum/0936c3631cc4af3c535635954239b5f8 to your computer and use it in GitHub Desktop.
Nuxt.js + TypeScript + ESLint をやる最小構成 ref: https://qiita.com/nasum/items/a1fc32e3540276161e59
{
"parser": "vue-eslint-parser", // パーサにvue-eslint-parserを指定しSFCをリント可能に
"parserOptions": {
"parser": "typescript-eslint-parser" // オプションでTypeScriptをパースできるパーサを指定
},
"extends": [
"eslint:recommended",
"typescript" // eslint-config-typescriptのルールを追加
],
"plugins": ["typescript"], // eslint-plugin-typescriptを指定
"rules": {
"semi": ["error", "never"] // 追加ルールでセミコロンの指摘を無視するようにする
}
}
module.exports = function() {
this.extendBuild(config => {
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue|ts)$/,
loader: 'eslint-loader',
exclude: /(node_modules)/,
})
})
}
これでリントされるようになります。
この状態で実行してみます。
```zsh
$ yarn run dev
$ yarn init
$ yarn add nuxt
$ mkdir pages
$ touch pages/index.vue
ERROR Failed to compile with 1 errors friendly-errors 23:59:03
ERROR in /Users/masaya/src/github.com/nasum/nuxt-typescript/pages/index.vue.ts friendly-errors 23:59:03
[tsl] ERROR in /Users/masaya/src/github.com/nasum/nuxt-typescript/pages/index.vue.ts(18,7) friendly-errors 23:59:03
TS2322: Type 'number' is not assignable to type 'string'.
$ yarn add -D eslint eslint-loader eslint-plugin-typescript typescript-eslint-parser vue-eslint-parser eslint-config-typescript
これらのパッケージをインストールしたので設定を書いていきます。
```json:.eslintrc
{
"parser": "vue-eslint-parser", // パーサにvue-eslint-parserを指定しSFCをリント可能に
"parserOptions": {
"parser": "typescript-eslint-parser" // オプションでTypeScriptをパースできるパーサを指定
},
"extends": [
"eslint:recommended",
"typescript" // eslint-config-typescriptのルールを追加
],
"plugins": ["typescript"], // eslint-plugin-typescriptを指定
"rules": {
"semi": ["error", "never"] // 追加ルールでセミコロンの指摘を無視するようにする
}
}
ERROR Failed to compile with 1 errors friendly-errors 00:32:00
ERROR in ./pages/index.vue friendly-errors 00:32:00
Module Error (from ./node_modules/eslint-loader/index.js): friendly-errors 00:32:00
/Users/masaya/src/github.com/nasum/nuxt-typescript/pages/index.vue
12:18 error Expected a semicolon typescript/member-delimiter-style
18:16 error Strings must use singlequote quotes
✖ 2 problems (2 errors, 0 warnings)
2 errors and 0 warnings potentially fixable with the `--fix` option.
$ yarn run dev
$ yarn add -D typescript ts-loader
$ yarn run dev
declare module "*.vue" {
import Vue from "vue";
const _default: Vue;
export default _default;
}
export default Vue.extend({
data(): Data {
return {
message: 1
}
}
})
export default Vue.extend({
data(): Data {
return {
message: 1
}
}
})
{
"name": "nuxt-typescript",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "nuxt"
},
"dependencies": {
"nuxt": "^2.3.4"
}
}
{
"compilerOptions": {
"target": "es5", // コンパイルするJSのバージョン指定。
"module": "esnext", // モジュールの形式の指定
"moduleResolution": "node", // モジュールの解決方法の指定
"allowSyntheticDefaultImports": true, // exportしていないモジュールのコンパイル時にエラーを出さないようにする
"allowJs": true, // コンパイル対象にJSを含んでもエラーを出さないようにする
"lib": ["esnext", "dom"], // コンパイルに含めるライブラリファイルの指定
}
}
module.exports = function() {
// Webpackのビルド設定を拡張する
this.extendBuild(config => {
config.module.rules.push(
{
test: /((client|server)\.js)|(\.tsx?)$/,
loader: 'ts-loader',
options: {
appendTsSuffixTo: [/\.vue$/]
}
}
)
for (let rule of config.module.rules) {
if (rule.loader === 'vue-loader') {
rule.options.loaders = {
ts: 'ts-loader'
}
}
}
if (config.resolve.extensions.indexOf('.ts') === -1) {
config.resolve.extensions.push('.ts')
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment