Skip to content

Commit 58e13d9

Browse files
author
unennhexium
committed
Support tsconfig without baseUrl property
Related issue: #18
1 parent feb2435 commit 58e13d9

File tree

6 files changed

+52
-17
lines changed

6 files changed

+52
-17
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.vscode/settings.json
2+
.idea
23
node_modules
34
package-lock.json
45
dist

index.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join } from 'path';
1+
import { join, relative } from 'path';
22
import { Plugin } from 'rollup';
33
import {
44
CompilerOptions,
@@ -65,12 +65,19 @@ export const typescriptPaths = ({
6565
return null;
6666
}
6767

68-
const targetFileName = join(
69-
outDir,
70-
preserveExtensions
71-
? resolvedFileName
72-
: resolvedFileName.replace(/\.tsx?$/i, '.js'),
73-
);
68+
const processedFileName = preserveExtensions
69+
? resolvedFileName
70+
: resolvedFileName.replace(/\.tsx?$/i, '.js');
71+
72+
/* Do not use:
73+
* - path.dirname(tsConfigPath) -> using abs. path to <proj_root>/test/<tsconfig>
74+
* - __dirname -> using abs. path to compiled plugin files <proj_root>/dist
75+
* - process.env.PWD -> non cross-platform
76+
* instead of process.cwd() -> using abs. path to <proj_root>/<tsconfig>, that is correct
77+
*/
78+
const targetFileName = typeof compilerOptions.baseUrl === 'undefined'
79+
? join(outDir, relative(process.cwd(), processedFileName))
80+
: join(outDir, processedFileName);
7481

7582
const resolved = absolute
7683
? sys.resolvePath(targetFileName)

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"dist"
99
],
1010
"scripts": {
11+
"test:wo-baseurl": "WO_BASEURL=true npm run test",
1112
"test": "npm run prepare && node test",
1213
"preversion": "npm test",
1314
"prepare": "rm -rf dist && tsc"

test/index.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,31 @@ const typescriptPaths = require('../dist').default;
66

77
const transform = (path) => path.replace(/\.js$/i, '.cjs.js');
88

9+
const tsConfigPath = process.env.WO_BASEURL
10+
? 'tsconfig-wo-baseurl.json'
11+
: 'tsconfig.json'
12+
913
const plugin = typescriptPaths({
10-
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
14+
tsConfigPath: resolve(__dirname, tsConfigPath),
1115
});
1216

1317
const pluginNonAbs = typescriptPaths({
14-
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
18+
tsConfigPath: resolve(__dirname, tsConfigPath),
1519
absolute: false,
1620
});
1721

1822
const pluginNonRelative = typescriptPaths({
19-
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
23+
tsConfigPath: resolve(__dirname, tsConfigPath),
2024
nonRelative: true,
2125
});
2226

2327
const pluginTransform = typescriptPaths({
24-
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
28+
tsConfigPath: resolve(__dirname, tsConfigPath),
2529
transform,
2630
});
2731

2832
const pluginPreserveExtensions = typescriptPaths({
29-
tsConfigPath: resolve(__dirname, 'tsconfig.json'),
33+
tsConfigPath: resolve(__dirname, tsConfigPath),
3034
preserveExtensions: true,
3135
});
3236

@@ -86,11 +90,19 @@ try {
8690
// skips non-relative paths unless enabled
8791
strictEqual(plugin.resolveId('foo/bar', ''), null);
8892

89-
// resolves non-relative from baseUrl even if no path is matched
90-
strictEqual(
91-
pluginNonRelative.resolveId('foo/bar', ''),
92-
join(__dirname, 'foo', 'bar.js'),
93-
);
93+
// resolves non-relative from baseUrl even if no path is matched, baseUrl is necessary in config
94+
if (!process.env.WO_BASEURL) {
95+
strictEqual(
96+
pluginNonRelative.resolveId('foo/bar', ''),
97+
join(__dirname, 'foo', 'bar.js'),
98+
);
99+
} else {
100+
console.log(
101+
'SKIP test forced nonRelative paths:\n',
102+
'- Irrelevant since baseUrl is not provided in this case\n',
103+
'- See WO_BASEURL environment variable in the test code'
104+
)
105+
}
94106

95107
// resolves as a relative path with option `absolute: false`
96108
strictEqual(

test/tsconfig-wo-baseurl.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
// comments should work
3+
"compilerOptions": {
4+
"paths": {
5+
"@foobar": ["test/foo/bar"],
6+
"@foobar-react": ["test/foo/bar-react"],
7+
"@bar/*": ["test/bar/*"],
8+
"bar/*": ["test/bar/*"],
9+
"@js": ["test/js"],
10+
"$/*": ["test/*"]
11+
}
12+
}
13+
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"lib": ["esnext"],
66
"declaration": true,
77
"outDir": "dist",
8+
"sourceMap": true,
89

910
"strict": true,
1011
"noUnusedLocals": true,

0 commit comments

Comments
 (0)