喜大普奔,Nuxt終於正式發佈2.0了,最近趁熱把博客從1.4升級到了2.0,而且用Typescript重構了下,能夠點Jooger.me看下,在升級Nuxt過程當中出現了一個小問題javascript
關於release 2.0的公告能夠查看官網的Release Notes以及官方的Demo,升級過程十分簡單,基本不須要什麼遷移成本,全部npm命令都跟之前同樣,只須要把一些關聯包升級一下便可vue
今天出現的問題是這樣的,隨着nuxt升級,webpack和vue-loader也分別升級到了4和15,升級事後,報了以下問題java
Invalid source file: ***.vue. Ensure that the files supplied to lint have a .ts, .tsx, .d.ts, .js or .jsx extension.
複製代碼
通常看到這個extension
的問題都下意識地想到webpack的resolve.extensions
沒有配置.ts或者.tsx擴展,但其實否則,仔細看前半句會發現是在處理.vue
文件的時候報的這個錯,因此很容易就想到應該是vue-loader
的問題了,在vue-loader的這個issue討論了這個問題webpack
耐心查看完,會發現實際上是tslint-loader的typeCheck在做怪,若是開啓這個選項,那就會致使上述錯誤,理由是這個選項致使在構建的時候tslint會lint整個vue文件,而不僅僅是文件裏的ts部分,因此直接解決辦法是把tslint-loader的typeCheck去掉git
至於爲啥會lint全文件,這個後續再驗證下,應該是vue-loader15的對vue文件進行拆分時出現問題github
並且關掉typeCheck會出現什麼問題,目前還未發現web
到這裏能夠其實再進一步思考一下,爲啥vue-cli3的tslint沒有報錯了,看了下vue-cli的ts插件cli-plugin-typescript裏的代碼vue-cli
addLoader({
loader: 'ts-loader',
options: {
transpileOnly: true,
appendTsSuffixTo: ['\\.vue$'],
// https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse
happyPackMode: useThreads
}
})
// make sure to append TSX suffix
tsxRule.use('ts-loader').loader('ts-loader').tap(options => {
options = Object.assign({}, options)
delete options.appendTsSuffixTo
options.appendTsxSuffixTo = ['\\.vue$']
return options
})
config
.plugin('fork-ts-checker')
.use(require('fork-ts-checker-webpack-plugin'), [{
vue: true,
tslint: options.lintOnSave !== false && fs.existsSync(api.resolve('tslint.json')),
formatter: 'codeframe',
// https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse
checkSyntacticErrors: useThreads
}])
複製代碼
能夠看到它是用了fork-ts-checker-webpack-plugin這個webpack插件,具體什麼用途能夠看看它的READMEtypescript
因此modules/typescript.js最終配置以下npm
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
module.exports = function () {
// Add .ts extension for store, middleware and more
this.nuxt.options.extensions.push("ts")
// Extend build
this.extendBuild(config => {
const tsLoader = {
loader: 'ts-loader',
options: {
transpileOnly: true,
appendTsSuffixTo: [/\.vue$/]
}
}
// Add TypeScript loader
config.module.rules.push(
Object.assign({
test: /((client|server)\.js)|(\.tsx?)$/
},
tsLoader
)
)
// Add .ts extension in webpack resolve
if (config.resolve.extensions.indexOf(".ts") === -1) {
config.resolve.extensions.push(".ts");
}
config.plugins.push(new ForkTsCheckerWebpackPlugin({
vue: true,
tslint: true,
formatter: 'codeframe',
// https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse
checkSyntacticErrors: process.env.NODE_ENV === 'production'
}))
})
}
複製代碼
更新
今天在用的時候,發現改了代碼save的時候,nuxt會從新hot reload,這時會報以下錯誤
Error: fork-ts-checker-webpack-plugin hooks are already in use
複製代碼
很明顯,是reload過程當中,加載了兩次,因此modules/typescript.js代碼更新以下
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
// 判斷時候load過這個plugin
let hasForkTsCheckerPlugin = false
module.exports = function () {
// Add .ts extension for store, middleware and more
this.nuxt.options.extensions.push("ts")
// Extend build
this.extendBuild(config => {
const tsLoader = {
loader: 'ts-loader',
options: {
transpileOnly: true,
appendTsSuffixTo: [/\.vue$/]
}
}
// Add TypeScript loader
config.module.rules.push(
Object.assign({
test: /((client|server)\.js)|(\.tsx?)$/
},
tsLoader
)
)
// Add .ts extension in webpack resolve
if (config.resolve.extensions.indexOf(".ts") === -1) {
config.resolve.extensions.push(".ts");
}
if (!hasForkTsCheckerPlugin) {
// 第一次load
hasForkTsCheckerPlugin = true
config.plugins.push(new ForkTsCheckerWebpackPlugin({
vue: true,
tslint: true,
formatter: 'codeframe',
// https://github.com/TypeStrong/ts-loader#happypackmode-boolean-defaultfalse
checkSyntacticErrors: process.env.NODE_ENV === 'production'
}))
}
})
}
複製代碼