在类型脚本项目中,我有一些要作为字符串导入的原始文本文件,这些文件应捆绑在最终的js文件中。
我在项目中使用Webpack 5.21。
假设我的源文件中有某处:
我希望从我的app.ts文件中可以这样导入:
import {somefile } from './templates'
somefile
应该是包含tmpl.html文件原始内容的字符串。
这是我尝试过的:
在我的webpack.config.ts文件中,我添加了以下规则:
{
test: /\.tmpl\.html$/i,
type:'asset/source',
exclude: /node_modules/,
}
我还添加了一个解析扩展:
resolve: {
extensions: ['.tsx', '.ts', '.js', '.tmpl.html'],
},
在我的templates\index.ts
档案中,我尝试过:
import somefile from './somefile.tmpl.html'
export default {
somefile
}
但是,当我webpack
从命令行调用时,得到了:
[tsl] ERROR in C:\Code\myproject\src\templates\index.ts(4,23)
TS2307: Cannot find module './somefile.tmpl.html' or its corresponding type declarations.
少了什么东西 ?
PS:我的背景是一个自定义构建系统,该系统接受了一些打字稿文件,并将其转换为独立的jsfiles。我不是正在运行的Web应用程序。
此错误与Typescript有关,无法识别.html
模块
您需要添加一个d.ts
文件,其中包含
// global.d.ts
declare module '*.tmpl.html' {
const value: string;
export default value;
}
// tsconfig.json
{
compiler: {
...
},
include: ["path/to/global.d.ts"]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句