Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix exports when configs with different bundle settings are used. #185

Merged
merged 2 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/packemon/src/Artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,13 @@ export class Artifact {
// eslint-disable-next-line complexity
getBuildOutput(format: Format, outputName: string, declaration: boolean = false) {
const inputFile = this.inputs[outputName];
let name = outputName;
const inputPath = inputFile ? removeSourcePath(inputFile) : '';
let outputPath = outputName;

// When using a public API, we do not create output files based on the input map.
// Instead files mirror the source file structure, so we need to take that into account!
if (this.api === 'public' && inputFile) {
name = removeSourcePath(inputFile);
if ((this.api === 'public' || !this.bundle) && inputFile) {
outputPath = inputPath;
}

const folder = format === 'lib' && this.sharedLib ? `lib/${this.platform}` : format;
Expand All @@ -240,9 +241,11 @@ export class Artifact {

return {
declExt,
declPath: declExt ? `./${new VirtualPath(folder, `${name}.${declExt}`)}` : undefined,
declPath: declExt
? `./${new VirtualPath(folder, `${inputPath ?? outputPath}.${declExt}`)}`
: undefined,
entryExt,
entryPath: `./${new VirtualPath(folder, `${name}.${entryExt}`)}`,
entryPath: `./${new VirtualPath(folder, `${outputPath}.${entryExt}`)}`,
folder,
};
}
Expand Down
42 changes: 42 additions & 0 deletions packages/packemon/tests/Package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,48 @@ describe('Package', () => {
});
});

it('adds exports for multiple artifacts with different bundle types', async () => {
const a = createCodeArtifact([{ format: 'esm', declaration: true }], 'browser');
a.inputs.utils = 'src/utils/index.ts';
a.bundle = true;
pkg.artifacts.push(a);

const b = createCodeArtifact([{ format: 'lib', declaration: true }], 'node');
b.inputs.utils = 'src/utils/index.ts';
b.bundle = false;
pkg.artifacts.push(b);

await pkg.build({ addExports: true }, config);

expect(pkg.json.exports).toEqual({
'.': {
node: {
default: './lib/index.js',
types: './lib/index.d.ts',
},
browser: {
import: './esm/index.js',
module: './esm/index.js',
types: './esm/index.d.ts',
},
default: './lib/index.js',
},
'./utils': {
node: {
default: './lib/utils/index.js',
types: './lib/utils/index.d.ts',
},
browser: {
import: './esm/utils.js',
module: './esm/utils.js',
types: './esm/utils/index.d.ts',
},
default: './lib/utils/index.js',
},
'./package.json': './package.json',
});
});

it('adds exports for bundle and types artifacts in parallel', async () => {
pkg.artifacts.push(createCodeArtifact([{ declaration: true, format: 'lib' }]));

Expand Down