Skip to content

Commit 289c189

Browse files
Refactor context handling and update package exports
1 parent 34dae0d commit 289c189

File tree

5 files changed

+44
-28
lines changed

5 files changed

+44
-28
lines changed
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import ReactOnRails from './ReactOnRails';
2+
import { setReactOnRails } from './context';
23
import streamServerRenderedReactComponent from './streamServerRenderedReactComponent';
34

45
ReactOnRails.streamServerRenderedReactComponent = streamServerRenderedReactComponent;
56

6-
export * from './ReactOnRails';
7-
export { default } from './ReactOnRails';
7+
setReactOnRails(ReactOnRails);
8+
9+
export default ReactOnRails;
10+
export * from './types';

node_package/src/ReactOnRails.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import serverRenderReactComponent from './serverRenderReactComponent';
88
import buildConsoleReplay from './buildConsoleReplay';
99
import createReactOutput from './createReactOutput';
1010
import Authenticity from './Authenticity';
11-
import context from './context';
1211
import type {
1312
RegisteredComponent,
1413
RenderParams,
@@ -19,31 +18,16 @@ import type {
1918
AuthenticityHeaders,
2019
Store,
2120
StoreGenerator,
21+
ReactOnRails as ReactOnRailsType,
2222
} from './types';
2323
import reactHydrateOrRender from './reactHydrateOrRender';
2424

25-
const ctx = context();
26-
27-
if (ctx === undefined) {
28-
throw new Error("The context (usually Window or NodeJS's Global) is undefined.");
29-
}
30-
31-
if (ctx.ReactOnRails !== undefined) {
32-
throw new Error(`
33-
The ReactOnRails value exists in the ${ctx} scope, it may not be safe to overwrite it.
34-
35-
This could be caused by setting Webpack's optimization.runtimeChunk to "true" or "multiple," rather than "single." Check your Webpack configuration.
36-
37-
Read more at https://github.com/shakacode/react_on_rails/issues/1558.
38-
`);
39-
}
40-
4125
const DEFAULT_OPTIONS = {
4226
traceTurbolinks: false,
4327
turbo: false,
4428
};
4529

46-
ctx.ReactOnRails = {
30+
const ReactOnRails: ReactOnRailsType = {
4731
options: {},
4832
/**
4933
* Main entry point to using the react-on-rails npm package. This is how Rails will be able to
@@ -299,9 +283,4 @@ ctx.ReactOnRails = {
299283
},
300284
};
301285

302-
ctx.ReactOnRails.resetOptions();
303-
304-
ClientStartup.clientStartup(ctx);
305-
306-
export * from "./types";
307-
export default ctx.ReactOnRails;
286+
export default ReactOnRails;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import ReactOnRails from './ReactOnRails';
2+
import { setReactOnRails } from './context';
3+
4+
setReactOnRails(ReactOnRails);
5+
6+
export default ReactOnRails;
7+
export * from './types';

node_package/src/context.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,36 @@
1+
import { ReactOnRails as ReactOnRailsType } from './types';
2+
import * as ClientStartup from './clientStartup';
3+
14
/**
25
* Get the context, be it window or global
36
* @returns {boolean|Window|*|context}
47
*/
5-
export default function context(this: void): Window | NodeJS.Global | void {
8+
export function context(this: void): Window | NodeJS.Global | void {
69
return ((typeof window !== 'undefined') && window) ||
710
((typeof global !== 'undefined') && global) ||
811
this;
912
}
13+
14+
export const setReactOnRails = (value: ReactOnRailsType) => {
15+
const ctx = context();
16+
17+
if (ctx === undefined) {
18+
throw new Error("The context (usually Window or NodeJS's Global) is undefined.");
19+
}
20+
21+
if (ctx.ReactOnRails !== undefined) {
22+
throw new Error(`
23+
The ReactOnRails value exists in the ${ctx} scope, it may not be safe to overwrite it.
24+
25+
This could be caused by setting Webpack's optimization.runtimeChunk to "true" or "multiple," rather than "single." Check your Webpack configuration.
26+
27+
Read more at https://github.com/shakacode/react_on_rails/issues/1558.
28+
`);
29+
}
30+
31+
ctx.ReactOnRails = value;
32+
33+
ctx.ReactOnRails.resetOptions();
34+
35+
ClientStartup.clientStartup(ctx);
36+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"exports": {
66
".": {
77
"node": "./node_package/lib/ReactOnRails.node.js",
8-
"default": "./node_package/lib/ReactOnRails.js"
8+
"default": "./node_package/lib/ReactOnRails.web.js"
99
}
1010
},
1111
"directories": {

0 commit comments

Comments
 (0)