Unable to bring in the ".html" document in Typescript utilizing Parcel

I am currently in the process of setting up a basic project using Typescript and Parcel.

As part of this project, my goal is to import a .html file (or more specifically, its path) from a .ts file:

import html from "../renderer/index.html";
console.log(html); // file:///home/foo/bar/dist/main/renderer.1c7e1d82.html

To make this setup work as intended, I have added a declaration file named html.d.ts with the following content:

declare module '*.html' {
  const value: string;
  export default value
}

Here is the complete content of my tsconfig.json file:

{
    "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 seems correct, as my IDE recognizes the import statement above, and running tsc does not result in any errors, yielding the expected output (

require("../renderer/index.html")
).

However, when attempting to bundle with Parcel (parcel build main.ts), although the compilation itself works fine and the import gets replaced by

new URL("renderer.1c7e1d82.html", "file:" + __filename).toString();
, it fails to find the d.ts file, displaying the following warning:

@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 | 

According to the documentation, my .pretierrc is configured to use tsc:

{
  "extends": "@parcel/config-default",
  "transformers": {
    "*.{ts,tsx}": ["@parcel/transformer-typescript-tsc"]
  }
}

Despite this, it still does not consider the files or include parameters from tsconfig. All alternative approaches I have attempted (like removing files) lead to the same issue.

Answer №1

One way to inline parcel strings is by using the bundle-text: prefix.

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

When doing this, Parcel will automatically include the devDependency

@parcel/transformer-inline-string
.

For more information, visit .

It is recommended to create a definition file called global.d.ts with the following content:

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

Don't forget to add the line includes: ["**/*.ts"] to your tsconfig.json file.

EDIT:
Find out more about the tsconfig includes property at https://www.typescriptlang.org/tsconfig#include.
Remember to include the definition file as well using the pattern "**/*.ts".

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Implementing nested functions in Angular2 with TypeScript

Currently in the process of migrating from Angular 1 to 2, encountering a situation with nested functions in JavaScript: function normalizeDoc(doc, id) { function normalize(doc){... After removing the "function" keyword, TypeScript errors have starte ...

Issue with accessing 'email' property in Angular and Firebase because it is undefined

I've encountered an issue with fetching the current user logged into my application and displaying it in the header. I am using Angular 4 along with Firebase. Initially, I was able to display the user information successfully. However, after making s ...

`Browser Extension Compatibility``

I am currently working on developing a TypeScript extension that is compatible with all major browsers. I have come across the package https://www.npmjs.com/package/web-ext-types which I have integrated into my package.json file. While coding in TypeScrip ...

Typescript Error: TS2339: Unrecognized property 'map' on datatype 'string'

It appears that typescript does not currently support the new object types in ECMAScript 2015 (6th Edition, ECMA-262). Here is the code snippet I am working with: const SkipAny: string = requestBody.SkipAny; CurTester.SkipAny = SkipAny.map((value) => { ...

Ensuring Type Safety in Typescript: Service Fails to Return Expected Class Object

Can you help me with this issue? Here is the code for the user service: import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { User } from './user'; @Injectable() export clas ...

Generate personalized fields for Interface based on the Response Received

In my SPFX web part, the web service I am calling has properties that start with numbers: 30DaysTotal, 60DaysTotal, and 90DaysTotal. To handle this, I have defined an Interface as follows: export interface ISummary { Id : number; "30DaysGrand ...

Using TypeScript to generate a fresh object from a discriminated union sans the need for casting

Link to TS playground of the following Q Assume I have this listed type and object declarations: interface TypeA { id: string; type: 'A', fields: { thing1: string; thing2: number; } } const defaultA = { thing ...

Lack of MaterialUI Table props causing issues in Storybook

Currently, I am utilizing MaterialUI with some modifications to create a personalized library. My tool of choice for documentation is Storybook, using Typescript. An issue I have encountered is that the storybook table props are not consistently auto-gene ...

Ensure that any modifications made to an Angular service are reflected across all components that rely on that service

I am currently in the process of replicating a platform known as Kualitee.com, which serves as a test-management tool utilized by QA Engineers. Within Kualitee, users can access multiple projects, each containing various test cases and team members. The ab ...

Enhance Leaflet Marker functionality using Typescript

I am currently tackling a project that involves using Typescript and Leaflet. Traditionally, to extend the leaflet marker in JavaScript, it is done like this: L.Marker.Foo = L.Marker.extend({...}); But when I attempt to do this in Typescript, I encounter ...

Tips on how to retrieve an Observable Array instead of a subscription?

Is there a way to modify this forkJoin function so that it returns an observable array instead of a subscription? connect(): Observable<any[]> { this.userId = this.authService.userId; this.habits$ = this.habitService.fetchAllById(this.userId); this.s ...

Leveraging Mongo aggregation in Meteor applications

I have integrated the $lookup operator from MongoDB into my Meteor collection using the tunguska:reactive-aggregate package obtained from AtmosphereJs (). Below is an example of how I implemented it following the package documentation: import { ReactiveAgg ...

Converting an Observable variable to a specific type in Typescript

I am currently utilizing Angular 12. To avoid duplicating the same service logic, I am experimenting with creating a base class that includes all HTTP methods and then extending a child class to utilize in the components. crud.service.ts @Injectable({ ...

Exploring the world of interfaces in nested mapping functions

Currently, I'm faced with the challenge of mapping an array inside another map with an array. These are the specific interfaces that I am working with: interface Props { columns: Array<{field: string, headerName: string, width: number}>; row ...

Disabling the intellisense feature for locale suggestions in Monaco is recommended

Switch the keyboard language to a different one (in this case Japanese using alt + shift), and when typing in Monaco editor, an intellisense menu appears with options to remove and search. Monaco Editor Version: V0.33.0 https://i.stack.imgur.com/SIyeV.pn ...

I am searching for the vector geometric shapes svg elements that are commonly utilized in design editors. Can you point

When it comes to design editors, there are plenty of options such as Canva, Vistacreate, and Adobe Express. Each one uses a variety of styles for elements/objects/shapes. Are there any databases where I can find these resources? Some popular places include ...

What is the method for retrieving the second type of property from an array of objects?

I have a collection of objects that map other objects with unique identifiers (id) and names (name). My goal is to retrieve the name corresponding to a specific id. Here is my initial approach: const obj = { foo: { id: 1, name: 'one' }, ...

typescript: declaring types in a separate JavaScript file

Imagine you have a JavaScript library that exports some types for use (let's call it js1.js). You also have some TypeScript code sitting in a <script type="module"> tag that you want to use these types with (let's say ts1.ts). To make this ...

Angular styling and form error issue

Hey there! I'm new to Angular and facing a major issue along with a minor styling problem. Let's start with the big one: <mat-form-field appearance="fill"> <mat-label>Password</mat-label> <input matInput ...

Issue with TypeScript: "Cannot find name" error encountered during yarn build

I'm encountering an issue with my .tsx component code below: const Header = ({ dict: map, lang: string }) => { return ( <header className="flex items-center justify-between py-10"> <div> <Link href={`/${ ...