可以导入。使用包裹中的打字稿中文件
我正在尝试使用TypeScript和 parcel ”。
在此项目中,我正在尝试导入.html
file(或更准确地,更准确地说是其路径
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
不会失败,并产生预期的结果(quirect(“ ../ Renderer/index.html”)
)。
但是,尝试与包裹捆绑时(包裹构建main.ts
)。虽然汇编本身可以按预期运行,但导入被new URL替换(“ Renderer.1c7e1d82.html”,“ file:” + __fileName).toString().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"]
}
}
但是,它未能考虑到或includ
来自TSCONFIG的参数。我尝试过的任何其他置换(例如删除文件)都有相同的问题。
I'm trying to setup a simple project using Typescript and Parcel.
Within this project, I'm trying to import a .html
file (or, more accurately, its path) from a .ts
:
import html from "../renderer/index.html";
console.log(html); // file:///home/foo/bar/dist/main/renderer.1c7e1d82.html
As mentioned in several places, in order for this to work I added the following declaration file html.d.ts
:
declare module '*.html' {
const value: string;
export default value
}
And my full 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"]
}
The configuration appears to be correct, as my IDE seems to recognise the import above, and running tsc
doesn't fail and yields the expected result (require("../renderer/index.html")
).
However, when trying to bundle with Parcel (parcel build main.ts
). While the compilation itself works as expected, with the import being substituted by new URL("renderer.1c7e1d82.html", "file:" + __filename).toString();
It fails to recognise the d.ts
file, as the following warning is thrown:
@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 |
As per the documentation, my .pretierrc
is set to use tsc
:
{
"extends": "@parcel/config-default",
"transformers": {
"*.{ts,tsx}": ["@parcel/transformer-typescript-tsc"]
}
}
And yet, it fails to take into account files
or include
parameters from tsconfig. Any other permutation I've tried (such as removing files) had the same issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
包裹字符串内线是使用
Bundle-Text:
前缀完成的。包裹会自动添加DevDepentency
@Parcel/Transformer-Inline-string
。更多信息 https://parceljs.org/features/bundle-inlining 。
我通常会创建一个定义文件
global.d.ts
,其中包括:添加行
包括:[“ **/*。ts”]
totsconfig.json
。编辑:
有关
tsconfig
的更多更多信息.typescriptlang.org/tsconfig#包括。
请注意,还必须包括“定义文件”,该文件由模式
“ **/*。ts”
覆盖。Parcel string inlining is done using
bundle-text:
prefix.Parcel automatically adds devDependency
@parcel/transformer-inline-string
.More at https://parceljs.org/features/bundle-inlining.
I usually create a definition file
global.d.ts
which includes:And add line
includes: ["**/*.ts"]
totsconfig.json
.EDIT:
More about
tsconfig
includes
at https://www.typescriptlang.org/tsconfig#include.Note it's important to include the definition file as well which is covered by pattern
"**/*.ts"
.