nodejs / import-in-the-middle

Like `require-in-the-middle`, but for ESM import
https://www.npmjs.com/package/import-in-the-middle
Apache License 2.0
52 stars 20 forks source link

Does not handle `import *` of current file #82

Closed timfish closed 1 month ago

timfish commented 1 month ago

Another strange ESM edge case!

Some files in the openapi sdk import * themselves like below.

I don't know how common this is elsewhere but these files are auto-generated which might explain this strange pattern.

batches.mjs

// File generated from our OpenAPI spec by Stainless.
import * as BatchesAPI from "./batches.mjs";
export class Batches {}
export class BatchesPage {}
(function (Batches) {
    Batches.BatchesPage = BatchesAPI.BatchesPage;
})(Batches || (Batches = {}));

If you import the above in Node, there is no error.

If the import-in-the-middle/hook.mjs is registered, you get the following error:

file:///Users/tim/Documents/Repositories/repro/more.mjs:5
    Batches.BatchesPage = BatchesAPI.BatchesPage;
                                     ^
ReferenceError: Cannot access 'BatchesPage' before initialization
    at file:///Users/tim/Documents/Repositories/repro/more.mjs:5:38
    at file:///Users/tim/Documents/Repositories/repro/more.mjs:6:3
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:475:24)
    at async file:///Users/tim/Documents/Repositories/repro/test.mjs:5:1