`Using Rollup to combine local typescript files into a single main bundle`

I'm currently working on developing an npm module that is intended for use in web browsers.

For this project, I have chosen to utilize TypeScript and Rollup as my tools of choice.

Here is a snippet of my tsconfig.json:

{
  "compilerOptions": {
    "module": "CommonJS",
    "outDir": "lib",
    "strict": true,
    "rootDir": "src"
  }
}

Additionally, here is a glimpse at my rollup.config.js:

import typescript from "@rollup/plugin-typescript";

export default {
  input: "src/index.ts",
  output: {
    dir: "lib",
    format: "iife",
  },
  plugins: [typescript()],
};

Within the file src/index.ts, you can find the following code:

// src/index.ts
import log from './log'

const myFn = () => {
 ...myFn code
}

The issue I am facing is evident in the bundled code snippet below:

// lib/index.js bundle
var log_1 = require("./log");

My goal is to include the contents of the log file directly within the main lib/index.js file during bundling.

Is there a way to achieve this using TypeScript and Rollup?

It's worth mentioning that I previously attempted to use outFile (referencing the TypeScript documentation), but it seems that this feature is not supported by the "@rollup/plugin-typescript".

Could the solution involve manually compiling with tsc before proceeding with the Rollup bundling?

Answer №1

To effectively bundle your code using Rollup, the TypeScript compiler must be set up to generate ES Modules. Make sure you update the module format to "ESNext" instead of "CommonJS" in your tsconfig.json file or adjust the configuration for @rollup/plugin-typescript in your Rollup setup:

typescript({ module: "ESNext" })

It's important to note that starting from rollup/plugins#788, a warning will be issued if this configuration is not correctly set.

@rollup/plugin-typescript: Rollup specifically requires ES Modules output from TypeScript. If your configuration specifies a different "module" setting other than "esnext", it is recommended to change it to "esnext" in the target tsconfig.json file or plugin options unless you have specific knowledge about what you are doing.

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

Transform coordinated universal time to the corresponding local time using Angular

Looking to transform the UTC format date into an India date and time format using Angular 2019-02-18T17:31:19-05:00 I am looking for it to be in the following format: DD/MM/YYYY HH:MM(eg: 02/19/2019 04:01 AM). Any guidance on how to accomplish this would ...

The function of hasClass within an if statement appears to be malfunctioning

I'm struggling to figure out why my .hasClass function is not functioning correctly. I've implemented the Quick Search jQuery plugin, and below is the code I am using: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.mi ...

Looking to deactivate the entire keyboard with JavaScript? Make sure that the start key is not disabled, not even Ctrl

Despite my efforts to disable the entire keyboard using JavaScript, I have encountered some limitations. The Windows Start key and Enter key are not being disabled by my script. <script type='text/javascript'> document.onkeydown = functi ...

Bring to the front the div altered by a CSS3 animation

Recently, I encountered an issue with a div card that triggers an animation when clicked. The animation involves the card scaling up larger, but the problem arises as its edges get hidden under other cards. To visualize this, take a look at the screenshot ...

Component Missing in Vue.JS Display

I'm facing a challenge with implementing Vue Material on a Vue Router Dashboard page while attempting to store the panel in a separate file. Despite spending the last 2 hours researching this issue, I haven't been able to find a solution. The Vue ...

Obtaining the "match" object within a Custom Filter Selector on jQuery version 1.8

Here's a helpful resource on Creating a Custom Filter Selector with jQuery for your reference. A Quick Overview: If you're unfamiliar with jQuery's Custom Filter Selectors, they allow you to extend jQuery’s selector expressions by addi ...

Attempting to dynamically alter the background color of a sticky header once a specific class reaches the top of the viewport

I am in need of a solution to alter the background color of a simple sticky header whenever a div/class/id reaches the top of the window, and revert back when scrolled up. This change should occur multiple times. The base code for the sticky header can be ...

Is it possible for a three.js material to have different repeat values for a bump map compared to a texture map?

I'm attempting to reduce the repetitive nature of my texture by implementing a bump map that repeats less frequently. However, it appears to adopt the repeat value of 'landTexture' (64) below instead of the value I assigned it (1). landText ...

Maintain cookie data between pages to remember form inputs

The scenario: A form is utilized to capture the input value using $thisSearch.val(), store it as a cookie, and then add it to an array. This form is displayed as an overlay triggered from a menu item in the website header, allowing it to be accessed from a ...

Enhanced data visualization with Material UI's nested datagrid feature

Is there a way to display nested JSON data on a React Material UI data grid? I'm looking to showcase the phone numbers of users from the JSON in the provided sandbox example. You can check out the demo here. ...

Although VSCode is functioning properly, there seems to be a discrepancy between the VSCode and the TS compiler, resulting in an

While developing my project in VSCode, I encountered an issue with accessing req.user.name from the Request object for my Node.js Express application. VSCode was indicating that 'name' does not exist in the 'user' object. To address thi ...

Utilizing asynchronous operations dependent on the status of a separate entity

Dealing with asynchronous operations in Vue has been a challenge for me. Coming from a C# background, I find the async-await pattern more intuitive than in JavaScript or TypeScript, especially when working with Vue. I have two components set up without us ...

Is it detrimental to have lengthy jQuery chains?

After extensively utilizing jQuery for quite some time, I recently developed a slideshow plugin for my professional projects. Interestingly, without fully intending to, approximately 75% of the code was written in a single chain. Despite being meticulous ...

Using React JS to automatically execute an event based on a specific state value

Is there a way to initiate an action from a main component when the child component's state reaches a specific value? Let's consider a scenario where I have a Parent component and a Child component, with the parent's state containing active ...

Steps for adding a variable to a JSON request

I'm new to JavaScript and React Native and I'm looking for a way to include variables in a JSON request. Specifically, when making a JSON request using Fetch or Axios to a URL, I need to customize the URL based on certain parameters. For instance ...

Rails Ajax issue encountered

I previously followed a coderwall tutorial on ajax but encountered an ActionController::UnknownFormat error when attempting to open the link in a new tab (Link Name). Can anyone help me figure out what's wrong? Thanks! Additionally, clicking the link ...

The tooltip indicator fails to display

I'm having an issue with my tooltip. I would like my tooltip to look like this: However, the actual result is like this: Here is the HTML & jQuery code I am using: <td style="padding-left:5px;padding-right:5px;" align="left" data-toggle="to ...

Why does variable passing use 'object Text' instead of passing values?

In my for loop, I am dynamically creating a table with radio buttons and trying to create labels dynamically as well. However, when I pass the variable to the label text node, it prints out 'object Text' on the page instead of the expected value. ...

Ways to transfer data between components in Angular 2: Passing variables between children

Within my application, I have a parent component (P) and two children components (C1) and (C2). Both C1 and C2 are children of the parent component P, but they are not nested. In the parent template, it is structured as follows: <c1-component></c ...

Expressions without a call signature cannot be invoked

When using an adapter in the given example, I encountered a type error specifically on the last line of the getGloryOfAnimal method. Despite having clearly defined types, I am puzzled by this issue. interface ICheetah { pace: string; } interface ILio ...