可以导入。使用包裹中的打字稿中文件

发布于 2025-01-21 06:30:19 字数 2131 浏览 3 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静谧幽蓝 2025-01-28 06:30:19

包裹字符串内线是使用Bundle-Text:前缀完成的。

import html from "bundle-text:../renderer/index.html";

包裹会自动添加DevDepentency @Parcel/Transformer-Inline-string

更多信息 https://parceljs.org/features/bundle-inlining

我通常会创建一个定义文件global.d.ts,其中包括:

declare module "bundle-text:*" {
  const value: string;
  export default value;
}

添加行包括:[“ **/*。ts”] to tsconfig.json

编辑:
有关tsconfig 的更多更多信息.typescriptlang.org/tsconfig#包括。
请注意,还必须包括“定义文件”,该文件由模式“ **/*。ts”覆盖。

Parcel string inlining is done using bundle-text: prefix.

import html from "bundle-text:../renderer/index.html";

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:

declare module "bundle-text:*" {
  const value: string;
  export default value;
}

And add line includes: ["**/*.ts"] to tsconfig.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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文