@interface-technologies/check-for-js-bundle-update-saga 2.1.0-alpha.0 → 4.0.0-alpha.2

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/LICENSE CHANGED
@@ -1,7 +1,7 @@
1
- Copyright 2018 Interface Technologies, Inc.
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
-
5
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
-
1
+ Copyright 2018 Interface Technologies, Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
7
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/README.md CHANGED
@@ -1 +1 @@
1
- See https://srmagura.github.io/iti-react/.
1
+ See https://srmagura.github.io/iti-react/.
@@ -21,8 +21,7 @@ export interface CheckForJsBundleUpdateSagaOptions {
21
21
  * contains a hash of the JavaScript bundle's entry point. Here's how to do this
22
22
  * in ASP.NET Core:
23
23
  *
24
- * # TODO:SAM change langauge to razor once shiki bug fix released (https://github.com/shikijs/shiki/issues/219)
25
- * ```text
24
+ * ```razor
26
25
  * var path = $"dist/{bundleName}.js";
27
26
  * string jsBundleHash;
28
27
  *
@@ -46,7 +45,7 @@ export interface CheckForJsBundleUpdateSagaOptions {
46
45
  *
47
46
  * ```
48
47
  * export function* myCheckForJsBundleUpdateSaga(): SagaIterator<void> {
49
- * if ((window as unknown as WindowWithGlobals).isDebug) return
48
+ * if (process.env.NODE_ENV === 'development') return
50
49
  *
51
50
  * function onError(e: unknown): void {
52
51
  * console.error(e)
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.checkForJsBundleUpdateSaga = exports.reload = exports.getIndexHtml = void 0;
7
+ const jsx_runtime_1 = require("react/jsx-runtime");
8
+ const moment_timezone_1 = __importDefault(require("moment-timezone"));
9
+ const effects_1 = require("redux-saga/effects");
10
+ const iti_react_1 = require("@interface-technologies/iti-react");
11
+ const hashElementId = 'jsBundleHash';
12
+ const defaultDelayDuration = moment_timezone_1.default.duration(4, 'minutes');
13
+ const forceRefreshAfterAlertCount = 3;
14
+ /** @internal */
15
+ function getIndexHtml() {
16
+ return (fetch('/')
17
+ .then((response) => {
18
+ if (!response.ok)
19
+ return undefined;
20
+ return response.text();
21
+ })
22
+ // If fetch throws a TypeError for some weird reason, also return undefined
23
+ .catch(() => undefined));
24
+ }
25
+ exports.getIndexHtml = getIndexHtml;
26
+ /** @internal */
27
+ function reload() {
28
+ window.location.reload();
29
+ }
30
+ exports.reload = reload;
31
+ /**
32
+ * Peridoically fetches `/` (`index.html`) to check if a new JavaScript bundle has been
33
+ * released.
34
+ *
35
+ * If the bundle has been updated, the user is prompted to refresh the page.
36
+ * After 3 alerts are shown, the page is forcibly refreshed.
37
+ *
38
+ * Don't enable this in development!
39
+ *
40
+ * Your `index.html` must contain a hidden element with the ID `jsBundleHash` that
41
+ * contains a hash of the JavaScript bundle's entry point. Here's how to do this
42
+ * in ASP.NET Core:
43
+ *
44
+ * ```razor
45
+ * var path = $"dist/{bundleName}.js";
46
+ * string jsBundleHash;
47
+ *
48
+ * using (var sha256 = SHA256.Create())
49
+ * {
50
+ * var fullPath = System.IO.Path.Combine("wwwroot", path);
51
+ * using (var readStream = File.OpenRead(fullPath))
52
+ * {
53
+ * var hashBytes = sha256.ComputeHash(readStream);
54
+ * jsBundleHash = WebEncoders.Base64UrlEncode(hashBytes);
55
+ * }
56
+ * }
57
+ *
58
+ * <script src=@path asp-append-version="true"></script>
59
+ * <span id="jsBundleHash" style="display: none">
60
+ * @jsBundleHash
61
+ * </span>
62
+ * ```
63
+ *
64
+ * And example of using `checkForJsBundleUpdateSaga` from your TypeScript code:
65
+ *
66
+ * ```
67
+ * export function* myCheckForJsBundleUpdateSaga(): SagaIterator<void> {
68
+ * if (process.env.NODE_ENV === 'development') return
69
+ *
70
+ * function onError(e: unknown): void {
71
+ * console.error(e)
72
+ *
73
+ * const ierror = processError(e)
74
+ *
75
+ * // Never show the user an error because of this
76
+ * if (shouldLogError(ierror)) {
77
+ * Bugsnag.notify(ierror)
78
+ * }
79
+ * }
80
+ *
81
+ * yield call(checkForJsBundleUpdateSaga, { onError })
82
+ * }
83
+ * ```
84
+ */
85
+ function* checkForJsBundleUpdateSaga({ delayDuration = defaultDelayDuration, onError, }) {
86
+ var _a, _b, _c;
87
+ const jsBundleHash = (_b = (_a = document.getElementById(hashElementId)) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim();
88
+ if (!jsBundleHash) {
89
+ onError(new Error('Could not get jsBundleHash.'));
90
+ return;
91
+ }
92
+ yield (0, effects_1.delay)(delayDuration.asMilliseconds());
93
+ let alertShownCount = 0;
94
+ for (;;) {
95
+ try {
96
+ const indexHtml = (yield (0, effects_1.call)(getIndexHtml));
97
+ if (indexHtml) {
98
+ const document = new DOMParser().parseFromString(indexHtml, 'text/html');
99
+ const hashEl = document.getElementById(hashElementId);
100
+ if (hashEl) {
101
+ // innerText doesn't work here for some reason
102
+ const newJsBundleHash = (_c = hashEl.innerHTML) === null || _c === void 0 ? void 0 : _c.trim();
103
+ if (jsBundleHash !== newJsBundleHash) {
104
+ const content = ((0, jsx_runtime_1.jsxs)("div", { children: [(0, jsx_runtime_1.jsx)("p", { children: "Please save your work and refresh the page." }, void 0), (0, jsx_runtime_1.jsx)("p", { className: "mb-0", children: "You may encounter errors if you do not refresh the page." }, void 0)] }, void 0));
105
+ if (alertShownCount >= forceRefreshAfterAlertCount) {
106
+ window.onbeforeunload = null;
107
+ yield (0, effects_1.call)(reload);
108
+ return;
109
+ }
110
+ yield (0, effects_1.call)(iti_react_1.alert, content, { title: 'Website Update Available!' });
111
+ alertShownCount += 1;
112
+ }
113
+ }
114
+ else {
115
+ onError(new Error('Could not get jsBundleHash in fetched document.'));
116
+ }
117
+ }
118
+ }
119
+ catch (e) {
120
+ onError(e);
121
+ }
122
+ yield (0, effects_1.delay)(delayDuration.asMilliseconds());
123
+ }
124
+ }
125
+ exports.checkForJsBundleUpdateSaga = checkForJsBundleUpdateSaga;
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkForJsBundleUpdateSaga.js","sourceRoot":"","sources":["../src/checkForJsBundleUpdateSaga.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAA;AACzB,OAAO,MAAM,MAAM,iBAAiB,CAAA;AAEpC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAA;AAEzD,MAAM,aAAa,GAAG,cAAc,CAAA;AACpC,MAAM,oBAAoB,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,EAAE,SAAS,CAAC,CAAA;AAC1D,MAAM,2BAA2B,GAAG,C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ACR,8CAA8C;oBAC9C,MAAM,eAAe,GAAG,MAAA,MAAM,CAAC,SAAS,0CAAE,IAAI,EAAE,CAAA;oBAEhD,IAAI,YAAY,KAAK,eAAe,EAAE;wBAClC,MAAM,OAAO,GAAG,CACZ;4BACI,6EAAkD;4BAClD,2BAAG,SAAS,EAAC,MAAM,+DAGf,CACF,CACT,CAAA;wBAED,IAAI,eAAe,IAAI,2BAA2B,EAAE;4BAChD,MAAM,CAAC,cAAc,GAAG,IAAI,CAAA;4BAC5B,MAAM,IAAI,CAAC,MAAM,CAAC,CAAA;4BAClB,OAAM;yBACT;wBAED,MAAM,IAAI,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,KAAK,EAAE,2BAA2B,EAAE,CAAC,CAAA;wBAClE,eAAe,IAAI,CAAC,CAAA;qBACvB;iBACJ;qBAAM;oBACH,OAAO,CAAC,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC,CAAA;iBACxE;aACJ;SACJ;QAAC,OAAO,CAAC,EAAE;YACR,OAAO,CAAC,CAAC,CAAC,CAAA;SACb;QAED,MAAM,KAAK,CAAC,aAAa,CAAC,cAAc,EAAE,CAAC,CAAA;KAC9C;AACL,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const moment_timezone_1 = __importDefault(require("moment-timezone"));
7
+ const redux_saga_test_plan_1 = require("redux-saga-test-plan");
8
+ const effects_1 = require("redux-saga/effects");
9
+ const iti_react_1 = require("@interface-technologies/iti-react");
10
+ const checkForJsBundleUpdateSaga_1 = require("./checkForJsBundleUpdateSaga");
11
+ jest.mock('@interface-technologies/iti-react');
12
+ // Mock window.location.reload since it not implemented in jsdom
13
+ // eslint-disable-next-line
14
+ delete window.location;
15
+ window.location = { reload: jest.fn() };
16
+ const jsBundleHash = 'hash0';
17
+ const newJsBundleHash = 'hash1';
18
+ beforeEach(() => {
19
+ jest.useRealTimers();
20
+ const jsBundleHashElement = document.createElement('span');
21
+ jsBundleHashElement.innerText = jsBundleHash;
22
+ jsBundleHashElement.id = 'jsBundleHash';
23
+ document.body.appendChild(jsBundleHashElement);
24
+ });
25
+ function getHtml(hash) {
26
+ return `
27
+ <html>
28
+ <head></head>
29
+ <body>
30
+ <span id="jsBundleHash">${hash}</span>
31
+ </body>
32
+ </html>
33
+ `;
34
+ }
35
+ const delayDuration = moment_timezone_1.default.duration(0, 'seconds');
36
+ it('does not show alert if hash matches', async () => {
37
+ const onError = jest.fn();
38
+ await (0, redux_saga_test_plan_1.expectSaga)(checkForJsBundleUpdateSaga_1.checkForJsBundleUpdateSaga, { delayDuration, onError })
39
+ .provide([[(0, effects_1.call)(checkForJsBundleUpdateSaga_1.getIndexHtml), getHtml(jsBundleHash)]])
40
+ .not.call.fn(iti_react_1.alert)
41
+ .not.call.fn(checkForJsBundleUpdateSaga_1.reload)
42
+ .silentRun();
43
+ expect(onError).not.toHaveBeenCalled();
44
+ });
45
+ it('does not call onError if getIndexHtml returns undefined', async () => {
46
+ const onError = jest.fn();
47
+ await (0, redux_saga_test_plan_1.expectSaga)(checkForJsBundleUpdateSaga_1.checkForJsBundleUpdateSaga, { delayDuration, onError })
48
+ .provide([[(0, effects_1.call)(checkForJsBundleUpdateSaga_1.getIndexHtml), undefined]])
49
+ .not.call.fn(iti_react_1.alert)
50
+ .not.call.fn(checkForJsBundleUpdateSaga_1.reload)
51
+ .silentRun();
52
+ expect(onError).not.toHaveBeenCalled();
53
+ });
54
+ it('shows alert several times and then refreshes page', async () => {
55
+ const onError = jest.fn((e) => console.error(e));
56
+ await (0, redux_saga_test_plan_1.expectSaga)(checkForJsBundleUpdateSaga_1.checkForJsBundleUpdateSaga, { delayDuration, onError })
57
+ .provide([[(0, effects_1.call)(checkForJsBundleUpdateSaga_1.getIndexHtml), getHtml(newJsBundleHash)]])
58
+ .call.fn(iti_react_1.alert)
59
+ .call.fn(iti_react_1.alert)
60
+ .call.fn(iti_react_1.alert)
61
+ .call(checkForJsBundleUpdateSaga_1.reload)
62
+ .run();
63
+ expect(onError).not.toHaveBeenCalled();
64
+ expect(window.location.reload).toHaveBeenCalled();
65
+ });
@@ -0,0 +1 @@
1
+ {"version":3,"file":"checkForJsBundleUpdateSaga.test.js","sourceRoot":"","sources":["../src/checkForJsBundleUpdateSaga.test.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,iBAAiB,CAAA;AACpC,OAAO,EAAE,UAAU,EAAE,MAAM,sBAAsB,CAAA;AACjD,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAA;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAA;AACzD,OAAO,EACH,YAAY,EACZ,0BAA0B,EAC1B,MAAM,GACT,MAAM,8BAA8B,CAAA;AAErC,IAAI,CAAC,IAAI,CAAC,mCAAmC,CAAC,CAAA;AAE9C,gEAAgE;AAChE,2BAA2B;AAC3B,OAAQ,MAAc,CAAC,QAAQ,CAAA;AAC/B,MAAM,CAAC,QAAQ,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE,EAAyB,CAAA;AAE9D,MAAM,YAAY,GAAG,OAAO,CAAA;AAC5B,MAAM,eAAe,GAAG,OAAO,CAAA;AAE/B,UAAU,CAAC,GAAG,EAAE;IACZ,IAAI,CAAC,aAAa,EAAE,CAAA;IAEpB,MAAM,mBAAmB,GAAG,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,CAAA;IAC1D,mBAAmB,CAAC,SAAS,GAAG,YAAY,CAAA;IAC5C,mBAAmB,CAAC,EAAE,GAAG,cAAc,CAAA;IACvC,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,mBAAmB,CAAC,CAAA;AAClD,CAAC,CAAC,CAAA;AAEF,SAAS,OAAO,CAAC,IAAY;IACzB,OAAO;;;;8BAImB,I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EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAA;IAEhD,MAAM,UAAU,CAAC,0BAA0B,EAAE,EAAE,aAAa,EAAE,OAAO,EAAE,CAAC;SACnE,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;SACzD,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC;SACd,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC;SACd,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC;SACd,IAAI,CAAC,MAAM,CAAC;SACZ,GAAG,EAAE,CAAA;IAEV,MAAM,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAA;IACtC,MAAM,CAAC,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,gBAAgB,EAAE,CAAA;AACrD,CAAC,CAAC,CAAA"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { checkForJsBundleUpdateSaga } from './CheckForJsBundleUpdateSaga';
2
- export type { CheckForJsBundleUpdateSagaOptions } from './CheckForJsBundleUpdateSaga';
1
+ export { checkForJsBundleUpdateSaga } from './checkForJsBundleUpdateSaga';
2
+ export type { CheckForJsBundleUpdateSagaOptions } from './checkForJsBundleUpdateSaga';
package/dist/index.js CHANGED
@@ -1,131 +1,5 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
- var React = require('react');
6
- var moment = require('moment-timezone');
7
- var effects = require('redux-saga/effects');
8
- var itiReact = require('@interface-technologies/iti-react');
9
-
10
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
-
12
- var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
13
- var moment__default = /*#__PURE__*/_interopDefaultLegacy(moment);
14
-
15
- const hashElementId = 'jsBundleHash';
16
- const defaultDelayDuration = moment__default["default"].duration(4, 'minutes');
17
- const forceRefreshAfterAlertCount = 3;
18
- /** @internal */
19
- function getIndexHtml() {
20
- return (fetch('/')
21
- .then((response) => {
22
- if (!response.ok)
23
- return undefined;
24
- return response.text();
25
- })
26
- // If fetch throws a TypeError for some weird reason, also return undefined
27
- .catch(() => undefined));
28
- }
29
- /** @internal */
30
- function reload() {
31
- window.location.reload();
32
- }
33
- /**
34
- * Peridoically fetches `/` (`index.html`) to check if a new JavaScript bundle has been
35
- * released.
36
- *
37
- * If the bundle has been updated, the user is prompted to refresh the page.
38
- * After 3 alerts are shown, the page is forcibly refreshed.
39
- *
40
- * Don't enable this in development!
41
- *
42
- * Your `index.html` must contain a hidden element with the ID `jsBundleHash` that
43
- * contains a hash of the JavaScript bundle's entry point. Here's how to do this
44
- * in ASP.NET Core:
45
- *
46
- * # TODO:SAM change langauge to razor once shiki bug fix released (https://github.com/shikijs/shiki/issues/219)
47
- * ```text
48
- * var path = $"dist/{bundleName}.js";
49
- * string jsBundleHash;
50
- *
51
- * using (var sha256 = SHA256.Create())
52
- * {
53
- * var fullPath = System.IO.Path.Combine("wwwroot", path);
54
- * using (var readStream = File.OpenRead(fullPath))
55
- * {
56
- * var hashBytes = sha256.ComputeHash(readStream);
57
- * jsBundleHash = WebEncoders.Base64UrlEncode(hashBytes);
58
- * }
59
- * }
60
- *
61
- * <script src=@path asp-append-version="true"></script>
62
- * <span id="jsBundleHash" style="display: none">
63
- * @jsBundleHash
64
- * </span>
65
- * ```
66
- *
67
- * And example of using `checkForJsBundleUpdateSaga` from your TypeScript code:
68
- *
69
- * ```
70
- * export function* myCheckForJsBundleUpdateSaga(): SagaIterator<void> {
71
- * if ((window as unknown as WindowWithGlobals).isDebug) return
72
- *
73
- * function onError(e: unknown): void {
74
- * console.error(e)
75
- *
76
- * const ierror = processError(e)
77
- *
78
- * // Never show the user an error because of this
79
- * if (shouldLogError(ierror)) {
80
- * Bugsnag.notify(ierror)
81
- * }
82
- * }
83
- *
84
- * yield call(checkForJsBundleUpdateSaga, { onError })
85
- * }
86
- * ```
87
- */
88
- function* checkForJsBundleUpdateSaga({ delayDuration = defaultDelayDuration, onError, }) {
89
- var _a, _b, _c;
90
- const jsBundleHash = (_b = (_a = document.getElementById(hashElementId)) === null || _a === void 0 ? void 0 : _a.innerText) === null || _b === void 0 ? void 0 : _b.trim();
91
- if (!jsBundleHash) {
92
- onError(new Error('Could not get jsBundleHash.'));
93
- return;
94
- }
95
- yield effects.delay(delayDuration.asMilliseconds());
96
- let alertShownCount = 0;
97
- for (;;) {
98
- try {
99
- const indexHtml = (yield effects.call(getIndexHtml));
100
- if (indexHtml) {
101
- const document = new DOMParser().parseFromString(indexHtml, 'text/html');
102
- const hashEl = document.getElementById(hashElementId);
103
- if (hashEl) {
104
- // innerText doesn't work here for some reason
105
- const newJsBundleHash = (_c = hashEl.innerHTML) === null || _c === void 0 ? void 0 : _c.trim();
106
- if (jsBundleHash !== newJsBundleHash) {
107
- const content = (React__default["default"].createElement("div", null,
108
- React__default["default"].createElement("p", null, "Please save your work and refresh the page."),
109
- React__default["default"].createElement("p", { className: "mb-0" }, "You may encounter errors if you do not refresh the page.")));
110
- if (alertShownCount >= forceRefreshAfterAlertCount) {
111
- window.onbeforeunload = null;
112
- yield effects.call(reload);
113
- return;
114
- }
115
- yield effects.call(itiReact.alert, content, { title: 'Website Update Available!' });
116
- alertShownCount += 1;
117
- }
118
- }
119
- else {
120
- onError(new Error('Could not get jsBundleHash in fetched document.'));
121
- }
122
- }
123
- }
124
- catch (e) {
125
- onError(e);
126
- }
127
- yield effects.delay(delayDuration.asMilliseconds());
128
- }
129
- }
130
-
131
- exports.checkForJsBundleUpdateSaga = checkForJsBundleUpdateSaga;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkForJsBundleUpdateSaga = void 0;
4
+ var checkForJsBundleUpdateSaga_1 = require("./checkForJsBundleUpdateSaga");
5
+ Object.defineProperty(exports, "checkForJsBundleUpdateSaga", { enumerable: true, get: function () { return checkForJsBundleUpdateSaga_1.checkForJsBundleUpdateSaga; } });
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,8BAA8B,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@interface-technologies/check-for-js-bundle-update-saga",
3
- "version": "2.1.0-alpha.0",
3
+ "version": "4.0.0-alpha.2",
4
4
  "description": "Redux saga for checking if a JavaScript app has been deployed.",
5
5
  "homepage": "https://github.com/srmagura/iti-react",
6
6
  "repository": {
@@ -10,78 +10,28 @@
10
10
  "license": "MIT",
11
11
  "author": "Interface Technologies, Inc.",
12
12
  "main": "dist/index.js",
13
- "module": "dist/index.mjs",
14
13
  "types": "dist/index.d.ts",
14
+ "typedocMain": "src/index.ts",
15
15
  "files": [
16
16
  "dist"
17
17
  ],
18
- "scripts": {
19
- "build": "yarn clean && tsc && rollup -c ../rollup.config.js",
20
- "clean": "rimraf dist",
21
- "lint": "eslint --max-warnings 0",
22
- "lint-all": "yarn lint .",
23
- "lint-staged": "lint-staged --no-stash",
24
- "prepack": "yarn build",
25
- "prettier": "prettier",
26
- "test": "jest"
27
- },
28
18
  "prettier": "@interface-technologies/prettier-config",
29
19
  "dependencies": {
30
- "@types/react": "^17.0.27",
31
- "moment-timezone": "^0.5.33",
20
+ "@types/react": "^17.0.35",
21
+ "moment-timezone": "^0.5.34",
32
22
  "redux-saga": "^1.1.3"
33
23
  },
34
24
  "devDependencies": {
35
- "@babel/core": "^7.15.5",
36
- "@babel/preset-env": "^7.15.6",
37
- "@babel/preset-react": "^7.14.5",
38
- "@babel/preset-typescript": "^7.15.0",
39
- "@interface-technologies/eslint-config": "0.4.4",
40
- "@interface-technologies/iti-react": "3.1.0-alpha",
41
- "@interface-technologies/jest-config": "0.3.0",
42
- "@interface-technologies/lint-staged-config": "0.1.1",
43
- "@interface-technologies/prettier-config": "0.1.0",
44
- "@interface-technologies/tsconfig": "0.1.3",
25
+ "@interface-technologies/iti-react": "4.0.0-alpha.2",
26
+ "@interface-technologies/tsconfig": "4.0.0-alpha.2",
45
27
  "@redux-saga/is": "^1.1.2",
46
28
  "@redux-saga/symbols": "^1.1.2",
47
- "@rollup/plugin-commonjs": "^21.0.0",
48
- "@rollup/plugin-typescript": "^8.2.5",
49
29
  "@testing-library/react-hooks": "^7.0.2",
50
- "@types/jest": "27.0.2",
51
- "@typescript-eslint/eslint-plugin": "^4.33.0",
52
- "@typescript-eslint/parser": "^4.33.0",
53
- "babel-jest": "^27.2.4",
54
- "eslint": "^7.32.0",
55
- "eslint-config-airbnb": "^18.2.1",
56
- "eslint-config-airbnb-typescript": "^14.0.0",
57
- "eslint-config-prettier": "^8.3.0",
58
- "eslint-import-resolver-typescript": "^2.5.0",
59
- "eslint-plugin-import": "^2.24.2",
60
- "eslint-plugin-jest": "^24.5.2",
61
- "eslint-plugin-jest-dom": "^3.9.2",
62
- "eslint-plugin-jsx-a11y": "^6.4.1",
63
- "eslint-plugin-promise": "^5.1.0",
64
- "eslint-plugin-react": "^7.26.1",
65
- "eslint-plugin-react-hooks": "^4.2.0",
66
- "eslint-plugin-redux-saga": "^1.2.1",
67
- "eslint-plugin-testing-library": "^4.12.4",
68
- "jest": "^27.2.4",
69
- "jest-haste-map": "^27.2.4",
70
- "jest-resolve": "^27.2.4",
71
- "lint-staged": "^11.2.0",
72
- "prettier": "^2.4.1",
73
30
  "react": "^17.0.2",
74
- "react-test-renderer": "^17.0.2",
75
- "redux-saga-test-plan": "^4.0.3",
76
- "rimraf": "^3.0.2",
77
- "rollup": "^2.58.0",
78
- "rollup-plugin-copy": "^3.4.0",
79
- "ts-jest": "^27.0.5",
80
- "tslib": "^2.3.1",
81
- "typescript": "~4.4.3"
31
+ "redux-saga-test-plan": "^4.0.4"
82
32
  },
83
33
  "peerDependencies": {
84
- "@interface-technologies/iti-react": "^3.0.0",
34
+ "@interface-technologies/iti-react": "^4.0.0-alpha.2",
85
35
  "@popperjs/core": "^2.10.2",
86
36
  "bootstrap": "^5.1.2",
87
37
  "react": "^17.0.2"