question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Dynamic import of locales not available in Angular 13

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

common

Is this a regression?

Yes

Description

While in a previous version of angular, you could import locales and their extra’s dynamically, it is impossible in angular 13. Below there is a sample of code with Webpack magic comments that lazy-load of locales was possible.

import(
  /* webpackExclude: /\.d\.ts$/ */
  /* webpackMode: "lazy-once" */
  /* webpackChunkName: "i18n-base" */
  `@angular/common/locales/${id}.mjs`)
.then(m => m.default)
.catch(error => {
   .....
});

Now Webpack throws an error because exports attribute seems to be misconfigured in package.json

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Error: Module not found: Error: Package path ./locales is not exported from package .../node_modules/@angular/common (see exports field in .../node_modules/@angular/common/package.json)

Please provide the environment you discovered this bug in

Angular CLI: 13.0.1
Node: 12.22.6
Package Manager: yarn 1.22.15
OS: linux x64

Angular: 13.0.0
... animations, cdk, common, compiler, compiler-cli, core
... elements, forms, language-service, material
... material-moment-adapter, platform-browser
... platform-browser-dynamic, router, service-worker

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1300.1
@angular-devkit/build-angular   13.0.1
@angular-devkit/core            13.0.1
@angular-devkit/schematics      13.0.1
@angular/cli                    13.0.1
@schematics/angular             13.0.1
ng-packagr                      13.0.3
rxjs                            7.4.0
typescript                      4.4.4

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8

github_iconTop GitHub Comments

3reactions
geo242commented, Nov 23, 2021

Here is what is currently working for me with v13. I use this in a library and I can verify that the consuming applications of this library are getting the appropriate locale files built. I only want certain locals included.

import(
  /* webpackInclude: /(en|es|id)\.mjs$/ */
  `/node_modules/@angular/common/locales/${locale}.mjs`
  ).then(module => {
  registerLocaleData(module.default);
});```
2reactions
crh225commented, Nov 30, 2021

@geo242 answer worked for me. by adding the /node_modules/ in front of @angular/common

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dynamic Import of Locales in Angular | by Michael Karén
We are trying to import too many files, some of which break the build. If we look in the folder 'node_modules\@angular\common\locales' we see...
Read more >
How do I dynamically import locales in Angular 9?
The excellent article mentioned by Eliseo's comment has the answer. Typescript's import function is not an ordinary function call.
Read more >
Import global variants of the locale data - Angular
Import global variants of the locale datalink ... The Angular CLI automatically includes locale data if you run the ng build command with...
Read more >
Angular Dynamic Locale - StackBlitz
Starter project for Angular apps that exports to the Angular CLI.
Read more >
Lazy Loading Locales with Angular - ANGULARarchitects
Angular 9 introduces a new implementation for its built-in I18N support called @angular/localize . One concept it contains is called global ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found