-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(@angular-devkit/build-angular): treeshake unused class that use c…
…ustom decorators This changes enables wrapping classes in side-effect free modules that make use of custom decorators when using the esbuild based builders so that when such classes are unused they can be treeshaken.
- Loading branch information
1 parent
6473b01
commit 7b9b7fe
Showing
4 changed files
with
89 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/legacy-cli/e2e/tests/build/library/lib-unused-decorated-class-treeshake.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import assert from 'assert'; | ||
import { appendToFile, expectFileToExist, expectFileToMatch, readFile } from '../../../utils/fs'; | ||
import { ng } from '../../../utils/process'; | ||
import { libraryConsumptionSetup } from './setup'; | ||
import { updateJsonFile } from '../../../utils/project'; | ||
import { expectToFail } from '../../../utils/utils'; | ||
|
||
export default async function () { | ||
await ng('cache', 'off'); | ||
await libraryConsumptionSetup(); | ||
|
||
// Add an unused class as part of the public api. | ||
await appendToFile( | ||
'projects/my-lib/src/public-api.ts', | ||
` | ||
function something() { | ||
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) { | ||
console.log("someDecorator"); | ||
}; | ||
} | ||
export class ExampleClass { | ||
@something() | ||
method() {} | ||
} | ||
`, | ||
); | ||
|
||
// build the lib | ||
await ng('build', 'my-lib', '--configuration=production'); | ||
const packageJson = JSON.parse(await readFile('dist/my-lib/package.json')); | ||
assert.equal(packageJson.sideEffects, false); | ||
|
||
// build the app | ||
await ng('build', 'test-project', '--configuration=production', '--output-hashing=none'); | ||
// Output should not contain `ExampleClass` as the library is marked as side-effect free. | ||
await expectFileToExist('dist/test-project/browser/main.js'); | ||
await expectToFail(() => expectFileToMatch('dist/test-project/browser/main.js', 'someDecorator')); | ||
|
||
// Mark library as side-effectful. | ||
await updateJsonFile('dist/my-lib/package.json', (packageJson) => { | ||
packageJson.sideEffects = true; | ||
}); | ||
|
||
// build the app | ||
await ng('build', 'test-project', '--configuration=production', '--output-hashing=none'); | ||
// Output should contain `ExampleClass` as the library is marked as side-effectful. | ||
await expectFileToMatch('dist/test-project/browser/main.js', 'someDecorator'); | ||
} |