Skip to content

Commit 5922cb1

Browse files
DavidParks8dherges
authored andcommitted
feat: dynamic secondary entry points
1 parent bbb7c47 commit 5922cb1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+766
-476
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,5 @@ jspm_packages/
6363

6464
# Angular Build directory
6565
.ng_build/
66+
.ng_pkg_build/
6667
dist/

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,56 @@ in the `ngPackage` field:
7979

8080
Note: the JSON `$schema` reference enables JSON editing support (autocompletion) for the custom `ngPackage` property in an IDE like [VSCode](https://github.com/Microsoft/vscode).
8181

82+
#### Secondary Entry-Points
83+
84+
Besides the primary entry point, a package can contain one or more secondary entry points (e.g. `@angular/core/testing`).
85+
These contain symbols that we don't want to group together with the symbols in the main entry point.
86+
Module ID of an import for a secondary entry point directs a module loader to a directory by the secondary entry point’s name.
87+
For instance, “@angular/core/testing” resolves to a directory by the same name, “@angular/core/testing”.
88+
This directory contains a package.json file that directs the loader to the correct location for what it’s looking for.
89+
To that end, secondary entry points are dynamically discovered by searching for `package.json`
90+
files within sub directories of the main `package.json` file's folder.
91+
92+
##### So how do I use it?
93+
94+
All you have to do is create a `package.json` file and put it where you want a secondary entry
95+
point to be created. One way this can be done is by mimicking the folder structure of the
96+
following example which has a testing entry point in addition to it's main entry point.
97+
98+
```
99+
src
100+
├── testing
101+
│ ├── *.ts
102+
│ ├── public_api.ts
103+
│ └── package.json
104+
├── *.ts
105+
├── public_api.ts
106+
├── ng-package.json
107+
└── package.json
108+
```
109+
110+
The contents of the secondary `package.json` can be as simple as:
111+
```json
112+
{}
113+
```
114+
115+
No, that is not a typo. No name is required. No version is required. It's all handled for you by ng-packagr!
116+
When built, the secondary bundles would be accessible as `$(your-primary-package-name)/testing`.
117+
##### What if I don't like `public_api.ts`?
118+
119+
You can change the entry point file by using the `ngPackage` configuration field in your secondary `package.json`.
120+
For example, the following would use `index.ts` as the secondary entry point:
121+
122+
```json
123+
{
124+
"ngPackage": {
125+
"lib": {
126+
"entryFile": "index.ts"
127+
}
128+
}
129+
}
130+
```
131+
82132
#### More Examples
83133

84134
Nikolas LeBlanc has written a story on medium.com on [Building an Angular 4 Component Library with the Angular CLI and ng-packagr](https://medium.com/@ngl817/building-an-angular-4-component-library-with-the-angular-cli-and-ng-packagr-53b2ade0701e)
@@ -99,6 +149,8 @@ We keep track of user questions in GitHub's issue tracker and try to build a doc
99149
- :dancer: Creates type definitions (`.d.ts`)
100150
- :runner: Generates [Ahead-of-Time](https://angular.io/guide/aot-compiler#why-do-aot-compilation) metadata (`.metadata.json`)
101151
- :surfer: Inlines Templates and Stylesheets
152+
- :trophy: Supports dynamic discovery and bundling of secondary entry points
153+
- :mag_right: Supports scoped, and non-scoped packages
102154
- :sparkles: CSS Features
103155
- :camel: Runs [SCSS](http://sass-lang.com/guide) preprocessor, supporting the [relative `~` import syntax](https://github.com/webpack-contrib/sass-loader#imports)
104156
- :elephant: Runs [less](http://lesscss.org/#getting-started) preprocessor

integration/samples.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const fs = require('fs');
1+
const fs = require('fs-extra');
22
const path = require('path');
33
process.env.DEBUG = true;
44

integration/samples/custom/specs/package.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ describe(`sample-custom`, () => {
2929
expect(PACKAGE['main']).to.equal('bundles/sample-custom.umd.js');
3030
});
3131

32-
it(`should reference "module" bundle (FESM5, also FESM2014)`, () => {
33-
expect(PACKAGE['module']).to.equal('sample-custom/sample-custom.es5.js');
32+
it(`should reference "module" bundle (FESM5, also FESM2015)`, () => {
33+
expect(PACKAGE['module']).to.equal('sample-custom.es5.js');
3434
});
3535

3636
it(`should reference "es2015" bundle (FESM2015)`, () => {
37-
expect(PACKAGE['es2015']).to.equal('sample-custom/sample-custom.js');
37+
expect(PACKAGE['es2015']).to.equal('sample-custom.js');
3838
});
3939

4040
it(`should reference "typings" files`, () => {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
last 2 Chrome versions
2+
iOS > 10
3+
Safari > 10
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Sample library: Configuration in package.json
2+
======================================
3+
4+
The configuration in this sample resides inside `package.json`.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h1 class="baz">baz!</h1>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import '~node-sass/test/fixtures/include-files/file-not-processed-by-loader';
2+
3+
.baz {
4+
color: $variable-defined-by-file-not-processed-by-loader;
5+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'baz-component',
5+
templateUrl: './baz.component.html',
6+
styleUrls: ['./baz.component.scss']
7+
})
8+
export class BazComponent {
9+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "@sample/secondary-lib",
3+
"description": "A sample library with configuration in package.json",
4+
"version": "1.0.0-pre.0",
5+
"private": true,
6+
"repository": "https://github.com/dherges/ng-packagr.git",
7+
"peerDependencies": {
8+
"@angular/core": "^4.1.2",
9+
"@angular/common": "^4.1.2"
10+
},
11+
"ngPackage": {
12+
"$schema": "../../../src/lib/ng-package.schema.json",
13+
"lib": {
14+
"entryFile": "public_api.ts"
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)