Skip to content

Commit 7d906f0

Browse files
committed
fix: resolve as js path instead of ts
1 parent e69aa32 commit 7d906f0

File tree

4 files changed

+38
-18
lines changed

4 files changed

+38
-18
lines changed

index.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import { join } from 'path';
12
import { CompilerOptions, findConfigFile, nodeModuleNameResolver, sys } from 'typescript';
23

3-
export const resolveTypescriptPaths = (options: Options = {}) => {
4-
const compilerOptions = getCompilerOptions(options.tsConfigPath);
4+
export const resolveTypescriptPaths = ({
5+
tsConfigPath = findConfigFile('./', sys.fileExists),
6+
absolute = true,
7+
}: Options = {}) => {
8+
const { compilerOptions, outDir } = getTsConfig(tsConfigPath);
59

610
return {
711
name: 'resolve-typescript-paths',
@@ -30,31 +34,45 @@ export const resolveTypescriptPaths = (options: Options = {}) => {
3034
return null;
3135
}
3236

33-
return resolvedFileName;
37+
const jsFileName = join(outDir, resolvedFileName.replace(/\.tsx?$/i, '.js'));
38+
39+
return absolute ? sys.resolvePath(jsFileName) : jsFileName;
3440
},
3541
};
3642
};
3743

38-
const getCompilerOptions = (configPath = findConfigFile('./', sys.fileExists)): CompilerOptions => {
44+
const getTsConfig = (configPath?: string): TsConfig => {
45+
const defaults: TsConfig = { compilerOptions: {}, outDir: '.' };
46+
3947
if (!configPath) {
40-
return {};
48+
return defaults;
4149
}
4250

4351
const configJson = sys.readFile(configPath);
4452

4553
if (!configJson) {
46-
return {};
54+
return defaults;
4755
}
4856

49-
const config: { compilerOptions?: CompilerOptions } = JSON.parse(configJson);
50-
51-
if (!config || !config.compilerOptions) {
52-
return {};
53-
}
57+
const config: Partial<TsConfig> = JSON.parse(configJson);
5458

55-
return config.compilerOptions;
59+
return { ...defaults, ...config };
5660
};
5761

5862
export interface Options {
63+
/**
64+
* Custom path to your `tsconfig.json`. Use this if the plugin can't seem to
65+
* find the correct one by itself.
66+
*/
5967
tsConfigPath?: string;
68+
69+
/**
70+
* Whether to resolve to absolute paths or not; defaults to `true`.
71+
*/
72+
absolute?: boolean;
73+
}
74+
75+
interface TsConfig {
76+
compilerOptions: CompilerOptions;
77+
outDir: string;
6078
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"homepage": "https://github.com/simonhaenisch/rollup-plugin-typescript-paths#readme",
2424
"devDependencies": {
25+
"@types/node": "12.0.3",
2526
"typescript": "^3.4.5"
2627
},
2728
"peerDependencies": {

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Factions-badge.atrox.dev%2Fsimonhaenisch%2Frollup-plugin-typescript-paths%2Fbadge&style=flat)](https://actions-badge.atrox.dev/simonhaenisch/rollup-plugin-typescript-paths/goto)
44

5-
Rollup Plugin to automatically resolve path aliases set in the `compilerOptions` section of `tsconfig.json`.
5+
Rollup Plugin to automatically resolve path aliases set in the `compilerOptions` section of `tsconfig.json`. It assumes that your Typescript code has already been transpiled before being rolled up (if that's not the case, you should probably use [rollup-plugin-typescript](https://github.com/rollup/rollup-plugin-typescript)).
66

77
For example, if you have
88

@@ -52,7 +52,8 @@ export default {
5252

5353
## Options
5454

55-
* **`tsConfigPath`:** If the plugin has trouble finding your `tsconfig.json`, you can pass the path to it via this option.
55+
* **`tsConfigPath`:** Custom path to your `tsconfig.json`. Use this if the plugin can't seem to find the correct one by itself.
56+
* **`absolute`:** Whether to resolve to absolute paths or not; defaults to `true`.
5657

5758
## License
5859

test/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
#!/usr/bin/env node
22

33
const { strictEqual } = require('assert');
4-
const { resolve } = require('path');
4+
const { resolve, join } = require('path');
55
const { resolveTypescriptPaths } = require('../dist');
66

77
const plugin = resolveTypescriptPaths({ tsConfigPath: resolve(__dirname, 'tsconfig.json') });
88

99
try {
1010
strictEqual(plugin.resolveId('@asdf', ''), null);
1111
strictEqual(plugin.resolveId('\0@foobar', ''), null);
12-
strictEqual(plugin.resolveId('@foobar', ''), 'test/foo/bar.ts');
13-
strictEqual(plugin.resolveId('@bar/foo', ''), 'test/bar/foo.ts');
14-
strictEqual(plugin.resolveId('@js', ''), 'test/js/index.js');
12+
strictEqual(plugin.resolveId('@foobar', ''), join(__dirname, 'foo', 'bar.js'));
13+
strictEqual(plugin.resolveId('@bar/foo', ''), join(__dirname, 'bar', 'foo.js'));
14+
strictEqual(plugin.resolveId('@js', ''), join(__dirname, 'js', 'index.js'));
1515

1616
console.log('PASSED');
1717
} catch (error) {

0 commit comments

Comments
 (0)