@openmrs/esm-state 3.1.10-pre.92 → 3.1.12-pre.598
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -108
- package/dist/openmrs-esm-state.js +1 -1
- package/dist/openmrs-esm-state.js.map +1 -1
- package/docs/API.md +153 -0
- package/docs/interfaces/AppState.md +3 -0
- package/package.json +4 -5
- package/src/index.ts +1 -0
- package/src/state.ts +39 -0
- package/src/update.ts +16 -0
- package/typedoc.json +7 -0
- package/webpack.config.js +5 -11
- package/src/set-public-path.ts +0 -3
package/README.md
CHANGED
|
@@ -1,114 +1,11 @@
|
|
|
1
1
|
# openmrs-esm-state
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## What is this?
|
|
6
|
-
|
|
7
|
-
openmrs-esm-state is an [in-browser javascript module](https://github.com/openmrs/openmrs-rfc-frontend/blob/master/text/0002-modules.md)
|
|
8
|
-
that provides functions for managing OpenMRS state using [Unistore](https://github.com/developit/unistore#unistore).
|
|
3
|
+
openmrs-esm-state provides functions for managing OpenMRS state using
|
|
4
|
+
[Unistore](https://github.com/developit/unistore#unistore).
|
|
9
5
|
|
|
10
6
|
It also provides a global Unistore store called `app`.
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
```typescript
|
|
15
|
-
import { createGlobalStore } from '@openmrs/esm-state';
|
|
16
|
-
|
|
17
|
-
export interface BooksStore {
|
|
18
|
-
books: Array<string>;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
createGlobalStore("books", {
|
|
22
|
-
books: [],
|
|
23
|
-
});
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
```typescript
|
|
27
|
-
import { getGlobalStore } from '@openmrs/esm-state';
|
|
28
|
-
|
|
29
|
-
const booksStore = getGlobalStore("books");
|
|
30
|
-
console.log(booksStore.getState());
|
|
31
|
-
booksStore.subscribe(books => console.log(books));
|
|
32
|
-
booksStore.setState({ books: ["Pathologies of Power"]});
|
|
33
|
-
```
|
|
34
|
-
|
|
35
|
-
```typescript
|
|
36
|
-
import { getAppState } from '@openmrs/esm-state';
|
|
37
|
-
|
|
38
|
-
console.log(getAppState().getState());
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
In React:
|
|
42
|
-
|
|
43
|
-
```typescript
|
|
44
|
-
import React, { useEffect } from 'react';
|
|
45
|
-
import { getGlobalStore } from '@openmrs/esm-state';
|
|
46
|
-
|
|
47
|
-
function BookShelf() {
|
|
48
|
-
useEffect(() => {
|
|
49
|
-
function update(state) {
|
|
50
|
-
console.log(state);
|
|
51
|
-
}
|
|
52
|
-
const store = getGlobalStore("books");
|
|
53
|
-
// Use `getState` to run `update` on the current state.
|
|
54
|
-
update(store.getState());
|
|
55
|
-
// Use `subscribe` to run `update` on all future state updates.
|
|
56
|
-
// It returns an `unsubscribe` function. Return that function
|
|
57
|
-
// in `useEffect` so that it runs when the component unmounts.
|
|
58
|
-
return store.subscribe(update);
|
|
59
|
-
}, [])
|
|
60
|
-
}
|
|
61
|
-
```
|
|
62
|
-
|
|
63
|
-
Also see [connect](https://github.com/developit/unistore#connect) and
|
|
64
|
-
[Provider](https://github.com/developit/unistore#provider).
|
|
65
|
-
|
|
66
|
-
See [the Unistore docs](https://github.com/developit/unistore#unistore) for more
|
|
67
|
-
information about stores.
|
|
68
|
-
|
|
69
|
-
## Contributing / Development
|
|
70
|
-
|
|
71
|
-
[Instructions for local development](https://wiki.openmrs.org/display/projects/Setup+local+development+environment+for+OpenMRS+SPA)
|
|
72
|
-
|
|
73
|
-
## API
|
|
74
|
-
|
|
75
|
-
The following functions are exported from the `@openmrs/esm-state` module:
|
|
76
|
-
|
|
77
|
-
## createGlobalStore
|
|
78
|
-
|
|
79
|
-
```typescript
|
|
80
|
-
createGlobalStore<TState>(name: string, initialState: TState): Store<TState>
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
Creates a [store](https://github.com/developit/unistore#store).
|
|
84
|
-
|
|
85
|
-
##### Arguments
|
|
86
|
-
|
|
87
|
-
1. `name` (required): A name by which the store can be looked up later. Must be unique across the entire application.
|
|
88
|
-
2. `initialState` (required): An object which will be the initial state of the store.
|
|
89
|
-
|
|
90
|
-
##### Return value
|
|
91
|
-
|
|
92
|
-
The newly created store.
|
|
93
|
-
|
|
94
|
-
## getGlobalStore
|
|
95
|
-
|
|
96
|
-
```typescript
|
|
97
|
-
getGlobalStore<TState = any>(name: string, fallbackState?: TState): Store<TState>
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
Returns the existing [store](https://github.com/developit/unistore#store) named `name`,
|
|
101
|
-
or creates a new store named `name` if none exists.
|
|
102
|
-
|
|
103
|
-
##### Arguments
|
|
104
|
-
|
|
105
|
-
1. `name` (required): The name of the store to look up.
|
|
106
|
-
2. `fallbackState` (optional): The initial value of the new store if no store named `name` exists.
|
|
107
|
-
|
|
108
|
-
##### Return value
|
|
109
|
-
|
|
110
|
-
The found or newly created store.
|
|
111
|
-
|
|
112
|
-
## getAppState
|
|
8
|
+
[API Docs](docs/API.md)
|
|
113
9
|
|
|
114
|
-
|
|
10
|
+
Please see the Developer Documentation page on
|
|
11
|
+
[Managing State](https://openmrs.github.io/openmrs-esm-core/#/main/state).
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
System.register([],(function(e){return{execute:function(){
|
|
1
|
+
System.register([],(function(t,e){return{execute:function(){t((()=>{var t={968:(t,e,r)=>{const n=r(577).R;e.s=function(t){if(t||(t=1),!r.y.meta||!r.y.meta.url)throw console.error("__system_context__",r.y),Error("systemjs-webpack-interop was provided an unknown SystemJS context. Expected context.meta.url, but none was provided");r.p=n(r.y.meta.url,t)}},577:(t,e,r)=>{e.R=function(t,e){var r=document.createElement("a");r.href=t;for(var n="/"===r.pathname[0]?r.pathname:"/"+r.pathname,o=0,a=n.length;o!==e&&a>=0;)"/"===n[--a]&&o++;if(o!==e)throw Error("systemjs-webpack-interop: rootDirectoryLevel ("+e+") is greater than the number of directories ("+o+") in the URL path "+t);var i=n.slice(0,a+1);return r.protocol+"//"+r.host+i};Number.isInteger}},r={};function n(e){var o=r[e];if(void 0!==o)return o.exports;var a=r[e]={exports:{}};return t[e](a,a.exports,n),a.exports}n.y=e,n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(t){if("object"==typeof window)return window}}(),n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),n.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},(()=>{var t;n.g.importScripts&&(t=n.g.location+"");var e=n.g.document;if(!t&&e&&(e.currentScript&&(t=e.currentScript.src),!t)){var r=e.getElementsByTagName("script");r.length&&(t=r[r.length-1].src)}if(!t)throw new Error("Automatic publicPath is not supported in this browser");t=t.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),n.p=t})();var o={};return(0,n(968).s)(1),(()=>{"use strict";function t(t,e){for(var r in e)t[r]=e[r];return t}function e(e){var r=[];function n(t){for(var e=[],n=0;n<r.length;n++)r[n]===t?t=null:e.push(r[n]);r=e}function o(n,o,a){e=o?n:t(t({},e),n);for(var i=r,u=0;u<i.length;u++)i[u](e,a)}return e=e||{},{action:function(t){function r(e){o(e,!1,t)}return function(){for(var n=arguments,o=[e],a=0;a<arguments.length;a++)o.push(n[a]);var i=t.apply(this,o);if(null!=i)return i.then?i.then(r):r(i)}},setState:o,subscribe:function(t){return r.push(t),function(){n(t)}},unsubscribe:n,getState:function(){return e}}}n.r(o),n.d(o,{createAppState:()=>u,createGlobalStore:()=>a,getAppState:()=>c,getGlobalStore:()=>i,subscribeTo:()=>s,update:()=>l});const r={};function a(t,n){const o=r[t];if(o)return o.active?console.error("Cannot override an existing store. Make sure that stores are only created once."):o.value.setState(n,!0),o.active=!0,o.value;{const o=e(n);return r[t]={value:o,active:!0},o}}function i(t,n){const o=r[t];if(!o){const o=e(n);return r[t]={value:o,active:!1},o}return o.value}function u(t){return a("app",t)}function c(){return i("app",{})}function s(t,e,r){let n=e(t.getState());return t.subscribe((t=>{const o=e(t);o!==n&&(n=o,r(o))}))}function l(t,[e,...r],n){return e?0===r.length?{...t,[e]:n}:{...t,[e]:l(t[e]||{},r,n)}:t}})(),o})())}}}));
|
|
2
2
|
//# sourceMappingURL=openmrs-esm-state.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["webpack:///webpack/bootstrap","webpack:////home/runner/work/openmrs-esm-core/openmrs-esm-core/node_modules/systemjs-webpack-interop/public-path.js","webpack:///./src/set-public-path.ts","webpack:////home/runner/work/openmrs-esm-core/openmrs-esm-core/node_modules/unistore/dist/unistore.es.js","webpack:///./src/state.ts"],"names":["installedModules","__webpack_require__","moduleId","exports","module","i","l","modules","call","m","c","d","name","getter","o","Object","defineProperty","enumerable","get","r","Symbol","toStringTag","value","t","mode","__esModule","ns","create","key","bind","n","object","property","prototype","hasOwnProperty","p","s","resolveDirectory","urlString","rootDirectoryLevel","a","document","createElement","href","pathname","numDirsProcessed","index","length","Error","finalPath","slice","protocol","host","setPublicPath","systemjsModuleName","trim","isInteger","moduleUrl","window","System","resolve","err","Number","val","isFinite","Math","floor","u","push","e","f","action","arguments","apply","this","then","setState","subscribe","unsubscribe","getState","availableStores","createGlobalStore","initialState","available","active","console","error","store","createStore","getGlobalStore","fallbackState","createAppState","getAppState"],"mappings":"wEACE,IAAIA,EAAmB,GAGvB,SAASC,EAAoBC,GAG5B,GAAGF,EAAiBE,GACnB,OAAOF,EAAiBE,GAAUC,QAGnC,IAAIC,EAASJ,EAAiBE,GAAY,CACzCG,EAAGH,EACHI,GAAG,EACHH,QAAS,IAUV,OANAI,EAAQL,GAAUM,KAAKJ,EAAOD,QAASC,EAAQA,EAAOD,QAASF,GAG/D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AOC,GAG3B,SAASU,IACd,OAAOH,EAAyB,MAAO","file":"openmrs-esm-state.js","sourcesContent":[" \t// The module cache\n \tvar installedModules = {};\n\n \t// The require function\n \tfunction __webpack_require__(moduleId) {\n\n \t\t// Check if module is in cache\n \t\tif(installedModules[moduleId]) {\n \t\t\treturn installedModules[moduleId].exports;\n \t\t}\n \t\t// Create a new module (and put it into the cache)\n \t\tvar module = installedModules[moduleId] = {\n \t\t\ti: moduleId,\n \t\t\tl: false,\n \t\t\texports: {}\n \t\t};\n\n \t\t// Execute the module function\n \t\tmodules[moduleId].call(module.exports, module, module.exports, __webpack_require__);\n\n \t\t// Flag the module as loaded\n \t\tmodule.l = true;\n\n \t\t// Return the exports of the module\n \t\treturn module.exports;\n \t}\n\n\n \t// expose the modules object (__webpack_modules__)\n \t__webpack_require__.m = modules;\n\n \t// expose the module cache\n \t__webpack_require__.c = installedModules;\n\n \t// define getter function for harmony exports\n \t__webpack_require__.d = function(exports, name, getter) {\n \t\tif(!__webpack_require__.o(exports, name)) {\n \t\t\tObject.defineProperty(exports, name, { enumerable: true, get: getter });\n \t\t}\n \t};\n\n \t// define __esModule on exports\n \t__webpack_require__.r = function(exports) {\n \t\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n \t\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n \t\t}\n \t\tObject.defineProperty(exports, '__esModule', { value: true });\n \t};\n\n \t// create a fake namespace object\n \t// mode & 1: value is a module id, require it\n \t// mode & 2: merge all properties of value into the ns\n \t// mode & 4: return value when already ns object\n \t// mode & 8|1: behave like require\n \t__webpack_require__.t = function(value, mode) {\n \t\tif(mode & 1) value = __webpack_require__(value);\n \t\tif(mode & 8) return value;\n \t\tif((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;\n \t\tvar ns = Object.create(null);\n \t\t__webpack_require__.r(ns);\n \t\tObject.defineProperty(ns, 'default', { enumerable: true, value: value });\n \t\tif(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));\n \t\treturn ns;\n \t};\n\n \t// getDefaultExport function for compatibility with non-harmony modules\n \t__webpack_require__.n = function(module) {\n \t\tvar getter = module && module.__esModule ?\n \t\t\tfunction getDefault() { return module['default']; } :\n \t\t\tfunction getModuleExports() { return module; };\n \t\t__webpack_require__.d(getter, 'a', getter);\n \t\treturn getter;\n \t};\n\n \t// Object.prototype.hasOwnProperty.call\n \t__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };\n\n \t// __webpack_public_path__\n \t__webpack_require__.p = \"\";\n\n\n \t// Load entry module and return exports\n \treturn __webpack_require__(__webpack_require__.s = 1);\n","exports.setPublicPath = function setPublicPath(\n systemjsModuleName,\n rootDirectoryLevel\n) {\n if (!rootDirectoryLevel) {\n rootDirectoryLevel = 1;\n }\n if (\n typeof systemjsModuleName !== \"string\" ||\n systemjsModuleName.trim().length === 0\n ) {\n throw Error(\n \"systemjs-webpack-interop: setPublicPath(systemjsModuleName) must be called with a non-empty string 'systemjsModuleName'\"\n );\n }\n\n if (\n typeof rootDirectoryLevel !== \"number\" ||\n rootDirectoryLevel <= 0 ||\n !isInteger(rootDirectoryLevel)\n ) {\n throw Error(\n \"systemjs-webpack-interop: setPublicPath(systemjsModuleName, rootDirectoryLevel) must be called with a positive integer 'rootDirectoryLevel'\"\n );\n }\n\n let moduleUrl;\n try {\n moduleUrl = window.System.resolve(systemjsModuleName);\n if (!moduleUrl) {\n throw Error();\n }\n } catch (err) {\n throw Error(\n \"systemjs-webpack-interop: There is no such module '\" +\n systemjsModuleName +\n \"' in the SystemJS registry. Did you misspell the name of your module?\"\n );\n }\n\n __webpack_public_path__ = resolveDirectory(moduleUrl, rootDirectoryLevel);\n};\n\nfunction resolveDirectory(urlString, rootDirectoryLevel) {\n // Our friend IE11 doesn't support new URL()\n // https://github.com/single-spa/single-spa/issues/612\n // https://gist.github.com/jlong/2428561\n\n const a = document.createElement(\"a\");\n a.href = urlString;\n\n const pathname = a.pathname[0] === \"/\" ? a.pathname : \"/\" + a.pathname;\n let numDirsProcessed = 0,\n index = pathname.length;\n while (numDirsProcessed !== rootDirectoryLevel && index >= 0) {\n const char = pathname[--index];\n if (char === \"/\") {\n numDirsProcessed++;\n }\n }\n\n if (numDirsProcessed !== rootDirectoryLevel) {\n throw Error(\n \"systemjs-webpack-interop: rootDirectoryLevel (\" +\n rootDirectoryLevel +\n \") is greater than the number of directories (\" +\n numDirsProcessed +\n \") in the URL path \" +\n urlString\n );\n }\n\n const finalPath = pathname.slice(0, index + 1);\n\n return a.protocol + \"//\" + a.host + finalPath;\n}\n\nexports.resolveDirectory = resolveDirectory;\n\n// borrowed from https://github.com/parshap/js-is-integer/blob/master/index.js\nconst isInteger =\n Number.isInteger ||\n function isInteger(val) {\n return typeof val === \"number\" && isFinite(val) && Math.floor(val) === val;\n };\n","import { setPublicPath } from \"systemjs-webpack-interop\";\n\nsetPublicPath(\"@openmrs/esm-state\");\n","function n(n,t){for(var r in t)n[r]=t[r];return n}export default function(t){var r=[];function u(n){for(var t=[],u=0;u<r.length;u++)r[u]===n?n=null:t.push(r[u]);r=t}function e(u,e,f){t=e?u:n(n({},t),u);for(var i=r,o=0;o<i.length;o++)i[o](t,f)}return t=t||{},{action:function(n){function r(t){e(t,!1,n)}return function(){for(var u=arguments,e=[t],f=0;f<arguments.length;f++)e.push(u[f]);var i=n.apply(this,e);if(null!=i)return i.then?i.then(r):r(i)}},setState:e,subscribe:function(n){return r.push(n),function(){u(n)}},unsubscribe:u,getState:function(){return t}}}\n//# sourceMappingURL=unistore.es.js.map\n","import createStore, { Store } from \"unistore\";\n\ninterface StoreEntity {\n value: Store<any>;\n active: boolean;\n}\n\nconst availableStores: Record<string, StoreEntity> = {};\n\nexport function createGlobalStore<TState>(\n name: string,\n initialState: TState\n): Store<TState> {\n const available = availableStores[name];\n\n if (available) {\n if (available.active) {\n console.error(\n \"Cannot override an existing store. Make sure that stores are only created once.\"\n );\n } else {\n available.value.setState(initialState, true);\n }\n\n available.active = true;\n return available.value;\n } else {\n const store = createStore(initialState);\n\n availableStores[name] = {\n value: store,\n active: true,\n };\n\n return store;\n }\n}\n\nexport function getGlobalStore<TState = any>(\n name: string,\n fallbackState?: TState\n): Store<TState> {\n const available = availableStores[name];\n\n if (!available) {\n const store = createStore(fallbackState);\n availableStores[name] = {\n value: store,\n active: false,\n };\n return store;\n }\n\n return available.value;\n}\n\nexport interface AppState {}\n\nexport function createAppState(initialState: AppState) {\n return createGlobalStore(\"app\", initialState);\n}\n\nexport function getAppState() {\n return getGlobalStore<AppState>(\"app\", {});\n}\n"],"sourceRoot":""}
|
|
1
|
+
{"version":3,"file":"openmrs-esm-state.js","mappings":"yFAAA,MAAMA,EAAmB,SAEzBC,EAAQ,EAAiB,SAAwBC,GAY7C,GAXGA,IACHA,EAAe,IAUV,IAAmBC,OAAS,IAAmBA,KAAKC,IAEvD,MADAC,QAAQC,MAAM,qBAAsB,KAC9BC,MACJ,uHAIJ,IAA0BP,EACxB,IAAmBG,KAAKC,IACxBF,K,cCuDND,EAAQ,EAlCR,SAA0BO,EAAWC,GAKnC,IAAIC,EAAIC,SAASC,cAAc,KAC/BF,EAAEG,KAAOL,EAKT,IAHA,IAAIM,EAA6B,MAAlBJ,EAAEI,SAAS,GAAaJ,EAAEI,SAAW,IAAMJ,EAAEI,SACxDC,EAAmB,EACrBC,EAAQF,EAASG,OACZF,IAAqBN,GAAsBO,GAAS,GAE5C,MADFF,IAAWE,IAEpBD,IAIJ,GAAIA,IAAqBN,EACvB,MAAMF,MACJ,iDACEE,EACA,gDACAM,EACA,qBACAP,GAIN,IAAIU,EAAYJ,EAASK,MAAM,EAAGH,EAAQ,GAE1C,OAAON,EAAEU,SAAW,KAAOV,EAAEW,KAAOH,GAOpCI,OAAOC,YCjFLC,EAA2B,GAG/B,SAASC,EAAoBC,GAE5B,IAAIC,EAAeH,EAAyBE,GAC5C,QAAqBE,IAAjBD,EACH,OAAOA,EAAa1B,QA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O1B,OALAH,EAAgBE,GAAQ,CACtB3B,MAAO+B,EACPD,QAAQ,GAGHC,GAYJ,SAASE,EACdN,EACAO,GAEA,MAAML,EAAYJ,EAAgBE,GAElC,IAAKE,EAAW,CACd,MAAME,EAAQC,EAAYE,GAK1B,OAJAT,EAAgBE,GAAQ,CACtB3B,MAAO+B,EACPD,QAAQ,GAEHC,EAGT,OAAOF,EAAU7B,MAQZ,SAASmC,EAAeP,GAC7B,OAAOF,EAAkB,MAAOE,GAM3B,SAASQ,IACd,OAAOH,EAAyB,MAAO,IAGlC,SAASI,EACdN,EACAO,EACAC,GAEA,IAAIC,EAAWF,EAAOP,EAAMP,YAE5B,OAAOO,EAAMT,WAAWmB,IACtB,MAAMC,EAAUJ,EAAOG,GAEnBC,IAAYF,IACdA,EAAWE,EACXH,EAAOG,OCpGN,SAASC,EACdnD,GACCoD,KAAaC,GACd7C,GAEA,OAAK4C,EAEkC,IAA5BC,EAAiBnF,OACnB,IAAK8B,EAAK,CAACoD,GAAW5C,GAEtB,IACFR,EACH,CAACoD,GAAWD,EAAOnD,EAAIoD,IAAa,GAAIC,EAAkB7C,IANrDR,I","sources":["webpack://@openmrs/esm-state/../../../node_modules/systemjs-webpack-interop/auto-public-path/auto-public-path.js","webpack://@openmrs/esm-state/../../../node_modules/systemjs-webpack-interop/public-path.js","webpack://@openmrs/esm-state/webpack/bootstrap","webpack://@openmrs/esm-state/webpack/runtime/__system_context__","webpack://@openmrs/esm-state/webpack/runtime/define property getters","webpack://@openmrs/esm-state/webpack/runtime/global","webpack://@openmrs/esm-state/webpack/runtime/hasOwnProperty shorthand","webpack://@openmrs/esm-state/webpack/runtime/make namespace object","webpack://@openmrs/esm-state/webpack/runtime/publicPath","webpack://@openmrs/esm-state/../../../node_modules/systemjs-webpack-interop/auto-public-path/1.js","webpack://@openmrs/esm-state/../../../node_modules/unistore/dist/unistore.es.js","webpack://@openmrs/esm-state/./src/state.ts","webpack://@openmrs/esm-state/./src/update.ts"],"sourcesContent":["const resolveDirectory = require(\"../public-path\").resolveDirectory;\n\nexports.autoPublicPath = function autoPublicPath(rootDirLevel) {\n if (!rootDirLevel) {\n rootDirLevel = 1;\n }\n\n if (typeof __webpack_public_path__ !== \"undefined\") {\n if (typeof __system_context__ === \"undefined\") {\n throw Error(\n \"systemjs-webpack-interop requires webpack@>=5.0.0-beta.15 and output.libraryTarget set to 'system'\"\n );\n }\n\n if (!__system_context__.meta || !__system_context__.meta.url) {\n console.error(\"__system_context__\", __system_context__);\n throw Error(\n \"systemjs-webpack-interop was provided an unknown SystemJS context. Expected context.meta.url, but none was provided\"\n );\n }\n\n __webpack_public_path__ = resolveDirectory(\n __system_context__.meta.url,\n rootDirLevel\n );\n }\n};\n","exports.setPublicPath = function setPublicPath(\n systemjsModuleName,\n rootDirectoryLevel\n) {\n if (!rootDirectoryLevel) {\n rootDirectoryLevel = 1;\n }\n if (\n typeof systemjsModuleName !== \"string\" ||\n systemjsModuleName.trim().length === 0\n ) {\n throw Error(\n \"systemjs-webpack-interop: setPublicPath(systemjsModuleName) must be called with a non-empty string 'systemjsModuleName'\"\n );\n }\n\n if (\n typeof rootDirectoryLevel !== \"number\" ||\n rootDirectoryLevel <= 0 ||\n isNaN(rootDirectoryLevel) ||\n !isInteger(rootDirectoryLevel)\n ) {\n throw Error(\n \"systemjs-webpack-interop: setPublicPath(systemjsModuleName, rootDirectoryLevel) must be called with a positive integer 'rootDirectoryLevel'\"\n );\n }\n\n var moduleUrl;\n try {\n moduleUrl = window.System.resolve(systemjsModuleName);\n if (!moduleUrl) {\n throw Error();\n }\n } catch (err) {\n throw Error(\n \"systemjs-webpack-interop: There is no such module '\" +\n systemjsModuleName +\n \"' in the SystemJS registry. Did you misspell the name of your module?\"\n );\n }\n\n __webpack_public_path__ = resolveDirectory(moduleUrl, rootDirectoryLevel);\n};\n\nfunction resolveDirectory(urlString, rootDirectoryLevel) {\n // Our friend IE11 doesn't support new URL()\n // https://github.com/single-spa/single-spa/issues/612\n // https://gist.github.com/jlong/2428561\n\n var a = document.createElement(\"a\");\n a.href = urlString;\n\n var pathname = a.pathname[0] === \"/\" ? a.pathname : \"/\" + a.pathname;\n var numDirsProcessed = 0,\n index = pathname.length;\n while (numDirsProcessed !== rootDirectoryLevel && index >= 0) {\n var char = pathname[--index];\n if (char === \"/\") {\n numDirsProcessed++;\n }\n }\n\n if (numDirsProcessed !== rootDirectoryLevel) {\n throw Error(\n \"systemjs-webpack-interop: rootDirectoryLevel (\" +\n rootDirectoryLevel +\n \") is greater than the number of directories (\" +\n numDirsProcessed +\n \") in the URL path \" +\n urlString\n );\n }\n\n var finalPath = pathname.slice(0, index + 1);\n\n return a.protocol + \"//\" + a.host + finalPath;\n}\n\nexports.resolveDirectory = resolveDirectory;\n\n// borrowed from https://github.com/parshap/js-is-integer/blob/master/index.js\nvar isInteger =\n Number.isInteger ||\n function isInteger(val) {\n return typeof val === \"number\" && isFinite(val) && Math.floor(val) === val;\n };\n","// The module cache\nvar __webpack_module_cache__ = {};\n\n// The require function\nfunction __webpack_require__(moduleId) {\n\t// Check if module is in cache\n\tvar cachedModule = __webpack_module_cache__[moduleId];\n\tif (cachedModule !== undefined) {\n\t\treturn cachedModule.exports;\n\t}\n\t// Create a new module (and put it into the cache)\n\tvar module = __webpack_module_cache__[moduleId] = {\n\t\t// no module.id needed\n\t\t// no module.loaded needed\n\t\texports: {}\n\t};\n\n\t// Execute the module function\n\t__webpack_modules__[moduleId](module, module.exports, __webpack_require__);\n\n\t// Return the exports of the module\n\treturn module.exports;\n}\n\n","__webpack_require__.y = __system_context__;","// define getter functions for harmony exports\n__webpack_require__.d = (exports, definition) => {\n\tfor(var key in definition) {\n\t\tif(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {\n\t\t\tObject.defineProperty(exports, key, { enumerable: true, get: definition[key] });\n\t\t}\n\t}\n};","__webpack_require__.g = (function() {\n\tif (typeof globalThis === 'object') return globalThis;\n\ttry {\n\t\treturn this || new Function('return this')();\n\t} catch (e) {\n\t\tif (typeof window === 'object') return window;\n\t}\n})();","__webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))","// define __esModule on exports\n__webpack_require__.r = (exports) => {\n\tif(typeof Symbol !== 'undefined' && Symbol.toStringTag) {\n\t\tObject.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });\n\t}\n\tObject.defineProperty(exports, '__esModule', { value: true });\n};","var scriptUrl;\nif (__webpack_require__.g.importScripts) scriptUrl = __webpack_require__.g.location + \"\";\nvar document = __webpack_require__.g.document;\nif (!scriptUrl && document) {\n\tif (document.currentScript)\n\t\tscriptUrl = document.currentScript.src\n\tif (!scriptUrl) {\n\t\tvar scripts = document.getElementsByTagName(\"script\");\n\t\tif(scripts.length) scriptUrl = scripts[scripts.length - 1].src\n\t}\n}\n// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration\n// or pass an empty string (\"\") and set the __webpack_public_path__ variable from your code to use your own logic.\nif (!scriptUrl) throw new Error(\"Automatic publicPath is not supported in this browser\");\nscriptUrl = scriptUrl.replace(/#.*$/, \"\").replace(/\\?.*$/, \"\").replace(/\\/[^\\/]+$/, \"/\");\n__webpack_require__.p = scriptUrl;","const autoPublicPath = require(\"./auto-public-path\").autoPublicPath;\n\nautoPublicPath(1);\n","function n(n,t){for(var r in t)n[r]=t[r];return n}export default function(t){var r=[];function u(n){for(var t=[],u=0;u<r.length;u++)r[u]===n?n=null:t.push(r[u]);r=t}function e(u,e,f){t=e?u:n(n({},t),u);for(var i=r,o=0;o<i.length;o++)i[o](t,f)}return t=t||{},{action:function(n){function r(t){e(t,!1,n)}return function(){for(var u=arguments,e=[t],f=0;f<arguments.length;f++)e.push(u[f]);var i=n.apply(this,e);if(null!=i)return i.then?i.then(r):r(i)}},setState:e,subscribe:function(n){return r.push(n),function(){u(n)}},unsubscribe:u,getState:function(){return t}}}\n//# sourceMappingURL=unistore.es.js.map\n","import createStore, { Store } from \"unistore\";\n\ninterface StoreEntity {\n value: Store<any>;\n active: boolean;\n}\n\nconst availableStores: Record<string, StoreEntity> = {};\n\n/**\n * Creates a Unistore [store](https://github.com/developit/unistore#store).\n *\n * @param name A name by which the store can be looked up later.\n * Must be unique across the entire application.\n * @param initialState An object which will be the initial state of the store.\n * @returns The newly created store.\n */\nexport function createGlobalStore<TState>(\n name: string,\n initialState: TState\n): Store<TState> {\n const available = availableStores[name];\n\n if (available) {\n if (available.active) {\n console.error(\n \"Cannot override an existing store. Make sure that stores are only created once.\"\n );\n } else {\n available.value.setState(initialState, true);\n }\n\n available.active = true;\n return available.value;\n } else {\n const store = createStore(initialState);\n\n availableStores[name] = {\n value: store,\n active: true,\n };\n\n return store;\n }\n}\n\n/**\n * Returns the existing [store](https://github.com/developit/unistore#store) named `name`,\n * or creates a new store named `name` if none exists.\n *\n * @param name The name of the store to look up.\n * @param fallbackState The initial value of the new store if no store named `name` exists.\n * @returns The found or newly created store.\n */\nexport function getGlobalStore<TState = any>(\n name: string,\n fallbackState?: TState\n): Store<TState> {\n const available = availableStores[name];\n\n if (!available) {\n const store = createStore(fallbackState);\n availableStores[name] = {\n value: store,\n active: false,\n };\n return store;\n }\n\n return available.value;\n}\n\nexport interface AppState {}\n\n/**\n * @internal\n */\nexport function createAppState(initialState: AppState) {\n return createGlobalStore(\"app\", initialState);\n}\n\n/**\n * @returns The [store](https://github.com/developit/unistore#store) named `app`.\n */\nexport function getAppState() {\n return getGlobalStore<AppState>(\"app\", {});\n}\n\nexport function subscribeTo<T, U>(\n store: Store<T>,\n select: (state: T) => U,\n handle: (subState: U) => void\n) {\n let previous = select(store.getState());\n\n return store.subscribe((state) => {\n const current = select(state);\n\n if (current !== previous) {\n previous = current;\n handle(current);\n }\n });\n}\n","export function update<T extends Record<string, any>>(\n obj: T,\n [property, ...restPropertyPath]: Array<string>,\n value: any\n): T {\n if (!property) {\n return obj;\n } else if (restPropertyPath.length === 0) {\n return { ...obj, [property]: value };\n } else {\n return {\n ...obj,\n [property]: update(obj[property] || {}, restPropertyPath, value),\n };\n }\n}\n"],"names":["resolveDirectory","exports","rootDirLevel","meta","url","console","error","Error","urlString","rootDirectoryLevel","a","document","createElement","href","pathname","numDirsProcessed","index","length","finalPath","slice","protocol","host","Number","isInteger","__webpack_module_cache__","__webpack_require__","moduleId","cachedModule","undefined","module","__webpack_modules__","y","__system_context__","d","definition","key","o","Object","defineProperty","enumerable","get","g","globalThis","this","Function","e","window","obj","prop","prototype","hasOwnProperty","call","r","Symbol","toStringTag","value","scriptUrl","importScripts","location","currentScript","src","scripts","getElementsByTagName","replace","p","autoPublicPath","n","t","u","push","f","i","action","arguments","apply","then","setState","subscribe","unsubscribe","getState","availableStores","createGlobalStore","name","initialState","available","active","store","createStore","getGlobalStore","fallbackState","createAppState","getAppState","subscribeTo","select","handle","previous","state","current","update","property","restPropertyPath"],"sourceRoot":""}
|
package/docs/API.md
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
[Back to README.md](../README.md)
|
|
2
|
+
|
|
3
|
+
# @openmrs/esm-state
|
|
4
|
+
|
|
5
|
+
## Table of contents
|
|
6
|
+
|
|
7
|
+
### Interfaces
|
|
8
|
+
|
|
9
|
+
- [AppState](interfaces/AppState.md)
|
|
10
|
+
|
|
11
|
+
### Functions
|
|
12
|
+
|
|
13
|
+
- [createGlobalStore](API.md#createglobalstore)
|
|
14
|
+
- [getAppState](API.md#getappstate)
|
|
15
|
+
- [getGlobalStore](API.md#getglobalstore)
|
|
16
|
+
- [subscribeTo](API.md#subscribeto)
|
|
17
|
+
- [update](API.md#update)
|
|
18
|
+
|
|
19
|
+
## Functions
|
|
20
|
+
|
|
21
|
+
### createGlobalStore
|
|
22
|
+
|
|
23
|
+
▸ **createGlobalStore**<`TState`\>(`name`, `initialState`): `Store`<`TState`\>
|
|
24
|
+
|
|
25
|
+
Creates a Unistore [store](https://github.com/developit/unistore#store).
|
|
26
|
+
|
|
27
|
+
#### Type parameters
|
|
28
|
+
|
|
29
|
+
| Name |
|
|
30
|
+
| :------ |
|
|
31
|
+
| `TState` |
|
|
32
|
+
|
|
33
|
+
#### Parameters
|
|
34
|
+
|
|
35
|
+
| Name | Type | Description |
|
|
36
|
+
| :------ | :------ | :------ |
|
|
37
|
+
| `name` | `string` | A name by which the store can be looked up later. Must be unique across the entire application. |
|
|
38
|
+
| `initialState` | `TState` | An object which will be the initial state of the store. |
|
|
39
|
+
|
|
40
|
+
#### Returns
|
|
41
|
+
|
|
42
|
+
`Store`<`TState`\>
|
|
43
|
+
|
|
44
|
+
The newly created store.
|
|
45
|
+
|
|
46
|
+
#### Defined in
|
|
47
|
+
|
|
48
|
+
[state.ts:18](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-state/src/state.ts#L18)
|
|
49
|
+
|
|
50
|
+
___
|
|
51
|
+
|
|
52
|
+
### getAppState
|
|
53
|
+
|
|
54
|
+
▸ **getAppState**(): `Store`<[`AppState`](interfaces/AppState.md)\>
|
|
55
|
+
|
|
56
|
+
#### Returns
|
|
57
|
+
|
|
58
|
+
`Store`<[`AppState`](interfaces/AppState.md)\>
|
|
59
|
+
|
|
60
|
+
The [store](https://github.com/developit/unistore#store) named `app`.
|
|
61
|
+
|
|
62
|
+
#### Defined in
|
|
63
|
+
|
|
64
|
+
[state.ts:85](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-state/src/state.ts#L85)
|
|
65
|
+
|
|
66
|
+
___
|
|
67
|
+
|
|
68
|
+
### getGlobalStore
|
|
69
|
+
|
|
70
|
+
▸ **getGlobalStore**<`TState`\>(`name`, `fallbackState?`): `Store`<`TState`\>
|
|
71
|
+
|
|
72
|
+
Returns the existing [store](https://github.com/developit/unistore#store) named `name`,
|
|
73
|
+
or creates a new store named `name` if none exists.
|
|
74
|
+
|
|
75
|
+
#### Type parameters
|
|
76
|
+
|
|
77
|
+
| Name | Type |
|
|
78
|
+
| :------ | :------ |
|
|
79
|
+
| `TState` | `any` |
|
|
80
|
+
|
|
81
|
+
#### Parameters
|
|
82
|
+
|
|
83
|
+
| Name | Type | Description |
|
|
84
|
+
| :------ | :------ | :------ |
|
|
85
|
+
| `name` | `string` | The name of the store to look up. |
|
|
86
|
+
| `fallbackState?` | `TState` | The initial value of the new store if no store named `name` exists. |
|
|
87
|
+
|
|
88
|
+
#### Returns
|
|
89
|
+
|
|
90
|
+
`Store`<`TState`\>
|
|
91
|
+
|
|
92
|
+
The found or newly created store.
|
|
93
|
+
|
|
94
|
+
#### Defined in
|
|
95
|
+
|
|
96
|
+
[state.ts:55](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-state/src/state.ts#L55)
|
|
97
|
+
|
|
98
|
+
___
|
|
99
|
+
|
|
100
|
+
### subscribeTo
|
|
101
|
+
|
|
102
|
+
▸ **subscribeTo**<`T`, `U`\>(`store`, `select`, `handle`): `Unsubscribe`
|
|
103
|
+
|
|
104
|
+
#### Type parameters
|
|
105
|
+
|
|
106
|
+
| Name |
|
|
107
|
+
| :------ |
|
|
108
|
+
| `T` |
|
|
109
|
+
| `U` |
|
|
110
|
+
|
|
111
|
+
#### Parameters
|
|
112
|
+
|
|
113
|
+
| Name | Type |
|
|
114
|
+
| :------ | :------ |
|
|
115
|
+
| `store` | `Store`<`T`\> |
|
|
116
|
+
| `select` | (`state`: `T`) => `U` |
|
|
117
|
+
| `handle` | (`subState`: `U`) => `void` |
|
|
118
|
+
|
|
119
|
+
#### Returns
|
|
120
|
+
|
|
121
|
+
`Unsubscribe`
|
|
122
|
+
|
|
123
|
+
#### Defined in
|
|
124
|
+
|
|
125
|
+
[state.ts:89](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-state/src/state.ts#L89)
|
|
126
|
+
|
|
127
|
+
___
|
|
128
|
+
|
|
129
|
+
### update
|
|
130
|
+
|
|
131
|
+
▸ **update**<`T`\>(`obj`, `__namedParameters`, `value`): `T`
|
|
132
|
+
|
|
133
|
+
#### Type parameters
|
|
134
|
+
|
|
135
|
+
| Name | Type |
|
|
136
|
+
| :------ | :------ |
|
|
137
|
+
| `T` | extends `Record`<`string`, `any`\> |
|
|
138
|
+
|
|
139
|
+
#### Parameters
|
|
140
|
+
|
|
141
|
+
| Name | Type |
|
|
142
|
+
| :------ | :------ |
|
|
143
|
+
| `obj` | `T` |
|
|
144
|
+
| `__namedParameters` | `string`[] |
|
|
145
|
+
| `value` | `any` |
|
|
146
|
+
|
|
147
|
+
#### Returns
|
|
148
|
+
|
|
149
|
+
`T`
|
|
150
|
+
|
|
151
|
+
#### Defined in
|
|
152
|
+
|
|
153
|
+
[update.ts:1](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-state/src/update.ts#L1)
|
package/package.json
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openmrs/esm-state",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.12-pre.598",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"description": "Frontend stores & state management for OpenMRS",
|
|
6
6
|
"browser": "dist/openmrs-esm-state.js",
|
|
7
7
|
"main": "src/index.ts",
|
|
8
8
|
"source": true,
|
|
9
9
|
"scripts": {
|
|
10
|
+
"document": "../../../document.sh esm-state",
|
|
10
11
|
"test": "jest --config jest.config.js --passWithNoTests",
|
|
11
12
|
"build": "webpack --mode=production",
|
|
12
13
|
"analyze": "webpack --mode=production --env.analyze=true",
|
|
13
14
|
"typescript": "tsc",
|
|
14
|
-
"lint": "eslint src --ext ts,tsx"
|
|
15
|
-
"format": "prettier --write src/**"
|
|
15
|
+
"lint": "eslint src --ext ts,tsx"
|
|
16
16
|
},
|
|
17
17
|
"keywords": [
|
|
18
18
|
"openmrs",
|
|
@@ -37,8 +37,7 @@
|
|
|
37
37
|
"access": "public"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"systemjs-webpack-interop": "^2.1.2",
|
|
41
40
|
"unistore": "^3.5.2"
|
|
42
41
|
},
|
|
43
|
-
"gitHead": "
|
|
42
|
+
"gitHead": "ddb157645863df8cc29a26928bb29105b8c19119"
|
|
44
43
|
}
|
package/src/index.ts
CHANGED
package/src/state.ts
CHANGED
|
@@ -7,6 +7,14 @@ interface StoreEntity {
|
|
|
7
7
|
|
|
8
8
|
const availableStores: Record<string, StoreEntity> = {};
|
|
9
9
|
|
|
10
|
+
/**
|
|
11
|
+
* Creates a Unistore [store](https://github.com/developit/unistore#store).
|
|
12
|
+
*
|
|
13
|
+
* @param name A name by which the store can be looked up later.
|
|
14
|
+
* Must be unique across the entire application.
|
|
15
|
+
* @param initialState An object which will be the initial state of the store.
|
|
16
|
+
* @returns The newly created store.
|
|
17
|
+
*/
|
|
10
18
|
export function createGlobalStore<TState>(
|
|
11
19
|
name: string,
|
|
12
20
|
initialState: TState
|
|
@@ -36,6 +44,14 @@ export function createGlobalStore<TState>(
|
|
|
36
44
|
}
|
|
37
45
|
}
|
|
38
46
|
|
|
47
|
+
/**
|
|
48
|
+
* Returns the existing [store](https://github.com/developit/unistore#store) named `name`,
|
|
49
|
+
* or creates a new store named `name` if none exists.
|
|
50
|
+
*
|
|
51
|
+
* @param name The name of the store to look up.
|
|
52
|
+
* @param fallbackState The initial value of the new store if no store named `name` exists.
|
|
53
|
+
* @returns The found or newly created store.
|
|
54
|
+
*/
|
|
39
55
|
export function getGlobalStore<TState = any>(
|
|
40
56
|
name: string,
|
|
41
57
|
fallbackState?: TState
|
|
@@ -56,10 +72,33 @@ export function getGlobalStore<TState = any>(
|
|
|
56
72
|
|
|
57
73
|
export interface AppState {}
|
|
58
74
|
|
|
75
|
+
/**
|
|
76
|
+
* @internal
|
|
77
|
+
*/
|
|
59
78
|
export function createAppState(initialState: AppState) {
|
|
60
79
|
return createGlobalStore("app", initialState);
|
|
61
80
|
}
|
|
62
81
|
|
|
82
|
+
/**
|
|
83
|
+
* @returns The [store](https://github.com/developit/unistore#store) named `app`.
|
|
84
|
+
*/
|
|
63
85
|
export function getAppState() {
|
|
64
86
|
return getGlobalStore<AppState>("app", {});
|
|
65
87
|
}
|
|
88
|
+
|
|
89
|
+
export function subscribeTo<T, U>(
|
|
90
|
+
store: Store<T>,
|
|
91
|
+
select: (state: T) => U,
|
|
92
|
+
handle: (subState: U) => void
|
|
93
|
+
) {
|
|
94
|
+
let previous = select(store.getState());
|
|
95
|
+
|
|
96
|
+
return store.subscribe((state) => {
|
|
97
|
+
const current = select(state);
|
|
98
|
+
|
|
99
|
+
if (current !== previous) {
|
|
100
|
+
previous = current;
|
|
101
|
+
handle(current);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
package/src/update.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export function update<T extends Record<string, any>>(
|
|
2
|
+
obj: T,
|
|
3
|
+
[property, ...restPropertyPath]: Array<string>,
|
|
4
|
+
value: any
|
|
5
|
+
): T {
|
|
6
|
+
if (!property) {
|
|
7
|
+
return obj;
|
|
8
|
+
} else if (restPropertyPath.length === 0) {
|
|
9
|
+
return { ...obj, [property]: value };
|
|
10
|
+
} else {
|
|
11
|
+
return {
|
|
12
|
+
...obj,
|
|
13
|
+
[property]: update(obj[property] || {}, restPropertyPath, value),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
package/typedoc.json
ADDED
package/webpack.config.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const ForkTsCheckerWebpackPlugin = require("fork-ts-checker-webpack-plugin");
|
|
2
|
+
const SystemJSPublicPathWebpackPlugin = require("systemjs-webpack-interop/SystemJSPublicPathWebpackPlugin");
|
|
2
3
|
const { resolve } = require("path");
|
|
3
4
|
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
|
|
4
5
|
const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
|
|
@@ -6,23 +7,15 @@ const { BundleAnalyzerPlugin } = require("webpack-bundle-analyzer");
|
|
|
6
7
|
const { peerDependencies } = require("./package.json");
|
|
7
8
|
|
|
8
9
|
module.exports = (env) => ({
|
|
9
|
-
entry: [
|
|
10
|
-
resolve(__dirname, "src/set-public-path.ts"),
|
|
11
|
-
resolve(__dirname, "src/index.ts"),
|
|
12
|
-
],
|
|
10
|
+
entry: [resolve(__dirname, "src/index.ts")],
|
|
13
11
|
output: {
|
|
14
12
|
filename: "openmrs-esm-state.js",
|
|
15
13
|
path: resolve(__dirname, "dist"),
|
|
16
14
|
libraryTarget: "system",
|
|
17
15
|
},
|
|
18
|
-
devtool: "
|
|
16
|
+
devtool: "source-map",
|
|
19
17
|
module: {
|
|
20
18
|
rules: [
|
|
21
|
-
{
|
|
22
|
-
parser: {
|
|
23
|
-
system: false,
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
19
|
{
|
|
27
20
|
test: /\.m?(js|ts|tsx)$/,
|
|
28
21
|
exclude: /(node_modules|bower_components)/,
|
|
@@ -30,11 +23,12 @@ module.exports = (env) => ({
|
|
|
30
23
|
},
|
|
31
24
|
],
|
|
32
25
|
},
|
|
33
|
-
externals: Object.keys(peerDependencies),
|
|
26
|
+
externals: Object.keys(peerDependencies || {}),
|
|
34
27
|
resolve: {
|
|
35
28
|
extensions: [".ts", ".js", ".tsx", ".jsx"],
|
|
36
29
|
},
|
|
37
30
|
plugins: [
|
|
31
|
+
new SystemJSPublicPathWebpackPlugin(),
|
|
38
32
|
new CleanWebpackPlugin(),
|
|
39
33
|
new ForkTsCheckerWebpackPlugin(),
|
|
40
34
|
new BundleAnalyzerPlugin({
|
package/src/set-public-path.ts
DELETED