Skip to content

Commit 11535bb

Browse files
authored
feat: add "sideEffects": false flag to dist-ready package.json (#776)
1 parent e463c99 commit 11535bb

File tree

5 files changed

+36
-5
lines changed

5 files changed

+36
-5
lines changed

integration/samples/apf/specs/package.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ describe(`@sample/apf`, () => {
5656
it(`should reference "metadata" file`, () => {
5757
expect(PACKAGE['metadata']).to.equal('sample-apf.metadata.json');
5858
});
59+
60+
it(`should apply the 'sideEffects: false' flag by default`, () => {
61+
expect(PACKAGE['sideEffects']).to.be.false;
62+
});
5963
});
6064

6165
describe(`secondary/package.json`, () => {
@@ -111,5 +115,9 @@ describe(`@sample/apf`, () => {
111115
it(`should reference "metadata" file`, () => {
112116
expect(PACKAGE['metadata']).to.equal('sample-apf-secondary.metadata.json');
113117
});
118+
119+
it(`should apply the 'sideEffects: false' flag by default`, () => {
120+
expect(PACKAGE['sideEffects']).to.be.false;
121+
});
114122
});
115123
});

integration/samples/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"version": "1.0.0-pre.0",
55
"private": true,
66
"repository": "https://github.com/dherges/ng-packagr.git",
7+
"sideEffects": true,
78
"peerDependencies": {
89
"@angular/common": "^4.1.3",
910
"@angular/core": "^4.1.3",

integration/samples/core/specs/package.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@ describe(`@sample/core`, () => {
3434
it(`should have 'scripts' section removed`, () => {
3535
expect(PACKAGE['scripts']).to.be.undefined;
3636
});
37+
38+
it(`should keep the 'sideEffects: true' flag`, () => {
39+
expect(PACKAGE['sideEffects']).to.be.true;
40+
});
3741
});
3842
});

src/lib/ng-package-format/entry-point.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ import { DirectoryPath, SourceFilePath, CssUrl, DestinationFiles } from './share
3434
*/
3535
export class NgEntryPoint {
3636
constructor(
37+
/** Values from the `package.json` file of this entry point. */
3738
public readonly packageJson: any,
39+
/** Values from either the `ngPackage` option (from `package.json`) or values from `ng-package.json`. */
3840
public readonly ngPackageJson: NgPackageConfig,
41+
/** Corresponding JSON schema class instantiated from `ngPackageJson` values. */
3942
private readonly $schema: SchemaClass<NgPackageConfig>,
43+
/** Absolute directory path of this entry point's `package.json` location. */
4044
public readonly basePath: string,
45+
/** XX: additional auto-configured data passed for scondary entry point's. Needs better docs. */
4146
private readonly secondaryData?: { [key: string]: any }
4247
) {}
4348

@@ -154,4 +159,15 @@ export class NgEntryPoint {
154159
return this.moduleId.split('/').join(separator);
155160
}
156161
}
162+
163+
/**
164+
* Enables the `"sideEffects": false` flag in `package.json`.
165+
* The flag is enabled and set to `false` by default which results in more aggressive optimizations applied by webpack v4 builds consuming the library.
166+
* To override the default behaviour, you need to set `"sideEffects": true` explicitly in your `package.json`.
167+
*
168+
* @link https://github.com/webpack/webpack/tree/master/examples/side-effects
169+
*/
170+
public get hasSideEffects(): boolean {
171+
return this.packageJson['sideEffects'] === true;
172+
}
157173
}

src/lib/ng-v5/entry-point/write-package.transform.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export const writePackageTransform: Transform = transformFromPromise(async graph
3434
fesm2015: relativeUnixFromDestPath(destinationFiles.fesm2015),
3535
typings: relativeUnixFromDestPath(destinationFiles.declarations),
3636
// XX 'metadata' property in 'package.json' is non-standard. Keep it anyway?
37-
metadata: relativeUnixFromDestPath(destinationFiles.metadata)
37+
metadata: relativeUnixFromDestPath(destinationFiles.metadata),
38+
// webpack v4+ specific flag to enable advanced optimizations and code splitting
39+
sideEffects: ngEntryPoint.hasSideEffects
3840
});
3941

4042
log.success(`Built ${ngEntryPoint.moduleId}`);
@@ -55,18 +57,18 @@ export const writePackageTransform: Transform = transformFromPromise(async graph
5557
* flattened JavaScript bundles, type definitions, (...).
5658
*
5759
* @param entryPoint An entry point of an Angular package / library
58-
* @param binaries Binary artefacts (bundle files) to merge into `package.json`
60+
* @param additionalProperties Additional properties, e.g. binary artefacts (bundle files), to merge into `package.json`
5961
*/
6062
export async function writePackageJson(
6163
entryPoint: NgEntryPoint,
6264
pkg: NgPackage,
63-
binaries: { [key: string]: string }
65+
additionalProperties: { [key: string]: string | boolean }
6466
): Promise<void> {
6567
log.debug('Writing package.json');
6668
const packageJson: any = entryPoint.packageJson;
6769
// set additional properties
68-
for (const fieldName in binaries) {
69-
packageJson[fieldName] = binaries[fieldName];
70+
for (const fieldName in additionalProperties) {
71+
packageJson[fieldName] = additionalProperties[fieldName];
7072
}
7173

7274
// read tslib version from `@angular/compiler` so that our tslib

0 commit comments

Comments
 (0)