-
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): properly handle locally-built APF…
… v14 libraries Locally-built APF v14 libraries should be resolved properly. Webpack currently does not resolve them (in e.g. `dist/`) because the local distribution folders are not marked as module roots, causing Webpack to never hit the `module`/`raw-module` resolution hooks and therefore skipping package exports resolution and breaking secondary entry-points from being resolved properly (when bundling). We fix this by also attempting to resolve path mappings as modules, allowing for Webpacks `resolve-in-package` hooks to be activated. These hooks support the `exports` field and therefore APF v14 secondary entry-points which are not necessarily inside a Webpack resolve `modules:` root (but e.g. in `dist/`) (cherry picked from commit ba93117)
- Loading branch information
1 parent
b0c3ced
commit ac1383f
Showing
2 changed files
with
143 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import { createDir, writeFile } from '../../utils/fs'; | ||
import { ng } from '../../utils/process'; | ||
import { updateJsonFile } from '../../utils/project'; | ||
|
||
export default async function () { | ||
await ng('generate', 'library', 'mylib'); | ||
await createLibraryEntryPoint('secondary', 'SecondaryModule', 'index.ts'); | ||
await createLibraryEntryPoint('another', 'AnotherModule', 'index.ts'); | ||
|
||
// Scenario #1 where we use wildcard path mappings for secondary entry-points. | ||
await updateJsonFile('tsconfig.json', (json) => { | ||
json.compilerOptions.paths = { 'mylib': ['dist/mylib'], 'mylib/*': ['dist/mylib/*'] }; | ||
}); | ||
|
||
await writeFile( | ||
'src/app/app.module.ts', | ||
` | ||
import {NgModule} from '@angular/core'; | ||
import {BrowserModule} from '@angular/platform-browser'; | ||
import {SecondaryModule} from 'mylib/secondary'; | ||
import {AnotherModule} from 'mylib/another'; | ||
import {AppComponent} from './app.component'; | ||
@NgModule({ | ||
declarations: [ | ||
AppComponent | ||
], | ||
imports: [ | ||
SecondaryModule, | ||
AnotherModule, | ||
BrowserModule | ||
], | ||
providers: [], | ||
bootstrap: [AppComponent] | ||
}) | ||
export class AppModule { } | ||
`, | ||
); | ||
|
||
await ng('build', 'mylib'); | ||
await ng('build'); | ||
|
||
// Scenario #2 where we don't use wildcard path mappings. | ||
await updateJsonFile('tsconfig.json', (json) => { | ||
json.compilerOptions.paths = { | ||
'mylib': ['dist/mylib'], | ||
'mylib/secondary': ['dist/mylib/secondary'], | ||
'mylib/another': ['dist/mylib/another'], | ||
}; | ||
}); | ||
|
||
await ng('build'); | ||
} | ||
|
||
async function createLibraryEntryPoint(name: string, moduleName: string, entryFileName: string) { | ||
await createDir(`projects/mylib/${name}`); | ||
await writeFile( | ||
`projects/mylib/${name}/${entryFileName}`, | ||
` | ||
import {NgModule} from '@angular/core'; | ||
@NgModule({}) | ||
export class ${moduleName} {} | ||
`, | ||
); | ||
|
||
await writeFile( | ||
`projects/mylib/${name}/ng-package.json`, | ||
JSON.stringify({ | ||
lib: { | ||
entryFile: entryFileName, | ||
}, | ||
}), | ||
); | ||
} |