首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用Parcel在打字本中导入".html“文件

无法使用Parcel在打字本中导入".html“文件
EN

Stack Overflow用户
提问于 2022-04-14 11:09:55
回答 1查看 226关注 0票数 0

我正在尝试使用类型记录和包裹设置一个简单的项目。

在这个项目中,我试图从一个.html文件(或者更准确地说,它的路径)导入一个.ts文件

代码语言:javascript
复制
import html from "../renderer/index.html";
console.log(html); // file:///home/foo/bar/dist/main/renderer.1c7e1d82.html

正如在几个地方所提到的,为了使其正常工作,我添加了以下声明文件html.d.ts

代码语言:javascript
复制
declare module '*.html' {
  const value: string;
  export default value
}

和我的全部tsconfig.json

代码语言:javascript
复制
{
    "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文件,因为引发以下警告:

代码语言:javascript
复制
@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

代码语言:javascript
复制
{
  "extends": "@parcel/config-default",
  "transformers": {
    "*.{ts,tsx}": ["@parcel/transformer-typescript-tsc"]
  }
}

然而,它没有考虑到来自tsconfig的filesinclude参数。我尝试过的任何其他置换(比如删除文件)都有相同的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-15 12:48:08

包裹字符串内联使用bundle-text:前缀。

代码语言:javascript
复制
import html from "bundle-text:../renderer/index.html";

包裹自动添加devDependency @parcel/transformer-inline-string

更多报道来自https://parceljs.org/features/bundle-inlining

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

代码语言:javascript
复制
declare module "bundle-text:*" {
  const value: string;
  export default value;
}

并将行includes: ["**/*.ts"]添加到tsconfig.json中。

编辑:

更多关于tsconfig includeshttps://www.typescriptlang.org/tsconfig#include

注意,包含定义文件也很重要,这是模式"**/*.ts"所涵盖的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71870680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档