項目使用的 Vue + TypeScript + webpack
,其中TypeScript
使用的是ts-loader
。
因爲使用了vue
的單文件組件
,因此ts-loader
配置了appendTsSuffixTo: [/\.vue$/]
。
可是發如今使用thread-loader
和cache-loader
加速構建時,會報Could not find file: '*.vue.ts'
的錯誤。可是項目中並無*.vue.ts
的文件。vue
appendTsSuffixTo
官方文檔給出的解釋:appendTsxSuffixTo webpack
A list of regular expressions to be matched against filename. If filename matches one of the regular expressions, a .ts or .tsx suffix will be appended to that filename.
This is useful for *.vue file format for now. (Probably will benefit from the new single file format in the future.)。
大體意思是,會給對應文件添加個.ts
或.tsx
後綴。這也就是報錯的找不到vue.ts
的由來。
讓咱們來梳理下ts編譯vue 單文件組件的過程:git
vue 單文件組件中假如使用了lang="ts"
,ts-loader
須要配置appendTsSuffixTo: [/\.vue$/]
,用來給.vue
文件添加個.ts
後綴用於編譯。
可是這個.ts
文件並不實際存在,因此在使用cache-loader
時,會報找不到這個文件的錯誤。github
因爲報的是找不到文件錯誤,那咱們就把 TypeScript代碼
從 .vue
中移出來。
使用一個單獨的ts文件,而後vue
在引用這個ts文件
。web
xxx.vue
文件:typescript
<template> <div> </div> </template> <script lang="ts" src="./xxx.ts"></script> <style> </style>
xxx.ts
文件:express
export default { }