Add support for absolute paths:

in imports:  (import * from "src/index.ts") instead od (import * from "../../../../../index.ts")
master
Kuba Orlik 1 year ago
parent 7c546e6314
commit f4659929ba

@ -1,14 +1,44 @@
const { build } = require("esbuild");
const glob = require("tiny-glob");
const { promises: fs } = require("node:fs");
const { extname, resolve, relative } = require("node:path");
const watch = process.argv.at(-1) === "--watch";
// plugin that rewrites the import paths so in typescript we can use
//
// import smth from src/index.ts
//
// instead of providing relative paths
const handle_absolute_paths = (project_dir) => ({
name: "sealgen-rewrite-asset-imports",
setup(build) {
build.onLoad({ filter: /\.tsx?$/ }, async (args) => {
let contents = await fs.readFile(args.path, "utf8");
const regex_import = new RegExp(
`^import ([^ ]+, )?(\\w+|{[^}]+}) from "([^"]+)";`,
"gm"
);
contents = contents.replaceAll(regex_import, (line) => {
const replaced = line.replace(
`"src/`,
`"` +
relative(args.path, resolve(project_dir, "./lib/src")) +
"/"
);
return replaced;
});
return {
contents,
loader: extname(args.path) === ".tsx" ? "tsx" : "ts",
};
});
},
});
(async () => {
let entryPoints = Object.fromEntries(
(await glob("./src/**/*.ts")).map((e) => [
e.replace(/\.ts$/, ""),
e,
])
(await glob("./src/**/*.ts")).map((e) => [e.replace(/\.ts$/, ""), e])
);
build({
entryPoints,
@ -19,5 +49,6 @@ const watch = process.argv.at(-1) === "--watch";
watch,
target: "node16",
format: "esm",
plugins: [handle_absolute_paths(__dirname)],
});
})();
})();

@ -1,4 +1,4 @@
import { Example } from "./index.js";
import { Example } from "src/index.js";
import * as assert from "assert";
describe("Example", () => {

@ -15,8 +15,11 @@
"declarationDir": "@types",
"resolveJsonModule": true,
"sourceMap": true,
"skipLibCheck": true
"skipLibCheck": true,
"paths": {
"src/*": ["./src/*"]
}
},
"include": ["src/**/*", ],
"include": ["src/**/*"],
"exclude": ["node_modules/**"]
}

Loading…
Cancel
Save