Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack import(@angular/common/locales/${mylocale}) not working #22088

Closed
leon opened this issue Nov 4, 2021 · 15 comments
Closed

webpack import(@angular/common/locales/${mylocale}) not working #22088

leon opened this issue Nov 4, 2021 · 15 comments

Comments

@leon
Copy link
Contributor

leon commented Nov 4, 2021

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

common

Is this a regression?

Yes

Description

We load our locales dynamically using a dynamic import and registerLocaleData

this works in angular 12

const localeModule = await import(
  /* webpackInclude: /(sv|nb|da|fi|de|fr)/ */
  /* webpackChunkName: "locale" */
  `@angular/common/locales/${this.locale}.js`
)
registerLocaleData(localeModule.default)

The Problem

I found the root cause and it's not a angular problem, but a webpack one
webpack/webpack#13865

But I though someone else might get stuck on the same thing, so I would add it as a regression.

Workaround

A workaround is to use a relative or absolute path to the node_modules folder instead

const localeModule = await import(
  /* webpackInclude: /(sv|nb|da|fi|de|fr).mjs$/ */
  /* webpackExports: "default" */
  /* webpackChunkName: "locale" */
  `node_modules/@angular/common/locales/${this.locale}`
)
registerLocaleData(localeModule.default)

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

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

Please provide the environment you discovered this bug in

No response

Anything else?

No response

@alan-agius4 alan-agius4 transferred this issue from angular/angular Nov 4, 2021
@alan-agius4 alan-agius4 added freq1: low Only reported by a handful of users who observe it rarely severity5: regression state: blocked on upstream type: bug/fix area: devkit/build-angular and removed freq1: low Only reported by a handful of users who observe it rarely labels Nov 4, 2021
@ngbot ngbot bot added this to the Backlog milestone Nov 4, 2021
@StanislavByzov
Copy link

I'm facing the same issue

@StanislavByzov
Copy link

This answer helped me highlightjs/highlight.js#3223 (comment)

@lmoralejo
Copy link

This answer helped me highlightjs/highlight.js#3223 (comment)

It helped me too.

@alan-agius4
Copy link
Collaborator

It's important to point out that using Webpack specific features such as magic comments above are not supported by the Angular tooling team. The fact that the Angular CLI is using Webpack is an implementation detail which can change in the future.

@jcompagner
Copy link

This answer helped me highlightjs/highlight.js#3223 (comment)

this for me doesn't work...
if i do this:

        const module1 =  `@angular/common/locales/${localeId}.js`;
        import(module1).then(

then yes it compiles, but i got a warning:
Warning: Critical dependency: the request of a dependency is an expression

and also the locale files are not generated (no output for the angular locales)

if i do this:

import(../../node_modules/@angular/common/locales/${localeId}.js)

which other say you now should do, that also doesn't work because yes then the files are generated but the actual import at runtime doesn't work...

image

as you can see the import askes for "./nl.js" but the map doesn't have that there is a "./global/" in front of it. so it doesn't map and can't load it..

So how can i get this working? or is this just not possible right now and i need to postpone the angular 13 upgrade?

@jcompagner
Copy link

Worked around that:

import(../../node_modules/@angular/common/locales/global/${localeId}.js)

so i really need to take them directly from the global dir itself, then the "/global/" prefix is gone.
and it maps it just fine.

@jcompagner
Copy link

it seems that we blame a WebPack bug right?
But why is it that these 2 that i also use:

import(numbro/languages/${localeId}).
import(@uppy/locales/lib/${localeId}.js)

are still working like a charm?
no need to change any code for that?

@geo242
Copy link

geo242 commented 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);
});```

@adampasc
Copy link

adampasc commented Dec 3, 2021

Is there any chance this will be fixed with one of the next releases? Our team also has to postpone angular13 migration due to this. None of the above workarounds seem to get the job done for us.

@alan-agius4
Copy link
Collaborator

@adampasc, this issue is not caused by Angular, but rather it’s a Webpack bug.

Please see: webpack/webpack#13865

@jcompagner
Copy link

@adampasc, this issue is not caused by Angular, but rather it’s a Webpack bug.

Please see: webpack/webpack#13865

i find this comment really weird
We do that special import of locale files not only for angular but also for a few others...
we do the same for numbro:

return import(numbro/languages/${localeId})

for Uppy:

import(@uppy/locales/lib/${localeId}.js)

also for CKEditor but that works a bit different

and all those still are working fine, no need to change anything, its purely the angular locales that with the upgrade to 13 that are suddenly not working in the above way anymore.

@alan-agius4
Copy link
Collaborator

This is because dynamic imports don't work in Webpack when an exports with wildcard is specified in package.json

Example

  "exports": {
    "./locales/global/*": {
      "default": "./locales/global/*.js"
    },
    "./locales/*": {
      "default": "./locales/*.mjs"
    },

@StanislavByzov
Copy link

This approach worked for me:
import('node_modules/@angular/common/locales/${this.locale}.mjs');

@alan-agius4
Copy link
Collaborator

Closing since this is not actionable from our end as this is a Webpack bug. Please see: webpack/webpack#13865

@alan-agius4 alan-agius4 closed this as not planned Won't fix, can't repro, duplicate, stale May 27, 2022
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jun 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants