unjs / unjs/obuild

Support rewriting specifiers in dynamic imports for `transform` entry mode

Open
#92 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
432
Forks
21
PR merge metrics
No merged PRs in 30d

Description

Describe the feature

Hi!

I was trying the transform entry type and noticed that import specifier rewriting works for static imports, but not for dynamic imports.

Example setup:

src/hello-world.ts

export default () => {
  return "hello-world";
};

src/index.ts

const { default: helloWorld } = await import("./hello-world.ts");
console.log(helloWorld());

build.config.ts

import { defineBuildConfig } from "obuild/config";

export default defineBuildConfig({
  entries: [
    {
      type: "transform",
      input: "./src",
      outDir: "./dist",
      dts: false
    }
  ]
});

The generated output in dist/index.mjs dynamic import remains:

await import("./hello-world.ts");

instead of:

await import("./hello-world.mjs");

Even though dist/hello-world.mjs file was generated.

I was wondering if dynamic imports are intentionally excluded, if they are I may be completely wrong here.

Would you be open to adding support for rewriting dynamic import specifiers as well? For example, adding a TransformEntry option like:

rewriteDynamicImportExtensions: true

(or another name that better fits) could enable transforming .ts extensions from dynamic imports

Solution

I tried implementing this change in a fork, it worked for me but I'm not an expert in build tools so I'm not sure if this'd be the right approach as it only rewrites string literals

https://github.com/unjs/obuild/blob/776eab2ae2ed6aa16e83829cc6d5831d8fd3a68a/src/builders/transform.ts#L202-L214

Thanks!

Additional information
  • Would you be willing to help implement this feature?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/builders/transform.ts around the linked lines and compare how static import specifiers are rewritten with the dynamic-import case. Use the provided src/index.ts, src/hello-world.ts, and build.config.ts example to check the generated dist/index.mjs. Done means the dynamic import points to the generated .mjs file while preserving existing transform behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.