Skip to content

Commit

Permalink
Avoid empty reexport statements in .cjs.mjs files (#558)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed May 18, 2023
1 parent 00c80bb commit 6860429
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
68 changes: 61 additions & 7 deletions packages/cli/src/build/__tests__/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1778,10 +1778,6 @@ test("type only export imported in .mts", async () => {
exports: { importConditionDefaultExport: "default" },
},
}),
"packages/pkg-a/something/package.json": JSON.stringify({
main: "dist/pkg-a-something.cjs.js",
module: "dist/pkg-a-something.esm.js",
}),
"packages/pkg-a/src/index.ts": ts`
export type SomeType = string;
`,
Expand Down Expand Up @@ -1831,9 +1827,7 @@ test("type only export imported in .mts", async () => {
}
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ packages/pkg-a/dist/pkg-a.cjs.mjs ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export {
} from "./pkg-a.cjs.js";
import "./pkg-a.cjs.js";
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ packages/pkg-a/dist/pkg-a.esm.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Expand Down Expand Up @@ -2191,3 +2185,63 @@ test("correct default export using mjs and dmts proxies with moduleResolution: b
`);
expect(node.stderr.toString("utf8")).toMatchInlineSnapshot(`""`);
});

test("module with no runtime exports but with init-time side-effects with importConditionDefaultExport", async () => {
let dir = await testdir({
"package.json": JSON.stringify({
name: "repo",
preconstruct: {
packages: ["packages/pkg-a"],
exports: {
importConditionDefaultExport: "default",
},
},
}),
"packages/pkg-a/package.json": JSON.stringify({
name: "pkg-a",
main: "dist/pkg-a.cjs.js",
module: "dist/pkg-a.esm.js",
exports: {
".": {
module: "./dist/pkg-a.esm.js",
import: "./dist/pkg-a.cjs.mjs",
default: "./dist/pkg-a.cjs.js",
},
"./package.json": "./package.json",
},
}),
"packages/pkg-a/src/index.ts": ts`
console.log("hey!");
`,
...tsSetupFiles,
"runtime-blah.mjs": js`
import "pkg-a";
`,
"runtime-blah.cjs": js`
require("pkg-a");
`,
});
await fs.ensureSymlink(
path.join(dir, "packages/pkg-a"),
path.join(dir, "node_modules/pkg-a")
);
await build(dir);

let node = await spawn("node", ["runtime-blah.mjs"], { cwd: dir });

expect(node.code).toBe(0);
expect(node.stdout.toString("utf8")).toMatchInlineSnapshot(`
"hey!
"
`);
expect(node.stderr.toString("utf8")).toMatchInlineSnapshot(`""`);

node = await spawn("node", ["runtime-blah.cjs"], { cwd: dir });

expect(node.code).toBe(0);
expect(node.stdout.toString("utf8")).toMatchInlineSnapshot(`
"hey!
"
`);
expect(node.stderr.toString("utf8")).toMatchInlineSnapshot(`""`);
});
4 changes: 1 addition & 3 deletions packages/cli/src/build/__tests__/imports-conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,7 @@ test("import with #something inside import type type arguments", async () => {
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/scope-pkg.cjs.mjs ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
export {
} from "./scope-pkg.cjs.js";
import "./scope-pkg.cjs.js";
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ dist/scope-pkg.esm.js ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ export function dtsTemplate(
}

function getReexportStatement(namedExports: string[], source: string): string {
if (!namedExports.length) {
// side-effects are important
return `import ${source};`;
}
// rollup will say a chunk has a "*external-pkg" export when it has an export * from 'external-pkg'
if (namedExports.some((exported) => exported[0] === "*")) {
return `export * from ${source};`;
Expand Down

0 comments on commit 6860429

Please sign in to comment.