utilities for the replacement of imports in js files.
import { readFile } from "fs/promise";
import { replaceImport } from "@devserver/replace-import";
readFile("my.js", "utf8").then((code) => {
replaceImport({
code,
/**
* Resolve allows you to solve the import in a
* personalized way, being useful to point to
* a CDN or a local resource
* @param {string} module - Name of the module imported by the JS code
* @return {string|Promise<string>}
*/
async resolve(module) {
return module;
},
});
});
It allows to solve a pkg from NPM based on the export format of NodeJs#packages_self_referencing_a_package_using_its_name.
import { exportMap } from "@devserver/replace-import/export-map";
const folders = {
".": "./core.js",
"./html": "./html.js",
"./utils/*": "./u/*.js",
};
exportMap(folders, ""); //"./core.js"
It allows to solve the NPM modules through the reading of NPM packages
import { resolve } from "@devserver/replace-import/resolve";
(await resolve("atomico")).href; // file://...node_modules/atomico/core.js