mkdist icon indicating copy to clipboard operation
mkdist copied to clipboard

Improve import extension normalization

Open pi0 opened this issue 2 years ago • 2 comments

mkdist uses a post-build transform to add explicit extensions (src) but this is not always working (eg with multi line imports) https://github.com/unjs/unbuild/issues/83.

We shall migrate to mlly.parseStaticImports.

pi0 avatar Jun 27 '22 09:06 pi0

You mean that we will improve like this.

{ builder: "mkdist", input: "./src/common", name: "untils", outDir: "common", ext: "cjs", format: "cjs", },

But after build, I see code built not imported with extension.

var _other = require("../constants/other");

So we need to update to: var _other = require("../constants/other.cjs");

Right man?

hieusmiths avatar Jul 01 '22 02:07 hieusmiths

@hieusmiths Explicit .cjs extensions should be added in last version try upgrading.

If rewrite is still not working, please share your source code snippet 🙏

pi0 avatar Jul 01 '22 09:07 pi0

Facing with the same. @pi0 , but I am not understand why shall migrate to mlly.parseStaticImports. Is it possible to just improve the regx pattern

output.contents = output.contents!.replace(
    /(import|export)(.* from ["'])(.*)(["'])/g,
    (_, type, head, id, tail) => type + head + resolveId(output.path, id, esmResolveExtensions) + tail
  );

into

output.contents = output.contents!.replace(
    /(import|export)([\s\S]* from ["'])(.*)(["'])/g,
    (_, type, head, id, tail) => type + head + resolveId(output.path, id, esmResolveExtensions) + tail
  );

It seems worked.

zoeyzhao19 avatar Jan 05 '23 05:01 zoeyzhao19

I've created #120 to fix this. Would really appreciate it if we could merge and release the fix as the issue is creating broken builds without any warning for some people.

Note: The [\s\S] solution above works in some cases, but will spill over to exported objects and functions.

alexgrozav avatar Jan 12 '23 09:01 alexgrozav