Skip to content

Commit e1db937

Browse files
author
unennhexium
committed
Support tsconfig without baseUrl property
Related to [this](simonhaenisch#18) issue
1 parent feb2435 commit e1db937

File tree

5 files changed

+125
-94
lines changed

5 files changed

+125
-94
lines changed

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

+96-87
Original file line numberDiff line numberDiff line change
@@ -6,115 +6,124 @@ 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

33-
try {
34-
// skips if module doesn't exist
35-
strictEqual(plugin.resolveId('foo/baz', ''), null);
36-
37-
// skips if a matching path alias isn't found
38-
strictEqual(plugin.resolveId('@asdf', ''), null);
39-
40-
// skips if importee is a virtual module
41-
strictEqual(plugin.resolveId('\0@foobar', ''), null);
42-
43-
// resolves with non-wildcard paths
44-
strictEqual(
45-
plugin.resolveId('@foobar', ''),
46-
join(__dirname, 'foo', 'bar.js'),
47-
);
48-
strictEqual(
49-
plugin.resolveId('@foobar-react', ''),
50-
join(__dirname, 'foo', 'bar-react.js'),
51-
);
52-
53-
// resolves with wildcard paths
54-
strictEqual(
55-
plugin.resolveId('@bar/foo', ''),
56-
join(__dirname, 'bar', 'foo.js'),
57-
);
58-
59-
// resolves $/* paths
60-
strictEqual(
61-
plugin.resolveId('$/foo/bar', ''),
62-
join(__dirname, 'foo', 'bar.js'),
63-
);
64-
65-
// resolves from a directory with index file
66-
strictEqual(plugin.resolveId('@js', ''), join(__dirname, 'js', 'index.js'));
67-
68-
// resolves without an `@` prefix
69-
strictEqual(
70-
plugin.resolveId('bar/foo', ''),
71-
join(__dirname, 'bar', 'foo.js'),
72-
);
73-
74-
// resolves with a different importer
75-
strictEqual(
76-
plugin.resolveId('bar/foo', join(__dirname, 'foo', 'bar.ts')),
77-
join(__dirname, 'bar', 'foo.js'),
78-
);
79-
80-
// doesn't accidentally resolve relative paths that also have an alias
81-
strictEqual(
82-
plugin.resolveId('../bar/foo', join(__dirname, 'foo', 'bar.ts')),
83-
null,
84-
);
85-
86-
// skips non-relative paths unless enabled
87-
strictEqual(plugin.resolveId('foo/bar', ''), null);
8837

89-
// resolves non-relative from baseUrl even if no path is matched
38+
// skips if module doesn't exist
39+
strictEqual(plugin.resolveId('foo/baz', ''), null);
40+
41+
// skips if a matching path alias isn't found
42+
strictEqual(plugin.resolveId('@asdf', ''), null);
43+
44+
// skips if importee is a virtual module
45+
strictEqual(plugin.resolveId('\0@foobar', ''), null);
46+
47+
// resolves with non-wildcard paths
48+
strictEqual(
49+
plugin.resolveId('@foobar', ''),
50+
join(__dirname, 'foo', 'bar.js'),
51+
);
52+
strictEqual(
53+
plugin.resolveId('@foobar-react', ''),
54+
join(__dirname, 'foo', 'bar-react.js'),
55+
);
56+
57+
// resolves with wildcard paths
58+
strictEqual(
59+
plugin.resolveId('@bar/foo', ''),
60+
join(__dirname, 'bar', 'foo.js'),
61+
);
62+
63+
// resolves $/* paths
64+
strictEqual(
65+
plugin.resolveId('$/foo/bar', ''),
66+
join(__dirname, 'foo', 'bar.js'),
67+
);
68+
69+
// resolves from a directory with index file
70+
strictEqual(plugin.resolveId('@js', ''), join(__dirname, 'js', 'index.js'));
71+
72+
// resolves without an `@` prefix
73+
strictEqual(
74+
plugin.resolveId('bar/foo', ''),
75+
join(__dirname, 'bar', 'foo.js'),
76+
);
77+
78+
// resolves with a different importer
79+
strictEqual(
80+
plugin.resolveId('bar/foo', join(__dirname, 'foo', 'bar.ts')),
81+
join(__dirname, 'bar', 'foo.js'),
82+
);
83+
84+
// doesn't accidentally resolve relative paths that also have an alias
85+
strictEqual(
86+
plugin.resolveId('../bar/foo', join(__dirname, 'foo', 'bar.ts')),
87+
null,
88+
);
89+
90+
// skips non-relative paths unless enabled
91+
strictEqual(plugin.resolveId('foo/bar', ''), null);
92+
93+
// resolves non-relative from baseUrl even if no path is matched, baseUrl is necessary in config
94+
if (!process.env.WO_BASEURL) {
9095
strictEqual(
9196
pluginNonRelative.resolveId('foo/bar', ''),
9297
join(__dirname, 'foo', 'bar.js'),
9398
);
94-
95-
// resolves as a relative path with option `absolute: false`
96-
strictEqual(
97-
pluginNonAbs.resolveId('@foobar', ''),
98-
join('test', 'foo', 'bar.js'),
99-
);
100-
101-
// applies function from `transform` option
102-
strictEqual(
103-
pluginTransform.resolveId('@foobar', ''),
104-
join(__dirname, 'foo', 'bar.cjs.js'),
105-
);
106-
107-
// resolves including the file extension with option `preserveExtensions: true`
108-
strictEqual(
109-
pluginPreserveExtensions.resolveId('@foobar', ''),
110-
join(__dirname, 'foo', 'bar.ts'),
111-
);
112-
strictEqual(
113-
pluginPreserveExtensions.resolveId('@foobar-react', ''),
114-
join(__dirname, 'foo', 'bar-react.tsx'),
115-
);
116-
117-
console.log('PASSED');
118-
} catch (error) {
119-
throw error;
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+
)
120105
}
106+
107+
// resolves as a relative path with option `absolute: false`
108+
strictEqual(
109+
pluginNonAbs.resolveId('@foobar', ''),
110+
join('test', 'foo', 'bar.js'),
111+
);
112+
113+
// applies function from `transform` option
114+
strictEqual(
115+
pluginTransform.resolveId('@foobar', ''),
116+
join(__dirname, 'foo', 'bar.cjs.js'),
117+
);
118+
119+
// resolves including the file extension with option `preserveExtensions: true`
120+
strictEqual(
121+
pluginPreserveExtensions.resolveId('@foobar', ''),
122+
join(__dirname, 'foo', 'bar.ts'),
123+
);
124+
strictEqual(
125+
pluginPreserveExtensions.resolveId('@foobar-react', ''),
126+
join(__dirname, 'foo', 'bar-react.tsx'),
127+
);
128+
129+
console.log('PASSED');

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)