喜大普奔,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'
}))
}
})
}
复制代码