Skip to content

Commit c8c1133

Browse files
committed
Resolve extensions on relative imports for webpack
Configure babel-plugin-module-resolver to resolve relative import paths from TypeScript source files of the workspace packages to the TypeScript source file that corresponds to the import path.
1 parent c65a41f commit c8c1133

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

babel.config.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
*/
2222

2323
const path = require('path');
24+
const { resolvePath } = require('babel-plugin-module-resolver');
25+
26+
const packagePath = path.join(__dirname, 'packages');
2427

2528
module.exports = (api) => {
2629
const ENV = api.env();
@@ -47,14 +50,25 @@ module.exports = (api) => {
4750
// Options for the module-resolver plugin.
4851
// Used for resolving source files during development.
4952
const resolverOptions = {
50-
alias: {
51-
...(DEV || TEST
52-
? {
53+
...(DEV || TEST
54+
? {
55+
alias: {
5356
'^@apache-annotator/([^/]+)$': ([, name]) =>
54-
path.join(__dirname, 'packages', name, '/src/index.ts'),
55-
}
56-
: null),
57-
},
57+
path.join(packagePath, name, '/src/index.ts'),
58+
},
59+
resolvePath(sourcePath, currentFile, opts) {
60+
if (
61+
currentFile.startsWith(packagePath) &&
62+
currentFile.endsWith('.ts') &&
63+
sourcePath.startsWith('.') &&
64+
sourcePath.endsWith('.js')
65+
) {
66+
return sourcePath.replace(/\.js$/, '.ts');
67+
}
68+
return resolvePath(sourcePath, currentFile, opts);
69+
},
70+
}
71+
: null),
5872
};
5973

6074
// Options for the @babel/transform-runtime plugin.

0 commit comments

Comments
 (0)