Skip to content

Commit

Permalink
Merge pull request #909 from pmcelhaney/file-extension-fix
Browse files Browse the repository at this point in the history
fix #906 -- transpile imports without JS extension properly
  • Loading branch information
pmcelhaney committed May 15, 2024
2 parents f692750 + 88adb61 commit 30e2609
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-horses-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"counterfact": patch
---

fixes an issue where imports without a file extension were not transpiled properly
3 changes: 2 additions & 1 deletion src/server/convert-js-extensions-to-cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function convertFileExtensionsToCjs(code: string) {
) {
// Change the module string from "foo.js" to "foo.cjs"
node.arguments[0].value = node.arguments[0].value.replace(
/\.js$/u,
// eslint-disable-next-line prefer-named-capture-group, regexp/no-unused-capturing-group, regexp/prefer-named-capture-group
/(\.js|\.ts)?$/u,
".cjs",
);
}
Expand Down
22 changes: 17 additions & 5 deletions test/server/convert-js-extension-to-cjs.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import { convertFileExtensionsToCjs } from "../../src/server/convert-js-extensions-to-cjs.js";

describe("convertFileExtensionsToCjs", () => {
it("converts a local require", () => {
expect(convertFileExtensionsToCjs('require("./foo.js");')).toEqual(
'require("./foo.cjs");',
);
});
it.each(["./foo.js", "./foo.ts", "./foo"])(
"converts %s a local require",
(path) => {
expect(convertFileExtensionsToCjs(`require("${path}");`)).toEqual(
'require("./foo.cjs");',
);
},
);

it.each(["../foo.js", "../foo.ts", "../foo"])(
"converts %s a local require",
(path) => {
expect(convertFileExtensionsToCjs(`require("${path}");`)).toEqual(
'require("../foo.cjs");',
);
},
);

it("does not convert a non-local require", () => {
expect(convertFileExtensionsToCjs('require("foo/bar.js");')).toEqual(
Expand Down

0 comments on commit 30e2609

Please sign in to comment.