Last active
December 25, 2018 01:07
-
-
Save nasum/0936c3631cc4af3c535635954239b5f8 to your computer and use it in GitHub Desktop.
Nuxt.js + TypeScript + ESLint をやる最小構成 ref: https://qiita.com/nasum/items/a1fc32e3540276161e59
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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"] // 追加ルールでセミコロンの指摘を無視するようにする | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ yarn init | |
$ yarn add nuxt | |
$ mkdir pages | |
$ touch pages/index.vue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ yarn add -D eslint eslint-loader eslint-plugin-typescript typescript-eslint-parser vue-eslint-parser eslint-config-typescript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
これらのパッケージをインストールしたので設定を書いていきます。 | |
```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"] // 追加ルールでセミコロンの指摘を無視するようにする | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ yarn run dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ yarn add -D typescript ts-loader |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ yarn run dev |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare module "*.vue" { | |
import Vue from "vue"; | |
const _default: Vue; | |
export default _default; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default Vue.extend({ | |
data(): Data { | |
return { | |
message: 1 | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default Vue.extend({ | |
data(): Data { | |
return { | |
message: 1 | |
} | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "nuxt-typescript", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"scripts": { | |
"dev": "nuxt" | |
}, | |
"dependencies": { | |
"nuxt": "^2.3.4" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"compilerOptions": { | |
"target": "es5", // コンパイルするJSのバージョン指定。 | |
"module": "esnext", // モジュールの形式の指定 | |
"moduleResolution": "node", // モジュールの解決方法の指定 | |
"allowSyntheticDefaultImports": true, // exportしていないモジュールのコンパイル時にエラーを出さないようにする | |
"allowJs": true, // コンパイル対象にJSを含んでもエラーを出さないようにする | |
"lib": ["esnext", "dom"], // コンパイルに含めるライブラリファイルの指定 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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