Skip to content

Commit

Permalink
feat: add support for package.json exports
Browse files Browse the repository at this point in the history
With this change exports field will be included in the package.json of the library.
  • Loading branch information
alan-agius4 committed Oct 1, 2021
1 parent bdbabeb commit 047e3ca
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 3 deletions.
32 changes: 32 additions & 0 deletions integration/samples/apf/specs/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ describe(`@sample/apf`, () => {
it(`should apply the 'sideEffects: false' flag by default`, () => {
expect(PACKAGE['sideEffects']).to.be.false;
});

it(`should add package 'exports'`, () => {
expect(PACKAGE['exports']).to.deep.equal({
'./package.json': {
default: './package.json',
},
'.': {
types: './sample-apf.d.ts',
default: './fesm2020/sample-apf.js',
},
'./secondary': {
types: './secondary/sample-apf-secondary.d.ts',
default: './fesm2020/sample-apf-secondary.js',
},
'./secondary/testing': {
types: './secondary/testing/sample-apf-secondary-testing.d.ts',
default: './fesm2020/sample-apf-secondary-testing.js',
},
'./testing': {
types: './testing/sample-apf-testing.d.ts',
default: './fesm2020/sample-apf-testing.js',
},
});
});
});

describe(`secondary/package.json`, () => {
Expand Down Expand Up @@ -86,6 +110,10 @@ describe(`@sample/apf`, () => {
it(`should apply the 'sideEffects: false' flag by default`, () => {
expect(PACKAGE['sideEffects']).to.be.false;
});

it(`should not add package 'exports'`, () => {
expect(PACKAGE['exports']).to.be.undefined;
});
});

describe(`secondary/testing/package.json`, () => {
Expand Down Expand Up @@ -173,5 +201,9 @@ describe(`@sample/apf`, () => {
it(`should apply the 'sideEffects: false' flag by default`, () => {
expect(PACKAGE['sideEffects']).to.be.false;
});

it(`should not add package 'exports'`, () => {
expect(PACKAGE['exports']).to.be.undefined;
});
});
});
2 changes: 0 additions & 2 deletions src/lib/ng-package/entry-point/entry-point.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe(`NgEntryPoint`, () => {
expect(primaryEntryPoint.entryFilePath).to.equal(resolve(testSourcePath, 'public-api.ts'));
expect(primaryEntryPoint.isSecondaryEntryPoint).to.be.false;
expect(primaryEntryPoint.destinationPath).to.equal(resolve(testDestinationPath));
expect(Object.keys(primaryEntryPoint.destinationFiles).length).to.equal(4);
expect(primaryEntryPoint.entryFile).to.equal('public-api.ts');
expect(primaryEntryPoint.cssUrl).to.be.undefined; // TODO: should default to 'inline'.
expect(primaryEntryPoint.flatModuleFile).to.equal('example-test-entry-point');
Expand Down Expand Up @@ -104,7 +103,6 @@ describe(`NgEntryPoint`, () => {
expect(secondaryEntryPoint.entryFilePath).to.equal(resolve(secondaryEntryPointBasePath, 'public-api.ts'));
expect(secondaryEntryPoint.isSecondaryEntryPoint).to.be.true;
expect(secondaryEntryPoint.destinationPath).to.equal(resolve(secondaryData.destinationPath));
expect(Object.keys(secondaryEntryPoint.destinationFiles).length).to.equal(4);
expect(secondaryEntryPoint.entryFile).to.equal('public-api.ts');
expect(secondaryEntryPoint.cssUrl).to.be.undefined; // TODO: should default to 'inline'.
expect(secondaryEntryPoint.flatModuleFile).to.equal('example-test-entry-point-extra');
Expand Down
4 changes: 4 additions & 0 deletions src/lib/ng-package/entry-point/entry-point.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as path from 'path';
import { NgPackageConfig } from '../../../ng-package.schema';
import { CssUrl } from '../../styles/stylesheet-processor';
import { ensureUnixPath } from '../../utils/path';

/** A list of output absolute paths for various formats */
export interface DestinationFiles {
Expand All @@ -12,6 +13,8 @@ export interface DestinationFiles {
fesm2015: string;
/** Absolute path of this entry point `ESM2020` module */
esm2020: string;
/** Sub path of entrypoint distributable. */
directory: string;
}

/**
Expand Down Expand Up @@ -87,6 +90,7 @@ export class NgEntryPoint {
const pathJoinWithDest = (...paths: string[]) => path.join(primaryDestPath, ...paths);

return {
directory: ensureUnixPath(secondaryDir),
declarations: pathJoinWithDest(secondaryDir, `${flatModuleFile}.d.ts`),
esm2020: pathJoinWithDest('esm2020', secondaryDir, `${flatModuleFile}.js`),
fesm2020: pathJoinWithDest('fesm2020', `${flatModuleFile}.js`),
Expand Down
36 changes: 35 additions & 1 deletion src/lib/ng-package/entry-point/write-package.transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { ensureUnixPath } from '../../utils/path';
import { copyFile, exists, stat, rmdir, writeFile } from '../../utils/fs';
import * as log from '../../utils/log';
import { globFiles } from '../../utils/glob';
import { EntryPointNode, isEntryPointInProgress, isPackage, PackageNode, fileUrl } from '../nodes';
import { EntryPointNode, isEntryPointInProgress, isPackage, PackageNode, fileUrl, isEntryPoint } from '../nodes';
import { Node } from '../../graph/node';
import { NgPackagrOptions } from '../options.di';
import { BuildGraph } from '../../graph/build-graph';

type CompilationMode = 'partial' | 'full' | undefined;

Expand Down Expand Up @@ -99,12 +100,14 @@ export const writePackageTransform = (options: NgPackagrOptions) =>
ngEntryPoint,
ngPackage,
{
type: 'module',
module: relativeUnixFromDestPath(destinationFiles.fesm2015),
es2020: relativeUnixFromDestPath(destinationFiles.fesm2020),
esm2020: relativeUnixFromDestPath(destinationFiles.esm2020),
fesm2020: relativeUnixFromDestPath(destinationFiles.fesm2020),
fesm2015: relativeUnixFromDestPath(destinationFiles.fesm2015),
typings: relativeUnixFromDestPath(destinationFiles.declarations),
exports: ngEntryPoint.packageJson.exports || generatePackageExports(ngEntryPoint, graph),
// webpack v4+ specific flag to enable advanced optimizations and code splitting
sideEffects: ngEntryPoint.sideEffects,
},
Expand Down Expand Up @@ -271,3 +274,34 @@ function checkNonPeerDependencies(
}
}
}

type PackageExports = Record<string, { default: string; types?: string }>;
function generatePackageExports(
{ isSecondaryEntryPoint, destinationPath }: NgEntryPoint,
graph: BuildGraph,
): PackageExports | undefined {
if (isSecondaryEntryPoint) {
// Package exports are only available in the primary entrypoint.
return undefined;
}

const entryPoints = graph.filter(isEntryPoint);
const exports: PackageExports = {
'./package.json': {
default: './package.json',
},
};

const relativeUnixFromDestPath = (filePath: string) =>
'./' + ensureUnixPath(path.relative(destinationPath, filePath));

for (const entryPoint of entryPoints) {
const { destinationFiles, isSecondaryEntryPoint } = entryPoint.data.entryPoint;
exports[isSecondaryEntryPoint ? `./${destinationFiles.directory}` : '.'] = {
types: relativeUnixFromDestPath(destinationFiles.declarations),
default: relativeUnixFromDestPath(destinationFiles.fesm2020),
};
}

return exports;
}

0 comments on commit 047e3ca

Please sign in to comment.