fix: Reorder extension resolution to prioritise TypeScript over JSON by PierreVanobbergen · Pull Request #1315 · vercel/ncc (original) (raw)

When a directory contains both test.json and test.ts, an extensionless import like:

import { myFunction } from './test';

resolves to test.json instead of test.ts, causing all exported methods from the TypeScript file to be unresolvable during the build.

Webpack resolves extensions in the order they appear in the resolve.extensions array.

Since .json appears before .ts and .tsx, webpack picks the JSON file first when both exist.

Reproduce

(The test project need a tsconfig.json with compilerOptions.resolveJsonModule: true)

  1. Create a directory with two files sharing the same base name:
    src/test.json
    src/test.ts
    export function greet(): string {
    return "hello";
    }
  2. Import without an extension from another TypeScript file:
    src/index.ts
    import { greet } from './test';
    console.log(greet());
  3. Run ncc build src/index.ts the build fails because ./test resolves to test.json, which has no greet export.