Enhancing the appearance of TypeScript and React with custom styling using the NPM package

I'm currently working on a TypeScript (TS), React project where I have organized all my React components. To make things easier, I decided to create an NPM package out of this project and use it in various separate React projects. However, I've encountered an issue when trying to export the styling along with the TypeScript components.

When using components from the NPM package, I'm getting errors such as:

Module not found: Can't resolve '../css/footer.css'

After creating the packaging directory (dist) using tsc, all my components are present but the CSS files are missing. Both my components and CSS are located within the same src folder.

Below is a snippet of my tsconfig.json file:

{
  "compilerOptions": {
    "outDir": "dist",
    "target": "esnext",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": false,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

Should I consider moving the styling from CSS files to TypeScript files that utilize the <style jsx> tag, or is there a different approach to ensure that the NPM package includes both styling and components seamlessly?

Answer №1

tsc is the command that converts TypeScript files into Javascript. Unlike TypeScript files, CSS files are not included in this conversion process.

If you wish to incorporate CSS (or any other asset types) and have them compiled with your other files, consider using a bundler such as Webpack or Rollup. Tools like Microbundle offer convenient bundler configurations, allowing you to concentrate on your library development.

Alternatively, you can write your styles in TypeScript using a library like styled-jsx.

Even if you do not plan on using these tools, it is beneficial to explore the templates available for TypeScript + React npm packages to understand their functioning.

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

CSS First Element

"When an element with a specific id has a first-child descendant that is any of the heading tags (h1, h2, h3, h4, h5, or h6), apply the following CSS styles:" I have come up with the following selector: #content:first-child h1, #content:first-child h2, ...

Sending information from the parent component to the child Bootstrap Modal in Angular 6

As a newcomer to Angular 6, I am facing challenges with passing data between components. I am trying to launch a child component bootstrap modal from the parent modal and need to pass a string parameter to the child modal component. Additionally, I want t ...

Aligning content within an <a> container

I am in the process of creating a square box that is clickable and will redirect users to another page. To achieve this functionality, as well as implement hover effects later on, I am utilizing the < a > element. Below is the HTML markup: <a id ...

Choose a country from a modal in a React application

click here for image description I need help implementing the screenshot above. My goal is to change the default country name and flag when a user clicks on the country dropdown menu. I have been using headless UI so far, but I'm struggling to figure ...

Is there a way to get rid of the "bouncing effect" on my button while using an inline floating-label text input?

When an input with a floating label (Bootstrap 5) is inline with other elements, the elements may appear to jump up and down depending on the position of the floating label. https://i.sstatic.net/kDxBo.gif <link href="https://cdn.jsdelivr.net/npm/bo ...

Personalizing the design of Bootstrap

As a beginner in working with Bootstrap, I am currently focused on creating an index html page for an online shop. For reference, here is an example - https://jsfiddle.net/a393wtng/1/ Upon resizing the output frame, it becomes apparent that 4 products can ...

Slider - incorporating a heading onto a video with HTML styling

Is there a way to display a title on a slider when one of the slides contains a video? Here is an example code snippet: <!-- Swiper--> <div data-height="100vh" data-min-height="480px" data-slide-effect="fade" class="swiper-container swiper-s ...

Running the gulp uncss command with regex to ignore specific elements is not functioning as expected

I have been attempting to integrate uncss into my gulp workflow. In order to exclude certain classes, such as those added through javascript, I am specifying these classes with "ignore" (specifically, I am trying to remove the css from the jquery plugin m ...

Steps to resolve the Angular observable error

I am trying to remove the currently logged-in user using a filter method, but I encountered an error: Type 'Subscription' is missing the following properties from type 'Observable[]>': _isScalar, source, operator, lift, and 6 more ...

Using Angular to Bind Checkbox Value in Typescript

I have a challenge of creating a quiz where a card is displayed with 4 questions structured like this: <div class="col-md-6"> <div class="option" id="Answer1"> <label class="Answer1"> <input value= "Answer1" type="checkbox ...

Guide on reading a request body in the next13 app router

the get route handler provided: export async function GET(request: Request) { // logs null console.log(request.body); return Response.json({ msg:'success' }) } despite including body data in the request, the logge ...

What is the best way to transform a JavaScript object into an array?

Here is the information I have: {product_quantity: {quantity: 13, code: "AAA", warehouse: "1000",}} The product_quantity field is part of a JSON object in a MongoDB database. I am looking to convert it into this format: {"produ ...

React error due to setting div scroll height on component mount

In my code, there is a special div/container that contains multiple <p/> tags. The container has a reference called divRef, which is initially set to null using the useRef function. <div ref={divRef} style={styles.messageListContainer}> ...

Having trouble uploading multipart data in a React app with axios

Hey there! I'm facing a challenge with posting multipart data in my React app using Axios. Unfortunately, it's not getting posted and I receive an error after waiting for more than 5 minutes: "(failed) net:::err_CONNECTION_RESET". I'm unsure ...

CSS: positioning and resizing image while maintaining aspect ratio

Is there a way to center and resize an image in html/css while maintaining its aspect ratio? I currently have a solution that centers the image, but it does not scale up when necessary. Here is the code snippet: http://jsfiddle.net/4Mvan/438/ .container ...

Utilize SVG background with transparent text for a clear view of surrounding content

I'm looking for a way to overlay a video with a semi-transparent black div that includes text and a button, while still allowing users to see the video playing behind it. I previously achieved this using a PNG image but now need to apply the effect to ...

What could be causing the issues with SSL certificates when using Node.js/Express-TypeScript?

I'm currently in the process of transitioning a project's backend from JavaScript (Node.js/Express) to TypeScript. However, I've encountered an unusual issue where FS's readFileSync is unable to access the key.pem or cert.pem files in t ...

Observing the completion of a subscriber function

Is there a more streamlined way to determine if the subscriber has finished executing or return something and catch it up-stream? Consider the following code snippets: this._subscriptions.push(this._client .getCommandStream(this._command) // R ...

The footer should never be positioned below the sidebar

Hello, I'm trying to position my footer below my side navigation bar. It's working fine with the header, but I can't seem to figure out how to do it for the footer. Also, another question - how can I ensure that the text on a smaller screen ...

Solving the issue where the argument type is not assignable to the parameter type

I am attempting to filter an array of objects in React using TypeScript and encountered this error. Below is my interface, state, and function: TS2345: Argument of type '(prev: IBudget, current: IBudget) => IBudget | undefined' is not assigna ...