关于ts-loader 中的 appendTsSuffixTo浅见

引言

项目使用的 Vue + TypeScript + webpack,其中TypeScript 使用的是ts-loader
因为使用了vue单文件组件,因此ts-loader配置了appendTsSuffixTo: [/\.vue$/]
可是发如今使用thread-loadercache-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 {
}

参考

  1. threader-loader例子
  2. Vue single file , after add lang="ts",Module build failed: Error: Could not find file: '*.vue'.
  3. ts-loader
  4. awesome-typescript-loader
相关文章
相关标签/搜索