我正在尝试使用类型记录和包裹设置一个简单的项目。
在这个项目中,我试图从一个.html文件(或者更准确地说,它的路径)导入一个.ts文件
import html from "../renderer/index.html";
console.log(html); // file:///home/foo/bar/dist/main/renderer.1c7e1d82.html正如在几个地方所提到的,为了使其正常工作,我添加了以下声明文件html.d.ts
declare module '*.html' {
const value: string;
export default value
}和我的全部tsconfig.json
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"sourceMap": false,
"strict": true,
"esModuleInterop": true,
},
"files": [
"src/main/main.ts"
],
"include": ["@types"]
}配置似乎是正确的,因为我的IDE似乎识别了上面的导入,运行tsc不会失败,并产生预期的结果(require("../renderer/index.html"))。
但是,当试图与包(parcel build main.ts)捆绑时。虽然编译本身按预期工作,但导入被new URL("renderer.1c7e1d82.html", "file:" + __filename).toString();替换时,它无法识别d.ts文件,因为引发以下警告:
@parcel/transformer-typescript-types: Cannot find module '../renderer/index.html' or its corresponding type declarations.
/home/angrykoala/Desktop/scratchpad/gaucho2/src/main/main.ts:9:18
8 |
> 9 | import html from "../renderer/index.html";
> | ^^^^^^^^^^^^^^^^^^^^^^^^^ Cannot find module '../renderer/index.html' or its corresponding type declarations.
10 | console.log(html)
11 | 根据文档,我的.pretierrc被设置为使用tsc
{
"extends": "@parcel/config-default",
"transformers": {
"*.{ts,tsx}": ["@parcel/transformer-typescript-tsc"]
}
}然而,它没有考虑到来自tsconfig的files或include参数。我尝试过的任何其他置换(比如删除文件)都有相同的问题。
发布于 2022-04-15 12:48:08
包裹字符串内联使用bundle-text:前缀。
import html from "bundle-text:../renderer/index.html";包裹自动添加devDependency @parcel/transformer-inline-string。
更多报道来自https://parceljs.org/features/bundle-inlining。
我通常创建一个定义文件global.d.ts,其中包括:
declare module "bundle-text:*" {
const value: string;
export default value;
}并将行includes: ["**/*.ts"]添加到tsconfig.json中。
编辑:
更多关于tsconfig includes在https://www.typescriptlang.org/tsconfig#include。
注意,包含定义文件也很重要,这是模式"**/*.ts"所涵盖的。
https://stackoverflow.com/questions/71870680
复制相似问题