@atlaspack/transformer-react-refresh-wrap 2.14.11 → 2.14.13

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/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # @atlaspack/transformer-react-refresh-wrap
2
2
 
3
+ ## 2.14.13
4
+
5
+ ### Patch Changes
6
+
7
+ - [#633](https://github.com/atlassian-labs/atlaspack/pull/633) [`26aa9c5`](https://github.com/atlassian-labs/atlaspack/commit/26aa9c599d2be45ce1438a74c5fa22f39b9b554b) Thanks [@sbhuiyan-atlassian](https://github.com/sbhuiyan-atlassian)! - Ported various HMR changes from Parcel
8
+
9
+ - Updated dependencies [[`26aa9c5`](https://github.com/atlassian-labs/atlaspack/commit/26aa9c599d2be45ce1438a74c5fa22f39b9b554b), [`0501255`](https://github.com/atlassian-labs/atlaspack/commit/05012550da35b05ce7d356a8cc29311e7f9afdca)]:
10
+ - @atlaspack/feature-flags@2.18.1
11
+ - @atlaspack/utils@2.15.1
12
+ - @atlaspack/plugin@2.14.13
13
+
14
+ ## 2.14.12
15
+
16
+ ### Patch Changes
17
+
18
+ - Updated dependencies [[`e39c6cf`](https://github.com/atlassian-labs/atlaspack/commit/e39c6cf05f7e95ce5420dbcea66f401b1cbd397c)]:
19
+ - @atlaspack/utils@2.15.0
20
+ - @atlaspack/plugin@2.14.12
21
+
3
22
  ## 2.14.11
4
23
 
5
24
  ### Patch Changes
@@ -18,6 +18,13 @@ function _plugin() {
18
18
  };
19
19
  return data;
20
20
  }
21
+ function _featureFlags() {
22
+ const data = require("@atlaspack/feature-flags");
23
+ _featureFlags = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
21
28
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
29
  function shouldExclude(asset, options) {
23
30
  return !asset.isSource || !options.hmrOptions || !asset.env.isBrowser() || asset.env.isLibrary || asset.env.isWorker() || asset.env.isWorklet() || options.mode !== 'development' || !asset.getDependencies().find(v => v.specifier === 'react' || v.specifier === 'react/jsx-runtime' || v.specifier === 'react/jsx-dev-runtime' || v.specifier === '@emotion/react' || v.specifier === '@emotion/react/jsx-runtime' || v.specifier === '@emotion/react/jsx-dev-runtime');
@@ -30,11 +37,13 @@ var _default = exports.default = new (_plugin().Transformer)({
30
37
  if (shouldExclude(asset, options)) {
31
38
  return [asset];
32
39
  }
33
- let wrapperPath = `@atlaspack/transformer-react-refresh-wrap/${_path().default.basename(__dirname)}/helpers/helpers.js`;
40
+ const helperFilename = (0, _featureFlags().getFeatureFlag)('hmrImprovements') ? 'helpers-new.js' : 'helpers.js';
41
+ let wrapperPath = `@atlaspack/transformer-react-refresh-wrap/${_path().default.basename(__dirname)}/helpers/${helperFilename}`;
34
42
  let code = await asset.getCode();
35
43
  let map = await asset.getMap();
36
44
  let name = `$parcel$ReactRefreshHelpers$${asset.id.slice(-4)}`;
37
45
  code = `var ${name} = require(${JSON.stringify(wrapperPath)});
46
+ ${(0, _featureFlags().getFeatureFlag)('hmrImprovements') ? `${name}.init();` : ''}
38
47
  var prevRefreshReg = window.$RefreshReg$;
39
48
  var prevRefreshSig = window.$RefreshSig$;
40
49
  ${name}.prelude(module);
@@ -0,0 +1,197 @@
1
+ "use strict";
2
+
3
+ var Refresh = require('react-refresh/runtime');
4
+ function debounce(func, delay) {
5
+ if (process.env.NODE_ENV === 'test') {
6
+ return function (args) {
7
+ func.call(null, args);
8
+ };
9
+ } else {
10
+ let timeout = undefined;
11
+ let lastTime = 0;
12
+ return function (args) {
13
+ // Call immediately if last call was more than the delay ago.
14
+ // Otherwise, set a timeout. This means the first call is fast
15
+ // (for the common case of a single update), and subsequent updates
16
+ // are batched.
17
+ let now = Date.now();
18
+ if (now - lastTime > delay) {
19
+ lastTime = now;
20
+ func.call(null, args);
21
+ } else {
22
+ clearTimeout(timeout);
23
+ timeout = setTimeout(function () {
24
+ timeout = undefined;
25
+ lastTime = Date.now();
26
+ func.call(null, args);
27
+ }, delay);
28
+ }
29
+ };
30
+ }
31
+ }
32
+ var enqueueUpdate = debounce(function () {
33
+ Refresh.performReactRefresh();
34
+ }, 30);
35
+ module.exports.init = function () {
36
+ if (!window.$RefreshReg$) {
37
+ Refresh.injectIntoGlobalHook(window);
38
+ window.$RefreshReg$ = function () {};
39
+ window.$RefreshSig$ = function () {
40
+ return function (type) {
41
+ return type;
42
+ };
43
+ };
44
+ if (typeof window !== 'undefined') {
45
+ let ErrorOverlay = require('react-error-overlay');
46
+ ErrorOverlay.setEditorHandler(function (errorLocation) {
47
+ let file = `${errorLocation.fileName}:${errorLocation.lineNumber || 1}:${errorLocation.colNumber || 1}`;
48
+ fetch(`/__parcel_launch_editor?file=${encodeURIComponent(file)}`);
49
+ });
50
+ ErrorOverlay.startReportingRuntimeErrors({
51
+ onError: function () {}
52
+ });
53
+ window.addEventListener('parcelhmraccept', () => {
54
+ ErrorOverlay.dismissRuntimeErrors();
55
+ });
56
+ }
57
+ }
58
+ };
59
+
60
+ // Everything below is either adapted or copied from
61
+ // https://github.com/facebook/metro/blob/61de16bd1edd7e738dd0311c89555a644023ab2d/packages/metro/src/lib/polyfills/require.js
62
+ // MIT License - Copyright (c) Facebook, Inc. and its affiliates.
63
+
64
+ module.exports.prelude = function (module) {
65
+ window.$RefreshReg$ = function (type, id) {
66
+ Refresh.register(type, module.id + ' ' + id);
67
+ };
68
+ window.$RefreshSig$ = Refresh.createSignatureFunctionForTransform;
69
+ };
70
+ module.exports.postlude = function (module) {
71
+ if (isReactRefreshBoundary(module.exports)) {
72
+ registerExportsForReactRefresh(module);
73
+ if (module.hot) {
74
+ module.hot.dispose(function (data) {
75
+ if (Refresh.hasUnrecoverableErrors()) {
76
+ window.location.reload();
77
+ }
78
+ data.prevExports = module.exports;
79
+ });
80
+ module.hot.accept(function (getParents) {
81
+ var prevExports = module.hot.data.prevExports;
82
+ var nextExports = module.exports;
83
+ // Since we just executed the code for it, it's possible
84
+ // that the new exports make it ineligible for being a boundary.
85
+ var isNoLongerABoundary = !isReactRefreshBoundary(nextExports);
86
+ // It can also become ineligible if its exports are incompatible
87
+ // with the previous exports.
88
+ // For example, if you add/remove/change exports, we'll want
89
+ // to re-execute the importing modules, and force those components
90
+ // to re-render. Similarly, if you convert a class component
91
+ // to a function, we want to invalidate the boundary.
92
+ var didInvalidate = shouldInvalidateReactRefreshBoundary(prevExports, nextExports);
93
+ if (isNoLongerABoundary || didInvalidate) {
94
+ // We'll be conservative. The only case in which we won't do a full
95
+ // reload is if all parent modules are also refresh boundaries.
96
+ // In that case we'll add them to the current queue.
97
+ var parents = getParents();
98
+ if (parents.length === 0) {
99
+ // Looks like we bubbled to the root. Can't recover from that.
100
+ window.location.reload();
101
+ return;
102
+ }
103
+ return parents;
104
+ }
105
+ enqueueUpdate();
106
+ });
107
+ }
108
+ }
109
+ };
110
+ function isReactRefreshBoundary(exports) {
111
+ if (Refresh.isLikelyComponentType(exports)) {
112
+ return true;
113
+ }
114
+ if (exports == null || typeof exports !== 'object') {
115
+ // Exit if we can't iterate over exports.
116
+ return false;
117
+ }
118
+ var hasExports = false;
119
+ var areAllExportsComponents = true;
120
+ let isESM = ('__esModule' in exports);
121
+ for (var key in exports) {
122
+ hasExports = true;
123
+ if (key === '__esModule') {
124
+ continue;
125
+ }
126
+ var desc = Object.getOwnPropertyDescriptor(exports, key);
127
+ if (desc && desc.get && !isESM) {
128
+ // Don't invoke getters for CJS as they may have side effects.
129
+ return false;
130
+ }
131
+ var exportValue = exports[key];
132
+ if (!Refresh.isLikelyComponentType(exportValue)) {
133
+ areAllExportsComponents = false;
134
+ }
135
+ }
136
+ return hasExports && areAllExportsComponents;
137
+ }
138
+ function shouldInvalidateReactRefreshBoundary(prevExports, nextExports) {
139
+ var prevSignature = getRefreshBoundarySignature(prevExports);
140
+ var nextSignature = getRefreshBoundarySignature(nextExports);
141
+ if (prevSignature.length !== nextSignature.length) {
142
+ return true;
143
+ }
144
+ for (var i = 0; i < nextSignature.length; i++) {
145
+ if (prevSignature[i] !== nextSignature[i]) {
146
+ return true;
147
+ }
148
+ }
149
+ return false;
150
+ }
151
+
152
+ // When this signature changes, it's unsafe to stop at this refresh boundary.
153
+ function getRefreshBoundarySignature(exports) {
154
+ var signature = [];
155
+ signature.push(Refresh.getFamilyByType(exports));
156
+ if (exports == null || typeof exports !== 'object') {
157
+ // Exit if we can't iterate over exports.
158
+ // (This is important for legacy environments.)
159
+ return signature;
160
+ }
161
+ let isESM = ('__esModule' in exports);
162
+ for (var key in exports) {
163
+ if (key === '__esModule') {
164
+ continue;
165
+ }
166
+ var desc = Object.getOwnPropertyDescriptor(exports, key);
167
+ if (desc && desc.get && !isESM) {
168
+ // Don't invoke getters for CJS as they may have side effects.
169
+ continue;
170
+ }
171
+ var exportValue = exports[key];
172
+ signature.push(key);
173
+ signature.push(Refresh.getFamilyByType(exportValue));
174
+ }
175
+ return signature;
176
+ }
177
+ function registerExportsForReactRefresh(module) {
178
+ var exports = module.exports,
179
+ id = module.id;
180
+ Refresh.register(exports, id + ' %exports%');
181
+ if (exports == null || typeof exports !== 'object') {
182
+ // Exit if we can't iterate over exports.
183
+ // (This is important for legacy environments.)
184
+ return;
185
+ }
186
+ let isESM = ('__esModule' in exports);
187
+ for (var key in exports) {
188
+ var desc = Object.getOwnPropertyDescriptor(exports, key);
189
+ if (desc && desc.get && !isESM) {
190
+ // Don't invoke getters for CJS as they may have side effects.
191
+ continue;
192
+ }
193
+ var exportValue = exports[key];
194
+ var typeID = id + ' %exports% ' + key;
195
+ Refresh.register(exportValue, typeID);
196
+ }
197
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaspack/transformer-react-refresh-wrap",
3
- "version": "2.14.11",
3
+ "version": "2.14.13",
4
4
  "license": "(MIT OR Apache-2.0)",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -15,9 +15,11 @@
15
15
  "node": ">= 16.0.0"
16
16
  },
17
17
  "dependencies": {
18
- "@atlaspack/plugin": "2.14.11",
19
- "@atlaspack/utils": "2.14.11",
20
- "react-refresh": ">=0.9 <=0.14"
18
+ "@atlaspack/feature-flags": "2.18.1",
19
+ "@atlaspack/plugin": "2.14.13",
20
+ "@atlaspack/utils": "2.15.1",
21
+ "react-error-overlay": "6.0.9",
22
+ "react-refresh": ">=0.9 <=0.16"
21
23
  },
22
24
  "type": "commonjs"
23
25
  }
@@ -2,6 +2,7 @@
2
2
 
3
3
  import path from 'path';
4
4
  import {Transformer} from '@atlaspack/plugin';
5
+ import {getFeatureFlag} from '@atlaspack/feature-flags';
5
6
 
6
7
  function shouldExclude(asset, options) {
7
8
  return (
@@ -32,15 +33,20 @@ export default (new Transformer({
32
33
  return [asset];
33
34
  }
34
35
 
36
+ const helperFilename = getFeatureFlag('hmrImprovements')
37
+ ? 'helpers-new.js'
38
+ : 'helpers.js';
39
+
35
40
  let wrapperPath = `@atlaspack/transformer-react-refresh-wrap/${path.basename(
36
41
  __dirname,
37
- )}/helpers/helpers.js`;
42
+ )}/helpers/${helperFilename}`;
38
43
 
39
44
  let code = await asset.getCode();
40
45
  let map = await asset.getMap();
41
46
  let name = `$parcel$ReactRefreshHelpers$${asset.id.slice(-4)}`;
42
47
 
43
48
  code = `var ${name} = require(${JSON.stringify(wrapperPath)});
49
+ ${getFeatureFlag('hmrImprovements') ? `${name}.init();` : ''}
44
50
  var prevRefreshReg = window.$RefreshReg$;
45
51
  var prevRefreshSig = window.$RefreshSig$;
46
52
  ${name}.prelude(module);
@@ -0,0 +1,212 @@
1
+ var Refresh = require('react-refresh/runtime');
2
+
3
+ function debounce(func, delay) {
4
+ if (process.env.NODE_ENV === 'test') {
5
+ return function (args) {
6
+ func.call(null, args);
7
+ };
8
+ } else {
9
+ let timeout = undefined;
10
+ let lastTime = 0;
11
+ return function (args) {
12
+ // Call immediately if last call was more than the delay ago.
13
+ // Otherwise, set a timeout. This means the first call is fast
14
+ // (for the common case of a single update), and subsequent updates
15
+ // are batched.
16
+ let now = Date.now();
17
+ if (now - lastTime > delay) {
18
+ lastTime = now;
19
+ func.call(null, args);
20
+ } else {
21
+ clearTimeout(timeout);
22
+ timeout = setTimeout(function () {
23
+ timeout = undefined;
24
+ lastTime = Date.now();
25
+ func.call(null, args);
26
+ }, delay);
27
+ }
28
+ };
29
+ }
30
+ }
31
+ var enqueueUpdate = debounce(function () {
32
+ Refresh.performReactRefresh();
33
+ }, 30);
34
+
35
+ module.exports.init = function () {
36
+ if (!window.$RefreshReg$) {
37
+ Refresh.injectIntoGlobalHook(window);
38
+ window.$RefreshReg$ = function () {};
39
+ window.$RefreshSig$ = function () {
40
+ return function (type) {
41
+ return type;
42
+ };
43
+ };
44
+
45
+ if (typeof window !== 'undefined') {
46
+ let ErrorOverlay = require('react-error-overlay');
47
+ ErrorOverlay.setEditorHandler(function editorHandler(errorLocation) {
48
+ let file = `${errorLocation.fileName}:${
49
+ errorLocation.lineNumber || 1
50
+ }:${errorLocation.colNumber || 1}`;
51
+ fetch(`/__parcel_launch_editor?file=${encodeURIComponent(file)}`);
52
+ });
53
+
54
+ ErrorOverlay.startReportingRuntimeErrors({
55
+ onError: function () {},
56
+ });
57
+
58
+ window.addEventListener('parcelhmraccept', () => {
59
+ ErrorOverlay.dismissRuntimeErrors();
60
+ });
61
+ }
62
+ }
63
+ };
64
+
65
+ // Everything below is either adapted or copied from
66
+ // https://github.com/facebook/metro/blob/61de16bd1edd7e738dd0311c89555a644023ab2d/packages/metro/src/lib/polyfills/require.js
67
+ // MIT License - Copyright (c) Facebook, Inc. and its affiliates.
68
+
69
+ module.exports.prelude = function (module) {
70
+ window.$RefreshReg$ = function (type, id) {
71
+ Refresh.register(type, module.id + ' ' + id);
72
+ };
73
+ window.$RefreshSig$ = Refresh.createSignatureFunctionForTransform;
74
+ };
75
+
76
+ module.exports.postlude = function (module) {
77
+ if (isReactRefreshBoundary(module.exports)) {
78
+ registerExportsForReactRefresh(module);
79
+
80
+ if (module.hot) {
81
+ module.hot.dispose(function (data) {
82
+ if (Refresh.hasUnrecoverableErrors()) {
83
+ window.location.reload();
84
+ }
85
+
86
+ data.prevExports = module.exports;
87
+ });
88
+
89
+ module.hot.accept(function (getParents) {
90
+ var prevExports = module.hot.data.prevExports;
91
+ var nextExports = module.exports;
92
+ // Since we just executed the code for it, it's possible
93
+ // that the new exports make it ineligible for being a boundary.
94
+ var isNoLongerABoundary = !isReactRefreshBoundary(nextExports);
95
+ // It can also become ineligible if its exports are incompatible
96
+ // with the previous exports.
97
+ // For example, if you add/remove/change exports, we'll want
98
+ // to re-execute the importing modules, and force those components
99
+ // to re-render. Similarly, if you convert a class component
100
+ // to a function, we want to invalidate the boundary.
101
+ var didInvalidate = shouldInvalidateReactRefreshBoundary(
102
+ prevExports,
103
+ nextExports,
104
+ );
105
+ if (isNoLongerABoundary || didInvalidate) {
106
+ // We'll be conservative. The only case in which we won't do a full
107
+ // reload is if all parent modules are also refresh boundaries.
108
+ // In that case we'll add them to the current queue.
109
+ var parents = getParents();
110
+ if (parents.length === 0) {
111
+ // Looks like we bubbled to the root. Can't recover from that.
112
+ window.location.reload();
113
+ return;
114
+ }
115
+ return parents;
116
+ }
117
+ enqueueUpdate();
118
+ });
119
+ }
120
+ }
121
+ };
122
+
123
+ function isReactRefreshBoundary(exports) {
124
+ if (Refresh.isLikelyComponentType(exports)) {
125
+ return true;
126
+ }
127
+ if (exports == null || typeof exports !== 'object') {
128
+ // Exit if we can't iterate over exports.
129
+ return false;
130
+ }
131
+ var hasExports = false;
132
+ var areAllExportsComponents = true;
133
+ let isESM = '__esModule' in exports;
134
+ for (var key in exports) {
135
+ hasExports = true;
136
+ if (key === '__esModule') {
137
+ continue;
138
+ }
139
+ var desc = Object.getOwnPropertyDescriptor(exports, key);
140
+ if (desc && desc.get && !isESM) {
141
+ // Don't invoke getters for CJS as they may have side effects.
142
+ return false;
143
+ }
144
+ var exportValue = exports[key];
145
+ if (!Refresh.isLikelyComponentType(exportValue)) {
146
+ areAllExportsComponents = false;
147
+ }
148
+ }
149
+ return hasExports && areAllExportsComponents;
150
+ }
151
+
152
+ function shouldInvalidateReactRefreshBoundary(prevExports, nextExports) {
153
+ var prevSignature = getRefreshBoundarySignature(prevExports);
154
+ var nextSignature = getRefreshBoundarySignature(nextExports);
155
+ if (prevSignature.length !== nextSignature.length) {
156
+ return true;
157
+ }
158
+ for (var i = 0; i < nextSignature.length; i++) {
159
+ if (prevSignature[i] !== nextSignature[i]) {
160
+ return true;
161
+ }
162
+ }
163
+ return false;
164
+ }
165
+
166
+ // When this signature changes, it's unsafe to stop at this refresh boundary.
167
+ function getRefreshBoundarySignature(exports) {
168
+ var signature = [];
169
+ signature.push(Refresh.getFamilyByType(exports));
170
+ if (exports == null || typeof exports !== 'object') {
171
+ // Exit if we can't iterate over exports.
172
+ // (This is important for legacy environments.)
173
+ return signature;
174
+ }
175
+ let isESM = '__esModule' in exports;
176
+ for (var key in exports) {
177
+ if (key === '__esModule') {
178
+ continue;
179
+ }
180
+ var desc = Object.getOwnPropertyDescriptor(exports, key);
181
+ if (desc && desc.get && !isESM) {
182
+ // Don't invoke getters for CJS as they may have side effects.
183
+ continue;
184
+ }
185
+ var exportValue = exports[key];
186
+ signature.push(key);
187
+ signature.push(Refresh.getFamilyByType(exportValue));
188
+ }
189
+ return signature;
190
+ }
191
+
192
+ function registerExportsForReactRefresh(module) {
193
+ var exports = module.exports,
194
+ id = module.id;
195
+ Refresh.register(exports, id + ' %exports%');
196
+ if (exports == null || typeof exports !== 'object') {
197
+ // Exit if we can't iterate over exports.
198
+ // (This is important for legacy environments.)
199
+ return;
200
+ }
201
+ let isESM = '__esModule' in exports;
202
+ for (var key in exports) {
203
+ var desc = Object.getOwnPropertyDescriptor(exports, key);
204
+ if (desc && desc.get && !isESM) {
205
+ // Don't invoke getters for CJS as they may have side effects.
206
+ continue;
207
+ }
208
+ var exportValue = exports[key];
209
+ var typeID = id + ' %exports% ' + key;
210
+ Refresh.register(exportValue, typeID);
211
+ }
212
+ }