nhsuk / nhsuk-frontend

NHS.UK frontend contains the code you need to start building user interfaces for NHS websites and services.
https://nhsuk.github.io/nhsuk-frontend/
MIT License
606 stars 106 forks source link

Component JS files are only available in ESM syntax #937

Open jakeb-nhs opened 3 months ago

jakeb-nhs commented 3 months ago

What is the issue?

The component JS files available within the packages folder of the nhsuk-frontend distribution are only available in ESM syntax. This causes an issue when the consuming application is not using the ESM module type (but for instance uses CJS instead).

In order to support CJS applications there should be CJS versions of these JS files available, either instead of ESM, or in addition to ESM (by using different entrypoints via package.json).

What steps are required to reproduce the issue?

Import one of the JS files in an application which requires CJS for it's dependencies. E.g.

import HeaderJs from 'nhsuk-frontend/packages/components/header/header';

The CJS loader will complain about unexpected export tokens - this is because it cannot handle ESM syntax.

Current workaround

The JS files need to be copied into the consuming application (rather than referencing the nhsuk-frontend dependency) so that they can be transpiled to CJS syntax when needed.