Guide on transforming absolute imports into relative imports using Rollup.js?

Currently in the process of reorganizing my code and rewriting some TypeScript modules that are anticipated to be utilized by multiple microservices as packages.

Encountering an issue with absolute imports enabled by baseUrl in relation to this package conversion. Previously, I would

import { Thing } from "Place/That/I/Know";
where "Place/That/I/Know/" actually referenced
"src/Place/That/I/Know/"
. This change now triggers a Module not found error.

Considering utilizing Rollup.js to convert absolute imports, "Place/That/I/Know/", to relative imports "./Place/That/I/Know/". Came across a potential solution in this thread, but still seeking further guidance. Any recommendations?

Answer №1

When it comes to transforming existing "absolute" imports such as

import "/some/absolute/path.js"

You can utilize the eslint rule path/no-absolute-imports

#!/usr/bin/env bash

NPM=npm
#NPM=pnpm
#NPM=yarn

$NPM init

$NPM install eslint https://github.com/qdanik/eslint-plugin-path

cat >.eslintrc.cjs <<'EOF'
module.exports =
{
  "parserOptions": {
    "sourceType": "module",
    "ecmaVersion": "latest",
  },
  "plugins": ["path"],
  "rules": {
    "path/no-absolute-imports": "error"
  },
}
EOF

cat >jsconfig.json <<'EOF'
{
  "compilerOptions": {
    "baseUrl": "."
  }
}
EOF

cat >test.js <<'EOF'
import "/some/absolute/path.js"
EOF

npx eslint --fix .

cat test.js

This will yield the following output:

import "./some/absolute/path.js"

One common use case for this technique is in adjusting absolute import paths of a web application, ensuring compatibility with platforms like GitHub Pages that require relative import paths

Please note, however, that this method does not address paths related to service workers

navigator.serviceWorker.register("/workers/service.js", {
  type: "module",
  scope: "/app",
});

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

Utilizing getServerSideProps in the new app router (app/blah/page.tsx) for maximum functionality

I am a beginner in Next.js and I am currently experimenting with the new app router feature by placing my pages under app/.../page.tsx The code snippet provided works when using the page router (pages/blah.tsx) but encounters issues when used in app/blah/ ...

Obtain data from a single module and incorporate it into a different one

In my Angular 2 application, I am dealing with two component files - home.ts and folder-selector.ts. Within the folder-selector.ts file, there is a variable named pathToNode. I am trying to figure out how to access this value from within the home.ts file ...

Concealing a column within an Angular Material table

I'm currently faced with a challenge involving an Angular Material table containing numerous columns. My goal is to selectively hide certain columns based on specific CSS media queries. This is the code snippet I have experimented with so far: HTML: ...

Access network path through browser using Angular

I'm having trouble opening a network path from my browser using the code below. The browser keeps throwing an error saying it's unable to load local resources. Can you please provide some advice on this issue? public openUrl() { window.o ...

Steps to release a react application as an npm package

Hey there! I have a set of JavaScript files named auth.js, cookies.js, hooks.js, product.js, and index.js. My plan is to package them using npm for publishing. In my index.js file, I am exporting all the other files with the syntax: export * from './ ...

Is it possible for an Interface's property to be a type that contains an array?

As I dive into the Angular code, I encountered a peculiar type for a property within an Interface named 'Origin' that has left me perplexed. Here's the snippet: export interface Origin { areaNum?: number; open?: { [key: stri ...

Guide on setting up and configuring the seeder in MikroORM

Hey there, I recently tried to execute seeders in MikroORM and encountered a problem. I followed all the steps outlined here: . In the MikroORM route folder (alongside mikro-orm.config.ts), I created a seeders directory. I updated mikro-orm.ts with the fo ...

Having trouble getting Typescript code to function properly when using commonjs style require statements

I am completely new to Typescript, Node.js, and Express. Following the instructions outlined in this tutorial (https://www.digitalocean.com/community/tutorials/setting-up-a-node-project-with-typescript), I set up my project exactly as described there. The ...

Is it recommended for the main, module, and browser properties in package.json to reference the minified version or the source

When developing a JavaScript library, it is important to consider the structure in which it will be published. This typically includes various bundles such as CJS, ESM, and UMD, each with their own source, minified, and map files. my-package\ dist& ...

Leveraging command line arguments within an npm run script

I am currently working on modifying the {variable} section within my specifically-designed npm run deploy script "scripts":{ "deploy": "npm run build && scp -r ./public <a href="/cdn-cgi/l/email-protection" class=" ...

What is the best way to transfer a Blob image from Angular2 to NodeJs?

Encountering difficulties while attempting to upload a photo from the frontend. There is an input file where a file can be selected from the computer. The goal is to send that photo to the backend and store it as a Blob. Initially trying to retrieve the ...

Gulp seems to have lost track of its local whereabouts

Code snippet from gulpfile.js: var gulp = require('gulp'); var console = require('console'); gulp.task('default', function() { console.log('The "Gulp Default Ta ...

The system does not acknowledge 'CI' as a command that can be used internally or externally, functioning program, or batch file

Every time I attempt to execute npm run build in my React project, a persistent error keeps popping up: 'CI' is not recognized as an internal or external command, operable program or batch file. I have exhausted all troubleshooting steps - fro ...

Troubleshooting the error of an undefined RouterModule

Working on implementing lazy loading for my Angular 4 application, I have a total of 18 lazy loaded modules. Upon noticing that fetching these modules is taking some time, I decided to add a loading indicator. Everything worked fine when I added it locally ...

Angular index.html file can include a conditional script

I am currently working on an Angular project, where the index.html serves as the main entry point for the application, just like in any other Angular project. This file contains important links and configurations. Within the HTML code snippet below, you w ...

Is there a way to simulate Pinia and vue-i18n simultaneously?

Exploring Vue 3's Composition API with a twist: The store.ts file import { ref, Ref } from 'vue'; import { defineStore } from 'pinia'; export const useStore = defineStore('store', () => { const isLoading: Ref<bo ...

What is the process for assigning a serial number to each row in the MUI DataGrid?

Initially, the server is accessed to retrieve some data. After that, additional data is added. While the data does not contain an ID, the form must still display a serial number. const columns: GridColDef[] = [ { field: 'id' ...

adding new data rows to an existing data list using Angular

I am currently working on displaying data from a backend system. The data is coming from a table that receives new rows within a specific time frame. To retrieve these new rows, I have set up a mechanism using an Angular timer. My query pertains to the tem ...

Display the initial x list items without utilizing ngFor in Angular 2

In the template, there are 9 <li> elements, each with a *ngIf condition present. It is possible that 5 or more of them may return true, but the requirement is to only display the first 4 or less if needed. Priority is given to the order of the < ...

Utilize SWR in NextJS to efficiently manage API redirection duplication

When using SWR to fetch data, I encountered an error where the default path of nextjs was repeated: http://localhost:3000/127.0.0.1:8000/api/posts/get-five-post-popular?skip=0&limit=5 Here is my tsx code: 'use client' import useSWR from &quo ...