I'd like to append .js extension to all import statements where the file path to be imported starts with a .. For example:
import { LocalConf } from "./database/LocalConf";
import * as mariadb from "mariadb";
import { Errors } from "./util/Errors";
gets transformed to
import { LocalConf } from "./database/LocalConf.js";
import * as mariadb from "mariadb";
import { Errors } from "./util/Errors.js";
This is the js command I am running:
let newData = data.replace(/(import .*? from\s+['"])(\..*?[^\.js])(?=['"])/g, '$1$2.js')
Unfortunately [^\.js] matches a single character, and not a .js string. because of that,
import { Errors } from "./util/Errors";
never gets .js appeneded.
How can I match .js string instead of just single characters?
Could you do something like this?
function addExtension(path) {
if (path.contains('from "./')) {
return path + ".js";
}
return path;
}