Skip to content

Commit

Permalink
fix: Fix sub-path types exports being wrong.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Mar 6, 2023
1 parent e17237e commit e151564
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/packemon/src/Artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ export class Artifact {
// eslint-disable-next-line complexity
getBuildOutput(format: Format, outputName: string, declaration: boolean = false) {
const inputFile = this.inputs[outputName];
const inputPath = inputFile ? removeSourcePath(inputFile) : '';
const inputPath = inputFile ? removeSourcePath(inputFile) : undefined;
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' || !this.bundle) && inputFile) {
if ((this.api === 'public' || !this.bundle) && inputPath) {
outputPath = inputPath;
}

Expand Down
108 changes: 86 additions & 22 deletions packages/packemon/tests/Package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,19 +782,26 @@ describe('Package', () => {

// https://github.com/milesj/packemon/issues/42#issuecomment-808793241
it('private api: uses inputs as subpath imports', async () => {
const a = createCodeArtifact([{ format: 'cjs' }]);
const a = createCodeArtifact([{ format: 'cjs', declaration: true }]);
a.api = 'private';
a.inputs = { index: 'src/node.ts' };

const b = createCodeArtifact([{ format: 'lib' }]);
const b = createCodeArtifact([{ format: 'lib', declaration: true }]);
b.api = 'private';
b.inputs = { bin: 'src/cli.ts' };

const c = createCodeArtifact([{ format: 'lib' }, { format: 'esm' }], 'browser', 'current');
const c = createCodeArtifact(
[
{ format: 'lib', declaration: true },
{ format: 'esm', declaration: true },
],
'browser',
'current',
);
c.api = 'private';
c.inputs = { web: 'src/web.ts' };

const d = createCodeArtifact([{ format: 'mjs' }], 'node', 'current');
const d = createCodeArtifact([{ format: 'mjs', declaration: true }], 'node', 'current');
d.api = 'private';
d.inputs = { import: 'src/web.ts' };

Expand All @@ -808,39 +815,56 @@ describe('Package', () => {
bin: './lib/bin.js',
exports: {
'./package.json': './package.json',
'.': { node: { import: './cjs/index-wrapper.mjs', require: './cjs/index.cjs' } },
'./bin': { node: './lib/bin.js', default: './lib/bin.js' },
'.': {
node: {
import: './cjs/index-wrapper.mjs',
require: './cjs/index.cjs',
types: './cjs/node.d.ts',
},
},
'./bin': {
node: { default: './lib/bin.js', types: './lib/cli.d.ts' },
default: './lib/bin.js',
},
'./web': {
browser: {
import: './esm/web.js',
module: './esm/web.js',
default: './lib/web.js',
types: './esm/web.d.ts',
},
default: './lib/web.js',
},
'./import': { node: { import: './mjs/import.mjs' } },
'./import': { node: { import: './mjs/import.mjs', types: './mjs/web.d.ts' } },
},
}),
);
});

it('public api + bundle: uses inputs as subpath imports (non-deep imports)', async () => {
const a = createCodeArtifact([{ format: 'cjs' }]);
const a = createCodeArtifact([{ format: 'cjs', declaration: true }]);
a.api = 'public';
a.bundle = true;
a.inputs = { index: 'src/node.ts' };

const b = createCodeArtifact([{ format: 'lib' }]);
const b = createCodeArtifact([{ format: 'lib', declaration: true }]);
b.api = 'public';
b.bundle = true;
b.inputs = { bin: 'src/cli.ts' };

const c = createCodeArtifact([{ format: 'lib' }, { format: 'esm' }], 'browser', 'current');
const c = createCodeArtifact(
[
{ format: 'lib', declaration: true },
{ format: 'esm', declaration: true },
],
'browser',
'current',
);
c.api = 'public';
c.bundle = true;
c.inputs = { web: 'src/web.ts' };

const d = createCodeArtifact([{ format: 'mjs' }], 'node', 'current');
const d = createCodeArtifact([{ format: 'mjs', declaration: true }], 'node', 'current');
d.api = 'public';
d.bundle = true;
d.inputs = { import: 'src/web.ts' };
Expand All @@ -855,35 +879,59 @@ describe('Package', () => {
bin: './lib/cli.js',
exports: {
'./package.json': './package.json',
'./bin': { node: './lib/cli.js', default: './lib/cli.js' },
'./import': { node: { import: './mjs/web.mjs' } },
'./bin': {
node: {
default: './lib/cli.js',
types: './lib/cli.d.ts',
},
default: './lib/cli.js',
},
'./import': { node: { import: './mjs/web.mjs', types: './mjs/web.d.ts' } },
'./web': {
browser: { import: './esm/web.js', module: './esm/web.js', default: './lib/web.js' },
browser: {
import: './esm/web.js',
module: './esm/web.js',
default: './lib/web.js',
types: './esm/web.d.ts',
},
default: './lib/web.js',
},
'.': { node: { import: './cjs/node-wrapper.mjs', require: './cjs/node.cjs' } },
'.': {
node: {
import: './cjs/node-wrapper.mjs',
require: './cjs/node.cjs',
types: './cjs/node.d.ts',
},
},
},
}),
);
});

it('public api + no bundle: uses patterns as subpath imports (deep imports)', async () => {
const a = createCodeArtifact([{ format: 'cjs' }]);
const a = createCodeArtifact([{ format: 'cjs', declaration: true }]);
a.api = 'public';
a.bundle = false;
a.inputs = { index: 'src/node.ts' };

const b = createCodeArtifact([{ format: 'lib' }]);
const b = createCodeArtifact([{ format: 'lib', declaration: true }]);
b.api = 'public';
b.bundle = false;
b.inputs = { bin: 'src/cli.ts' };

const c = createCodeArtifact([{ format: 'lib' }, { format: 'esm' }], 'browser', 'current');
const c = createCodeArtifact(
[
{ format: 'lib', declaration: true },
{ format: 'esm', declaration: true },
],
'browser',
'current',
);
c.api = 'public';
c.bundle = false;
c.inputs = { web: 'src/web.ts' };

const d = createCodeArtifact([{ format: 'mjs' }], 'node', 'current');
const d = createCodeArtifact([{ format: 'mjs', declaration: true }], 'node', 'current');
d.api = 'public';
d.bundle = false;
d.inputs = { import: 'src/web.ts' };
Expand All @@ -900,17 +948,33 @@ describe('Package', () => {
exports: {
'./package.json': './package.json',
'./*': {
browser: { import: './esm/*.js', module: './esm/*.js', default: './lib/*.js' },
node: { import: './mjs/*.mjs', require: './cjs/*.cjs', default: './lib/*.js' },
browser: {
import: './esm/*.js',
module: './esm/*.js',
default: './lib/*.js',
types: './esm/*.d.ts',
},
node: {
import: './mjs/*.mjs',
require: './cjs/*.cjs',
default: './lib/*.js',
types: './mjs/*.d.ts',
},
default: './lib/*.js',
},
'.': {
browser: {
import: './esm/web.js',
module: './esm/web.js',
default: './lib/web.js',
types: './esm/web.d.ts',
},
node: {
import: './mjs/web.mjs',
require: './cjs/node.cjs',
default: './lib/cli.js',
types: './mjs/web.d.ts',
},
node: { import: './mjs/web.mjs', require: './cjs/node.cjs', default: './lib/cli.js' },
default: './lib/web.js',
},
},
Expand Down

0 comments on commit e151564

Please sign in to comment.