Skip to content

Allow preservation of dynamic import with variable in CJS #4610

@nzakas

Description

@nzakas

Feature Use Case

Because Node.js supports both require() and import() in CommonJS files, it would be helpful to be able to retain import() calls instead of translating them into require(). Here's a very simple example of what I'm trying to do:

https://rollupjs.org/repl/?version=2.71.1&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmV4cG9ydCUyMGZ1bmN0aW9uJTIwZG9JbXBvcnQoc3BlY2lmaWVyKSUyMCU3QiU1Q24lNUN0cmV0dXJuJTIwaW1wb3J0KHNwZWNpZmllciklM0IlNUNuJTdEJTIyJTJDJTIyaXNFbnRyeSUyMiUzQXRydWUlN0QlNUQlMkMlMjJvcHRpb25zJTIyJTNBJTdCJTIyZm9ybWF0JTIyJTNBJTIyY2pzJTIyJTJDJTIybmFtZSUyMiUzQSUyMm15QnVuZGxlJTIyJTJDJTIyYW1kJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlMkMlMjJnbG9iYWxzJTIyJTNBJTdCJTdEJTdEJTJDJTIyZXhhbXBsZSUyMiUzQW51bGwlN0Q=

Feature Proposal

Add a preserveDynamicImports option that can be set to:

  • "all" - never rewrite import() regardless of the value passed in
  • "variable" - only preserve import() if it's used with a variable, otherwise overwrite as usual
  • "static" - only preserve import() when passed a constant

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions