Skip to content

Commit

Permalink
fix: Use non-index inputs for entry points. (#149)
Browse files Browse the repository at this point in the history
* Code changes.

* Add tests.
  • Loading branch information
milesj authored Sep 7, 2022
1 parent 0bec0c7 commit 8cecfba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
38 changes: 21 additions & 17 deletions packages/packemon/src/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,25 +438,25 @@ export class Package {
this.artifacts.forEach((artifact) => {
// Build files
if (artifact instanceof CodeArtifact) {
const mainEntryName = artifact.inputs.index ? 'index' : Object.keys(artifact.inputs)[0];

buildCount += artifact.builds.length;

// Generate `main`, `module`, and `browser` fields
if (artifact.inputs.index) {
if (!mainEntry || artifact.platform === 'node') {
mainEntry = artifact.findEntryPoint(['lib', 'cjs', 'mjs', 'esm'], 'index');
}

if (!moduleEntry) {
moduleEntry = artifact.findEntryPoint(['esm'], 'index');
}

// Only include when we share a lib with another platform
if (!browserEntry && artifact.platform === 'browser') {
browserEntry = artifact.findEntryPoint(
artifact.sharedLib ? ['lib', 'umd'] : ['umd'],
'index',
);
}
if (!mainEntry || (artifact.platform === 'node' && mainEntryName === 'index')) {
mainEntry = artifact.findEntryPoint(['lib', 'cjs', 'mjs', 'esm'], mainEntryName);
}

if (!moduleEntry || (artifact.platform === 'browser' && mainEntryName === 'index')) {
moduleEntry = artifact.findEntryPoint(['esm'], mainEntryName);
}

// Only include when we share a lib with another platform
if (!browserEntry && artifact.platform === 'browser') {
browserEntry = artifact.findEntryPoint(
artifact.sharedLib ? ['lib', 'umd'] : ['umd'],
mainEntryName,
);
}

// Generate `bin` field
Expand All @@ -471,7 +471,11 @@ export class Package {

// Type declarations
if (artifact instanceof TypesArtifact) {
this.packageJson.types = artifact.findEntryPoint('index');
const mainEntryName = artifact.builds.some((build) => build.outputName === 'index')
? 'index'
: artifact.builds[0].outputName;

this.packageJson.types = artifact.findEntryPoint(mainEntryName);
}
});

Expand Down
4 changes: 2 additions & 2 deletions packages/packemon/tests/Package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,14 @@ describe('Package', () => {
);
});

it('doesnt add "main" if output name is not "index"', async () => {
it('adds "main" if output name is not "index"', async () => {
const a = createCodeArtifact([{ format: 'lib' }]);
a.inputs = { server: 'src/index.ts' };
pkg.addArtifact(a);

await pkg.build({}, config);

expect(pkg.packageJson.main).toBeUndefined();
expect(pkg.packageJson.main).toBe('./lib/server.js');
});

it('adds "main" when using shared `lib` format', async () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/packemon/tests/__snapshots__/outputs.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ exports[`Outputs (babel) artifacts builds all the artifacts with rollup 6`] = `
Array [
"package.json",
Object {
"browser": "./umd/client.js",
"dependencies": Object {
"typescript": "*",
},
"main": "./lib/index.js",
"module": "./esm/client.js",
"name": "project-rollup",
"packemon": Object {
"inputs": Object {
Expand Down Expand Up @@ -859,10 +861,12 @@ exports[`Outputs (swc) artifacts builds all the artifacts with rollup 6`] = `
Array [
"package.json",
Object {
"browser": "./umd/client.js",
"dependencies": Object {
"typescript": "*",
},
"main": "./lib/index.js",
"module": "./esm/client.js",
"name": "project-rollup",
"packemon": Object {
"inputs": Object {
Expand Down

0 comments on commit 8cecfba

Please sign in to comment.