Skip to content

Commit

Permalink
Fix exports map. (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Mar 6, 2023
1 parent 04ff6bf commit e17237e
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
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

0 comments on commit e17237e

Please sign in to comment.