Skip to content

Commit eac1d59

Browse files
committed
feat: add transform hook
1 parent 7d906f0 commit eac1d59

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { CompilerOptions, findConfigFile, nodeModuleNameResolver, sys } from 'ty
44
export const resolveTypescriptPaths = ({
55
tsConfigPath = findConfigFile('./', sys.fileExists),
66
absolute = true,
7+
transform,
78
}: Options = {}) => {
89
const { compilerOptions, outDir } = getTsConfig(tsConfigPath);
910

@@ -36,7 +37,13 @@ export const resolveTypescriptPaths = ({
3637

3738
const jsFileName = join(outDir, resolvedFileName.replace(/\.tsx?$/i, '.js'));
3839

39-
return absolute ? sys.resolvePath(jsFileName) : jsFileName;
40+
let resolved = absolute ? sys.resolvePath(jsFileName) : jsFileName;
41+
42+
if (transform) {
43+
resolved = transform(resolved);
44+
}
45+
46+
return resolved;
4047
},
4148
};
4249
};
@@ -70,6 +77,12 @@ export interface Options {
7077
* Whether to resolve to absolute paths or not; defaults to `true`.
7178
*/
7279
absolute?: boolean;
80+
81+
/**
82+
* If the plugin successfully resolves a path, this function allows you to
83+
* hook into the process and transform that path before it is returned.
84+
*/
85+
transform?(path: string): string;
7386
}
7487

7588
interface TsConfig {

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export default {
5454

5555
* **`tsConfigPath`:** Custom path to your `tsconfig.json`. Use this if the plugin can't seem to find the correct one by itself.
5656
* **`absolute`:** Whether to resolve to absolute paths or not; defaults to `true`.
57+
* **`transform`:** If the plugin successfully resolves a path, this function allows you to hook into the process and transform that path before it is returned.
5758

5859
## License
5960

test/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ const { strictEqual } = require('assert');
44
const { resolve, join } = require('path');
55
const { resolveTypescriptPaths } = require('../dist');
66

7+
const transform = path => path.replace(/\.js$/i, '.cjs.js');
8+
79
const plugin = resolveTypescriptPaths({ tsConfigPath: resolve(__dirname, 'tsconfig.json') });
10+
const pluginNonAbs = resolveTypescriptPaths({ tsConfigPath: resolve(__dirname, 'tsconfig.json'), absolute: false });
11+
const pluginTransform = resolveTypescriptPaths({ tsConfigPath: resolve(__dirname, 'tsconfig.json'), transform });
812

913
try {
1014
strictEqual(plugin.resolveId('@asdf', ''), null);
@@ -13,6 +17,9 @@ try {
1317
strictEqual(plugin.resolveId('@bar/foo', ''), join(__dirname, 'bar', 'foo.js'));
1418
strictEqual(plugin.resolveId('@js', ''), join(__dirname, 'js', 'index.js'));
1519

20+
strictEqual(pluginNonAbs.resolveId('@foobar', ''), join('test', 'foo', 'bar.js'));
21+
22+
strictEqual(pluginTransform.resolveId('@foobar', ''), join(__dirname, 'foo', 'bar.cjs.js'));
1623
console.log('PASSED');
1724
} catch (error) {
1825
throw error;

0 commit comments

Comments
 (0)