|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import type { NormalizedEntryPoint, NormalizedLibraryOptions } from '../types'; |
| 10 | +import { bundleEntryPoints } from './bundler'; |
| 11 | + |
| 12 | +describe('bundleEntryPoints', () => { |
| 13 | + const workspaceRoot = '/workspace/my-lib'; |
| 14 | + |
| 15 | + function createOptions(entryPoints: Map<string, NormalizedEntryPoint>): NormalizedLibraryOptions { |
| 16 | + return { |
| 17 | + workspaceRoot, |
| 18 | + projectRoot: workspaceRoot, |
| 19 | + packageName: 'my-lib', |
| 20 | + packageJson: { name: 'my-lib' }, |
| 21 | + outputPath: `${workspaceRoot}/dist`, |
| 22 | + deleteOutputPath: true, |
| 23 | + packageJsonPath: `${workspaceRoot}/package.json`, |
| 24 | + tsConfigPath: `${workspaceRoot}/tsconfig.lib.json`, |
| 25 | + entryPoints, |
| 26 | + inlineStyleLanguage: 'css', |
| 27 | + styleIncludePaths: [], |
| 28 | + assets: [], |
| 29 | + compilationMode: 'partial', |
| 30 | + declarationMap: false, |
| 31 | + allowedNonPeerDependencies: [], |
| 32 | + keepLifecycleScripts: false, |
| 33 | + watch: false, |
| 34 | + preserveSymlinks: false, |
| 35 | + progress: false, |
| 36 | + colors: false, |
| 37 | + cacheOptions: { |
| 38 | + enabled: false, |
| 39 | + basePath: '', |
| 40 | + path: '', |
| 41 | + cacheId: '', |
| 42 | + } as unknown as NormalizedLibraryOptions['cacheOptions'], |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + it('should tree-shake unexported internal types in .d.ts when using export *', async () => { |
| 47 | + const primaryEntry: NormalizedEntryPoint = { |
| 48 | + subpath: '.', |
| 49 | + name: 'my-lib', |
| 50 | + displayName: 'my-lib', |
| 51 | + bundleName: 'my-lib', |
| 52 | + entryFilePath: `${workspaceRoot}/src/public-api.ts`, |
| 53 | + isPrimary: true, |
| 54 | + }; |
| 55 | + const secondaryEntry: NormalizedEntryPoint = { |
| 56 | + subpath: './secondary', |
| 57 | + name: 'my-lib/secondary', |
| 58 | + displayName: 'my-lib/secondary', |
| 59 | + bundleName: 'my-lib-secondary', |
| 60 | + entryFilePath: `${workspaceRoot}/secondary/src/public-api.ts`, |
| 61 | + isPrimary: false, |
| 62 | + }; |
| 63 | + |
| 64 | + const entryPoints = new Map<string, NormalizedEntryPoint>([ |
| 65 | + ['my-lib', primaryEntry], |
| 66 | + ['my-lib/secondary', secondaryEntry], |
| 67 | + ]); |
| 68 | + |
| 69 | + const esmFiles = new Map<string, string>([ |
| 70 | + [`${workspaceRoot}/src/public-api.js`, 'export const a = 1;'], |
| 71 | + [`${workspaceRoot}/secondary/src/public-api.js`, 'export const b = 2;'], |
| 72 | + ]); |
| 73 | + |
| 74 | + const dtsFiles = new Map<string, string>([ |
| 75 | + [ |
| 76 | + `${workspaceRoot}/src/internal.d.ts`, |
| 77 | + 'export interface PublicA { id: string; }\ninterface UnexportedInternalA { secret: number; }', |
| 78 | + ], |
| 79 | + [ |
| 80 | + `${workspaceRoot}/src/public-api.d.ts`, |
| 81 | + 'export * from "./internal.js";\nexport declare const a = 1;', |
| 82 | + ], |
| 83 | + [ |
| 84 | + `${workspaceRoot}/secondary/src/internal.d.ts`, |
| 85 | + 'export interface PublicB { flag: boolean; }\ninterface UnexportedInternalB { hidden: string; }', |
| 86 | + ], |
| 87 | + [ |
| 88 | + `${workspaceRoot}/secondary/src/public-api.d.ts`, |
| 89 | + 'export * from "./internal.js";\nexport declare const b = 2;', |
| 90 | + ], |
| 91 | + ]); |
| 92 | + |
| 93 | + const output = await bundleEntryPoints( |
| 94 | + [ |
| 95 | + { entryPoint: primaryEntry, hasEsmChanges: true, hasDtsChanges: true }, |
| 96 | + { entryPoint: secondaryEntry, hasEsmChanges: true, hasDtsChanges: true }, |
| 97 | + ], |
| 98 | + esmFiles, |
| 99 | + dtsFiles, |
| 100 | + createOptions(entryPoints), |
| 101 | + ); |
| 102 | + |
| 103 | + expect(output.filesToEmit).toContain( |
| 104 | + jasmine.objectContaining({ |
| 105 | + path: 'types/my-lib.d.ts', |
| 106 | + contents: jasmine.stringContaining('PublicA'), |
| 107 | + }), |
| 108 | + ); |
| 109 | + expect(output.filesToEmit).not.toContain( |
| 110 | + jasmine.objectContaining({ |
| 111 | + path: 'types/my-lib.d.ts', |
| 112 | + contents: jasmine.stringContaining('UnexportedInternalA'), |
| 113 | + }), |
| 114 | + ); |
| 115 | + |
| 116 | + expect(output.filesToEmit).toContain( |
| 117 | + jasmine.objectContaining({ |
| 118 | + path: 'types/my-lib-secondary.d.ts', |
| 119 | + contents: jasmine.stringContaining('PublicB'), |
| 120 | + }), |
| 121 | + ); |
| 122 | + expect(output.filesToEmit).not.toContain( |
| 123 | + jasmine.objectContaining({ |
| 124 | + path: 'types/my-lib-secondary.d.ts', |
| 125 | + contents: jasmine.stringContaining('UnexportedInternalB'), |
| 126 | + }), |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should preserve declare global and module augmentations in side-effect .d.ts imports', async () => { |
| 131 | + const primaryEntry: NormalizedEntryPoint = { |
| 132 | + subpath: '.', |
| 133 | + name: 'my-lib', |
| 134 | + displayName: 'my-lib', |
| 135 | + bundleName: 'my-lib', |
| 136 | + entryFilePath: `${workspaceRoot}/src/public-api.ts`, |
| 137 | + isPrimary: true, |
| 138 | + }; |
| 139 | + const entryPoints = new Map<string, NormalizedEntryPoint>([['my-lib', primaryEntry]]); |
| 140 | + |
| 141 | + const esmFiles = new Map<string, string>([ |
| 142 | + [`${workspaceRoot}/src/public-api.js`, 'export const a = 1;'], |
| 143 | + ]); |
| 144 | + const dtsFiles = new Map<string, string>([ |
| 145 | + [ |
| 146 | + `${workspaceRoot}/src/globals.d.ts`, |
| 147 | + 'export {};\ndeclare global {\n interface Window {\n myCustomGlobal: boolean;\n }\n}\n', |
| 148 | + ], |
| 149 | + [ |
| 150 | + `${workspaceRoot}/src/augment.d.ts`, |
| 151 | + "declare module '@angular/core' {\n interface Component {\n augmentedProp?: string;\n }\n}\nexport {};\n", |
| 152 | + ], |
| 153 | + [ |
| 154 | + `${workspaceRoot}/src/public-api.d.ts`, |
| 155 | + 'import "./globals.js";\nexport * from "./augment.js";\nexport declare const a = 1;\n', |
| 156 | + ], |
| 157 | + ]); |
| 158 | + |
| 159 | + const output = await bundleEntryPoints( |
| 160 | + [{ entryPoint: primaryEntry, hasEsmChanges: true, hasDtsChanges: true }], |
| 161 | + esmFiles, |
| 162 | + dtsFiles, |
| 163 | + createOptions(entryPoints), |
| 164 | + ); |
| 165 | + |
| 166 | + const dtsFile = output.filesToEmit.find((f) => f.path === 'types/my-lib.d.ts'); |
| 167 | + expect(dtsFile?.contents).toContain('myCustomGlobal'); |
| 168 | + expect(dtsFile?.contents).toContain('augmentedProp'); |
| 169 | + }); |
| 170 | + |
| 171 | + it('should preserve legal comments in emitted FESM2022 .mjs bundles', async () => { |
| 172 | + const primaryEntry: NormalizedEntryPoint = { |
| 173 | + subpath: '.', |
| 174 | + name: 'my-lib', |
| 175 | + displayName: 'my-lib', |
| 176 | + bundleName: 'my-lib', |
| 177 | + entryFilePath: `${workspaceRoot}/src/public-api.ts`, |
| 178 | + isPrimary: true, |
| 179 | + }; |
| 180 | + const entryPoints = new Map<string, NormalizedEntryPoint>([['my-lib', primaryEntry]]); |
| 181 | + |
| 182 | + const esmFiles = new Map<string, string>([ |
| 183 | + [ |
| 184 | + `${workspaceRoot}/src/public-api.js`, |
| 185 | + '/*! @license MIT Custom Header */\nexport const VALUE = 42;', |
| 186 | + ], |
| 187 | + ]); |
| 188 | + const dtsFiles = new Map<string, string>([ |
| 189 | + [`${workspaceRoot}/src/public-api.d.ts`, 'export declare const VALUE = 42;'], |
| 190 | + ]); |
| 191 | + |
| 192 | + const output = await bundleEntryPoints( |
| 193 | + [{ entryPoint: primaryEntry, hasEsmChanges: true, hasDtsChanges: true }], |
| 194 | + esmFiles, |
| 195 | + dtsFiles, |
| 196 | + createOptions(entryPoints), |
| 197 | + ); |
| 198 | + |
| 199 | + expect(output.filesToEmit).toContain( |
| 200 | + jasmine.objectContaining({ |
| 201 | + path: 'fesm2022/my-lib.mjs', |
| 202 | + contents: jasmine.stringContaining('/*! @license MIT Custom Header */'), |
| 203 | + }), |
| 204 | + ); |
| 205 | + }); |
| 206 | +}); |
0 commit comments