Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
calcLongestCommonPath,
checkMFPlugin,
getAbsolutePath,
isDirectory,
isEmptyObject,
isIntermediateOutputFormat,
isObject,
Expand Down Expand Up @@ -1436,6 +1437,19 @@ const composeBundlelessExternalConfig = (
} else {
// 1. js files hit JS_EXTENSIONS_PATTERN,./foo ->./foo.mjs
if (jsRedirectExtension) {
// If the import path refers to a directory,
// it most likely actually refers to a `index.*` file due to Node's module resolution.
// When redirect.js.path is set to false, index should still be added before adding extension.
// When redirect.js.path is true, the resolver directly generate correct resolvedRequest with index appended.
if (
!jsRedirectPath &&
(await isDirectory(
join(dirname(issuer), resolvedRequest),
))
) {
// This uses `/` instead of `path.join` here because `join` removes potential "./" prefixes
resolvedRequest = `${resolvedRequest.replace(/\/+$/, '')}/index`;
}
resolvedRequest = `${resolvedRequest}${jsExtension}`;
}
}
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,12 @@ const windowsSlashRegex = /\\/g;
export function normalizeSlash(p: string): string {
return p.replace(windowsSlashRegex, '/');
}

export async function isDirectory(filePath: string): Promise<boolean> {
try {
const stat = await fsP.stat(filePath);
return stat.isDirectory();
} catch {
return false;
}
}
6 changes: 3 additions & 3 deletions tests/integration/redirect/js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ test('redirect.js.path false', async () => {
import { bar } from "@/bar";
import { foo } from "@/foo";
import { baz } from "~/baz";
import { bar as external_bar_js_bar } from "./bar.js";
import { bar as index_js_bar } from "./bar/index.js";
import { foo as external_foo_js_foo } from "./foo.js";
export * from "./.hidden.js";
export * from "./.hidden-folder.js";
const src = lodash.toUpper(lodash_merge(external_foo_js_foo) + external_bar_js_bar + foo + bar + baz + typeof prettier.version);
export * from "./.hidden-folder/index.js";
const src = lodash.toUpper(lodash_merge(external_foo_js_foo) + index_js_bar + foo + bar + baz + typeof prettier.version);
export { src as default };
"
`);
Expand Down
Loading