关于ts-loader 中的 appendTsSuffixTo浅见
文章目录
引言
项目使用的 Vue + TypeScript + webpack,其中TypeScript 使用的是ts-loader。
由于使用了vue的单文件组件,所以ts-loader配置了appendTsSuffixTo: [/\.vue$/] 。
但是发现在使用thread-loader和cache-loader加速构建时,会报Could not find file: '*.vue.ts'的错误。但是项目中并没有*.vue.ts的文件。
关于appendTsSuffixTo
官方文档给出的解释:appendTsxSuffixTo
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 单文件组件的过程:
vue 单文件组件中假如使用了lang="ts",ts-loader需要配置appendTsSuffixTo: [/\.vue$/],用来给.vue文件添加个.ts后缀用于编译。
但是这个.ts文件并不实际存在,所以在使用cache-loader时,会报找不到这个文件的错误。
解决方案
由于报的是找不到文件错误,那我们就把 TypeScript代码 从 .vue中移出来。
使用一个单独的ts文件,然后vue在引用这个ts文件。
xxx.vue文件:
<template>
<div>
</div>
</template>
<script lang="ts" src="./xxx.ts"></script>
<style>
</style>
xxx.ts文件:
export default {
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论