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

Bad behaviour with default import because of esModuleInterop ? #186

Open
mastondzn opened this issue Feb 26, 2024 · 1 comment
Open

Bad behaviour with default import because of esModuleInterop ? #186

mastondzn opened this issue Feb 26, 2024 · 1 comment
Labels

Comments

@mastondzn
Copy link

I was trying to use this package with @rollup/plugin-typescript to resolve and edit paths in emitted .d.ts files and the hacky way i finally made it work is like this:

// rollup.config.js
import typescript from "@rollup/plugin-typescript";
import { defineConfig } from "rollup";

/** @type {import("typescript-transform-paths").default} */
const transformPaths = (await import("typescript-transform-paths")).default.default;
// "typescript-transform-paths" has a broken default export

export default defineConfig({
  input: "src/index.ts",
  output: [
    { file: "dist/index.cjs", sourcemap: true, format: "cjs" },
    { file: "dist/index.js", sourcemap: true, format: "esm" },
  ],
  plugins: [
    typescript({
      transformers: {
        before: [{ type: "program", factory: transformPaths }],
        afterDeclarations: [{ type: "program", factory: transformPaths }],
      },
    }),
  ],
});

The true default import looks like this , when it should be the transformer function.

import test from "typescript-transform-paths"
console.log(test);
// {
//   register: [Getter],
//   nxTransformerPlugin: [Getter],
//   default: [Function: transformer]
// }

This also means that the types do not correlate (hence the jsdoc comment in my config) so you need to cast it to have nice dx.
I believe this happens because of the way esModuleInterop assigns "default" to the "exports" object though i am not sure.

Side note: For me this package did not work by just editing my tsconfig.json (as suggested in this project's readme), though i believe that it should have (i see no reason the rollup plugin cant use it), maybe that is related to this, as typescript cannot resolve this import. (Maybe the blocker to #134?)

A fix to allow for easier programmatic usage is to include the transformer (and other things, why not) as a named export along the default export, this way the type would match and people could use it this way.

A overall fix would be to figure out a better way to transpile this package to both esm and cjs (rollup?) and therefore not having to rely on esModuleInterop.

I could attempt a PR if any of these options are desirable 👍

@danielpza
Copy link
Member

Hey @mastondzn

A fix to allow for easier programmatic usage is to include the transformer (and other things, why not) as a named export along the default export, this way the type would match and people could use it this way.
I could attempt a PR if any of these options are desirable 👍

Sure, go for it 👍

@danielpza danielpza added the Good First Issue Good for newcomers label Aug 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants