File tree Expand file tree Collapse file tree 3 files changed +26
-3
lines changed
tests/integration/redirect Expand file tree Collapse file tree 3 files changed +26
-3
lines changed Original file line number Diff line number Diff line change @@ -63,6 +63,7 @@ import {
63
63
calcLongestCommonPath ,
64
64
checkMFPlugin ,
65
65
getAbsolutePath ,
66
+ isDirectory ,
66
67
isEmptyObject ,
67
68
isIntermediateOutputFormat ,
68
69
isObject ,
@@ -1436,6 +1437,19 @@ const composeBundlelessExternalConfig = (
1436
1437
} else {
1437
1438
// 1. js files hit JS_EXTENSIONS_PATTERN,./foo ->./foo.mjs
1438
1439
if ( jsRedirectExtension ) {
1440
+ // If the import path refers to a directory,
1441
+ // it most likely actually refers to a `index.*` file due to Node's module resolution.
1442
+ // When redirect.js.path is set to false, index should still be added before adding extension.
1443
+ // When redirect.js.path is true, the resolver directly generate correct resolvedRequest with index appended.
1444
+ if (
1445
+ ! jsRedirectPath &&
1446
+ ( await isDirectory (
1447
+ join ( dirname ( issuer ) , resolvedRequest ) ,
1448
+ ) )
1449
+ ) {
1450
+ // This uses `/` instead of `path.join` here because `join` removes potential "./" prefixes
1451
+ resolvedRequest = `${ resolvedRequest . replace ( / \/ + $ / , '' ) } /index` ;
1452
+ }
1439
1453
resolvedRequest = `${ resolvedRequest } ${ jsExtension } ` ;
1440
1454
}
1441
1455
}
Original file line number Diff line number Diff line change @@ -246,3 +246,12 @@ const windowsSlashRegex = /\\/g;
246
246
export function normalizeSlash ( p : string ) : string {
247
247
return p . replace ( windowsSlashRegex , '/' ) ;
248
248
}
249
+
250
+ export async function isDirectory ( filePath : string ) : Promise < boolean > {
251
+ try {
252
+ const stat = await fsP . stat ( filePath ) ;
253
+ return stat . isDirectory ( ) ;
254
+ } catch {
255
+ return false ;
256
+ }
257
+ }
Original file line number Diff line number Diff line change @@ -53,11 +53,11 @@ test('redirect.js.path false', async () => {
53
53
import { bar } from "@/bar";
54
54
import { foo } from "@/foo";
55
55
import { baz } from "~/baz";
56
- import { bar as external_bar_js_bar } from "./bar.js";
56
+ import { bar as index_js_bar } from "./bar/index .js";
57
57
import { foo as external_foo_js_foo } from "./foo.js";
58
58
export * from "./.hidden.js";
59
- export * from "./.hidden-folder.js";
60
- const src = lodash.toUpper(lodash_merge(external_foo_js_foo) + external_bar_js_bar + foo + bar + baz + typeof prettier.version);
59
+ export * from "./.hidden-folder/index .js";
60
+ const src = lodash.toUpper(lodash_merge(external_foo_js_foo) + index_js_bar + foo + bar + baz + typeof prettier.version);
61
61
export { src as default };
62
62
"
63
63
` ) ;
You can’t perform that action at this time.
0 commit comments