Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Avoid ESBuild destroying Proxy in __toESM #164

Merged
merged 1 commit into from
Oct 12, 2024

Conversation

Danielku15
Copy link
Member

@Danielku15 Danielku15 commented Oct 12, 2024

Fixes #153

When transpiling typescript (or ESM) toa ESBuild, it translates imports in a way like this:

TypeScript:

import path, { join } from 'node:path';

JS:

"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var import_node_path = __toESM(require("node:path"));
import_node_path.default.join("", "");
(0, import_node_path.join)("", "");

As we can see, it created a complete new object using Object.create copying all individual properties over. This way it translates ESM modules to CJS during runtime respecting the __esModule flags. This completely destroys our Proxy object representing our fake imported module.

With this fix we ensure Object.create also will create again a Proxy object restoring our runtime dynamic module.

@Danielku15 Danielku15 self-assigned this Oct 12, 2024
@Danielku15 Danielku15 force-pushed the feature/fix-broken-imports branch from da50beb to e49b584 Compare October 12, 2024 15:04
@Danielku15 Danielku15 merged commit c22bfb3 into main Oct 12, 2024
13 checks passed
@Danielku15 Danielku15 deleted the feature/fix-broken-imports branch October 12, 2024 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: ESbuild fails to install and extension fails to extract tests
1 participant