@@ -30,12 +30,42 @@ Try it out on the [demo page](https://hl7.github.io/fhirpath.js/).
3030npm install --save fhirpath
3131```
3232
33+ The library ships both CommonJS and ES module entry points.
34+
35+ For CommonJS:
3336``` js
3437const fhirpath = require (' fhirpath' );
3538// For FHIR model data (choice type support) pull in the model file:
3639const fhirpath_r4_model = require (' fhirpath/fhir-context/r4' );
3740```
3841
42+ For ESM:
43+ ``` js
44+ import fhirpath from ' fhirpath' ;
45+ // Named exports are also available, e.g. `import { evaluate } from 'fhirpath'`.
46+ // For FHIR model data (choice type support) pull in the model file:
47+ import fhirpath_r4_model from ' fhirpath/fhir-context/r4' ;
48+ ```
49+
50+ The ` import ` entry point resolves to a pre-bundled, self-contained ES module
51+ that inlines fhirpath's CommonJS dependencies. This means bundlers such as
52+ esbuild (used by the Angular CLI) will not emit "CommonJS or AMD dependencies
53+ can cause optimization bailouts" warnings for fhirpath.
54+
55+ Do not mix ` require('fhirpath') ` and ` import 'fhirpath' ` for the same package
56+ within a single application. The ` import ` entry point is a self-contained
57+ bundle with its own copy of the library and its dependencies, so values created
58+ via one entry point (e.g. ` FP_Decimal ` or other internal type instances) are
59+ not ` instanceof ` -compatible with the other, and helpers such as ` ucumUtils `
60+ would be duplicated. Pick one module system per application.
61+
62+ The ` esm/fhirpath.mjs ` bundle is generated by the package's ` prepare ` lifecycle
63+ script (` npm run build:esm ` ) and is included in the published npm package, so
64+ installing fhirpath from the npm registry needs no extra step. If you instead
65+ install it from a git reference with lifecycle scripts disabled (e.g.
66+ ` npm install --ignore-scripts ` ), run ` npm run build:esm ` afterward so the
67+ ` import ` entry point (` esm/fhirpath.mjs ` ) exists.
68+
3969### Web-browser:
4070
4171Download the zip file from the [ releases
@@ -715,14 +745,22 @@ open browser on localhost:8080
715745 ` ` `
716746* Compare the output files in the new folder to those of the last release
717747 (looking for issues that might be due to changes in the StructureDefinition format)
718- * Copy the ` index .js ` file from the last release into the new folder
748+ * Copy the CommonJS and ES module entry files from the last release into the
749+ new folder
719750 ` ` `
720- > cp ../ r5/ index .js r6
751+ > cp ../ r5/ index .js .. / r5 / index . mjs .. / r5 / index . d . ts .. / r5 / index . d . mts r6
721752 ` ` `
722- * Update the ` / index .d .ts ` file to include the new module as an export (can copy from previous version)
723- ` ` ` js
724- declare module " fhirpath/fhir-context/r6" {
725- export const {
753+ * Update the header comment (version name) in ` r6/ index .mjs ` and
754+ ` r6/ index .d .mts ` . If the new version has no ` score` property, also remove
755+ ` score` from the named-export lists in ` r6/ index .mjs ` and ` r6/ index .d .ts ` .
756+ * ` r6/ index .d .ts ` declares the model's named exports for CommonJS consumers
757+ (copy from the previous version, shown below); ` r6/ index .d .mts ` re-exports
758+ them for ES module consumers via ` export * from " ./index.js" ` and needs no
759+ per-version changes.
760+ ` ` ` ts
761+ import {Model } from " ../../src/fhirpath" ;
762+
763+ export const {
726764 version ,
727765 score ,
728766 choiceTypePaths ,
@@ -733,8 +771,22 @@ open browser on localhost:8080
733771 resourcesWithUrlParam ,
734772 path2TypeWithoutElements ,
735773 path2RefType
736- }: Model;
737- }
774+ }: Model;
775+ ` ` `
776+ * Register the new version in the ` exports ` map in ` package .json ` so it can be
777+ imported by name through both ` import ` and ` require` (copy an existing
778+ ` ./fhir-context/<version>` entry and change the version)
779+ ` ` ` json
780+ " ./fhir-context/r6" : {
781+ " import" : {
782+ " types" : " ./fhir-context/r6/index.d.mts" ,
783+ " default" : " ./fhir-context/r6/index.mjs"
784+ },
785+ " require" : {
786+ " types" : " ./fhir-context/r6/index.d.ts" ,
787+ " default" : " ./fhir-context/r6/index.js"
788+ }
789+ },
738790 ` ` `
739791
740792## Credits
0 commit comments