Skip to content

Commit

Permalink
Change unwrappedDefaultExportForImportCondition to `importDefaultEx…
Browse files Browse the repository at this point in the history
…port`
  • Loading branch information
Andarist committed Apr 27, 2023
1 parent d80fa6f commit 241b4ad
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions .changeset/brown-shoes-taste.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"@preconstruct/cli": minor
---

Added a new `exports.unwrappedDefaultExportForImportCondition` config option. It allows you to generate `import` exports condition (and corresponding files) to fix the export shape incompatibility between node and bundlers.
Added a new `exports.importDefaultExport` config option. It allows you to generate `import` exports condition (and corresponding files) to fix the export shape incompatibility between node and bundlers.

With this option this will always resolve to what has been written as a default export:
With this option set to `"unwrapped-default"` this will always resolve to what has been written as a default export:

```ts
// lib/src/index.js
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/build/__tests__/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ test("correct default export using mjs and dmts proxies", async () => {
preconstruct: {
entrypoints: ["index.ts", "something.ts"],
exports: {
unwrappedDefaultExportForImportCondition: true,
importDefaultExport: "unwrapped-default",
},
},
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/build/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export let getRollupConfig = (
resolveErrorsPlugin(pkg, warnings, type === "umd"),
type === "node-prod" && typescriptDeclarations(pkg),
type === "node-prod" &&
pkg.exportsFieldConfig()?.unwrappedDefaultExportForImportCondition &&
pkg.exportsFieldConfig()?.importDefaultExport === "unwrapped-default" &&
mjsProxy(),
serverComponentsPlugin({ sourceMap: type === "umd" }),
babel({
Expand Down
12 changes: 6 additions & 6 deletions packages/cli/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ type CanonicalExportsFieldConfig =
| {
envConditions: Set<"worker" | "browser">;
extra: Record<string, JSONValue>;
unwrappedDefaultExportForImportCondition: boolean;
importDefaultExport: "namespace" | "unwrapped-default";
};

function parseExportsFieldConfig(
Expand Down Expand Up @@ -383,7 +383,7 @@ function parseExportsFieldConfig(
const parsedConfig: CanonicalExportsFieldConfig = {
envConditions: new Set(),
extra: {},
unwrappedDefaultExportForImportCondition: false,
importDefaultExport: "namespace",
};
if (config === true) {
return parsedConfig;
Expand Down Expand Up @@ -422,12 +422,12 @@ function parseExportsFieldConfig(
name
);
}
} else if (key === "unwrappedDefaultExportForImportCondition") {
if (typeof value === "boolean") {
parsedConfig.unwrappedDefaultExportForImportCondition = value;
} else if (key === "importDefaultExport") {
if (value === "unwrapped-default" || value === "namespace") {
parsedConfig.importDefaultExport = value;
} else {
throw new FatalError(
'the "preconstruct.exports.unwrappedDefaultExportForImportCondition" field must be a boolean if it is present',
'the "preconstruct.exports.importDefaultExport" field must be set to "unwrapped-default" or "namespace" if it is present',
name
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export default function typescriptDeclarations(pkg: Package): Plugin {
});

if (
pkg.exportsFieldConfig()?.unwrappedDefaultExportForImportCondition
pkg.exportsFieldConfig()?.importDefaultExport === "unwrapped-default"
) {
this.emitFile({
type: "asset",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export function exportsField(
default: esmBuild,
}
: esmBuild,
...(exportsFieldConfig.unwrappedDefaultExportForImportCondition && {
...(exportsFieldConfig.importDefaultExport === "unwrapped-default" && {
import: getExportsFieldOutputPath(entrypoint, "cjs").replace(
/\.js$/,
".mjs"
Expand Down

0 comments on commit 241b4ad

Please sign in to comment.