Skip to content

Instantly share code, notes, and snippets.

@UberMouse
Created September 29, 2022 21:02
Show Gist options
  • Save UberMouse/edf8bede31c3a104b136fc0a47737d61 to your computer and use it in GitHub Desktop.
Save UberMouse/edf8bede31c3a104b136fc0a47737d61 to your computer and use it in GitHub Desktop.
const { promisify } = require("util");
const { exec: _exec } = require("child_process");
const { resolve } = require("path");
const exec = promisify(_exec);
const PLUGIN_NAME = "FixTsconfigPathsPlugin";
class FixTsconfigPathsPlugin {
pluginName = PLUGIN_NAME;
apply(heftSession, heftConfiguration) {
const logger = heftSession.requestScopedLogger("fix-tsconfig-paths");
heftSession.hooks.build.tap(PLUGIN_NAME, (build) => {
build.hooks.compile.tap(PLUGIN_NAME, (compile) => {
compile.hooks.afterCompile.tapPromise(PLUGIN_NAME, async () => {
const fixerBin = resolve(__dirname, "node_modules", ".bin", "tsconfig-replace-paths");
const tsconfigPath = resolve(heftConfiguration.buildFolder, "tsconfig.json");
const args = [
"--project", tsconfigPath,
"--out", resolve(heftConfiguration.buildFolder, "dist"),
"--src", resolve(heftConfiguration.buildFolder, "src")
];
const tsconfig = require(tsconfigPath);
if (tsconfig && tsconfig.compilerOptions && tsconfig.compilerOptions.paths) {
await exec(`${fixerBin} ${args.join(" ")}`, {
cwd: __dirname,
});
args[3] = resolve(heftConfiguration.buildFolder, "dist-cjs");
await exec(`${fixerBin} ${args.join(" ")}`, {
cwd: __dirname,
});
}
});
});
});
}
}
module.exports = new FixTsconfigPathsPlugin();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment