-
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/build): warn when
@angular/localize/init
is imported d…
…irectly Importing `@angular/localize/init` directly can cause unpredictable behavior, as highlighted in multiple reports: - angular/angular#59422 - angular/angular#48545 - angular/angular#59405 This update introduces a warning to alert developers of the potential risks associated with direct imports.
- Loading branch information
1 parent
22bd88c
commit bfe9ee3
Showing
2 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
packages/angular/build/src/tools/esbuild/angular-localize-init-warning-plugin.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,53 @@ | ||
/** | ||
* @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.dev/license | ||
*/ | ||
|
||
import type { Plugin } from 'esbuild'; | ||
|
||
const NG_LOCALIZE_RESOLUTION = Symbol('NG_LOCALIZE_RESOLUTION'); | ||
|
||
/** | ||
* This plugin addresses an issue where '@angular/localize/init' is directly imported, | ||
* potentially resulting in undefined behavior. By detecting such imports, the plugin | ||
* issues a warning and suggests including '@angular/localize/init' as a polyfill. | ||
* | ||
* @returns An esbuild plugin. | ||
*/ | ||
export function createAngularLocalizeInitWarningPlugin(): Plugin { | ||
return { | ||
name: 'angular-localize-init-warning', | ||
setup(build) { | ||
build.onResolve({ filter: /^@angular\/localize\/init/ }, async (args) => { | ||
if (args.pluginData?.[NG_LOCALIZE_RESOLUTION]) { | ||
return null; | ||
} | ||
|
||
const { importer, kind, resolveDir, namespace, pluginData = {} } = args; | ||
pluginData[NG_LOCALIZE_RESOLUTION] = true; | ||
|
||
const result = await build.resolve(args.path, { | ||
importer, | ||
kind, | ||
namespace, | ||
pluginData, | ||
resolveDir, | ||
}); | ||
|
||
return { | ||
...result, | ||
warnings: [ | ||
...result.warnings, | ||
{ | ||
text: `Direct import of '@angular/localize/init' detected. This may lead to undefined behavior.`, | ||
notes: [{ text: `Include '@angular/localize/init' as a polyfill instead.` }], | ||
}, | ||
], | ||
}; | ||
}); | ||
}, | ||
}; | ||
} |
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