Skip to content

Commit b224987

Browse files
Update ReactOnRails package imports and enhance npm package resolution
- Introduced a method to determine the appropriate npm package for ReactOnRails, allowing for dynamic imports based on the environment (Pro or standard). - Updated import statements across various components to utilize the new package resolution method, ensuring consistency and reducing hardcoded paths. - Modified the `package.json` preinstall script to include linking for both react-on-rails and react-on-rails-pro, improving setup for development environments. - Adjusted yarn.lock to reflect the new linking structure for the react-on-rails package. These changes enhance the modularity and maintainability of the React on Rails framework.
1 parent ba8dd11 commit b224987

File tree

10 files changed

+23
-18
lines changed

10 files changed

+23
-18
lines changed

lib/react_on_rails/packs_generator.rb

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ def self.instance
1616
@instance ||= PacksGenerator.new
1717
end
1818

19+
def react_on_rails_npm_package
20+
return "react-on-rails-pro" if ReactOnRails::Utils.react_on_rails_pro?
21+
22+
"react-on-rails"
23+
end
24+
1925
def generate_packs_if_stale
2026
return unless ReactOnRails.configuration.auto_load_bundle
2127

@@ -104,7 +110,7 @@ def pack_file_contents(file_path)
104110

105111
if load_server_components && !client_entrypoint?(file_path)
106112
return <<~FILE_CONTENT.strip
107-
import registerServerComponent from 'react-on-rails/registerServerComponent/client';
113+
import registerServerComponent from '#{react_on_rails_npm_package}/registerServerComponent/client';
108114
109115
registerServerComponent("#{registered_component_name}");
110116
FILE_CONTENT
@@ -113,7 +119,7 @@ def pack_file_contents(file_path)
113119
relative_component_path = relative_component_path_from_generated_pack(file_path)
114120

115121
<<~FILE_CONTENT.strip
116-
import ReactOnRails from 'react-on-rails/client';
122+
import ReactOnRails from '#{react_on_rails_npm_package}/client';
117123
import #{registered_component_name} from '#{relative_component_path}';
118124
119125
ReactOnRails.register({#{registered_component_name}});
@@ -129,14 +135,14 @@ def create_server_pack
129135

130136
def build_server_pack_content(component_on_server_imports, server_components, client_components)
131137
content = <<~FILE_CONTENT
132-
import ReactOnRails from 'react-on-rails';
138+
import ReactOnRails from '#{react_on_rails_npm_package}';
133139
134140
#{component_on_server_imports.join("\n")}\n
135141
FILE_CONTENT
136142

137143
if server_components.any?
138144
content += <<~FILE_CONTENT
139-
import registerServerComponent from 'react-on-rails/registerServerComponent/server';
145+
import registerServerComponent from '#{react_on_rails_npm_package}/registerServerComponent/server';
140146
registerServerComponent({#{server_components.join(",\n")}});\n
141147
FILE_CONTENT
142148
end

react_on_rails_pro/spec/dummy/client/app/components/AsyncOnServerSyncOnClient.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import * as React from 'react';
44
import { Suspense, useEffect } from 'react';
5-
import RSCRoute from 'react-on-rails/RSCRoute';
5+
import RSCRoute from 'react-on-rails-pro/RSCRoute';
66

77
const AsyncComponentOnServer = async ({
88
promise,

react_on_rails_pro/spec/dummy/client/app/components/ServerComponentRouter.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { Suspense } from 'react';
22
import { Routes, Route, Link } from 'react-router-dom';
3-
import RSCRoute from 'react-on-rails/RSCRoute';
3+
import RSCRoute from 'react-on-rails-pro/RSCRoute';
44
// @ts-expect-error - EchoProps is a JavaScript file without TypeScript types
55
import EchoProps from './EchoProps';
66
import { ErrorBoundary } from './ErrorBoundary';

react_on_rails_pro/spec/dummy/client/app/components/ServerComponentWithRetry.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { useState } from 'react';
22
import { ErrorBoundary } from 'react-error-boundary';
3-
import RSCRoute from 'react-on-rails/RSCRoute';
4-
import { useRSC } from 'react-on-rails/RSCProvider';
5-
import { isServerComponentFetchError } from 'react-on-rails/ServerComponentFetchError';
3+
import RSCRoute from 'react-on-rails-pro/RSCRoute';
4+
import { useRSC } from 'react-on-rails-pro/RSCProvider';
5+
import { isServerComponentFetchError } from 'react-on-rails-pro/ServerComponentFetchError';
66

77
const ErrorFallback = ({ error, resetErrorBoundary }: { error: Error; resetErrorBoundary: () => void }) => {
88
const { refetchComponent } = useRSC();
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import wrapServerComponentRenderer from 'react-on-rails/wrapServerComponentRenderer/client';
3+
import wrapServerComponentRenderer from 'react-on-rails-pro/wrapServerComponentRenderer/client';
44
import AsyncOnServerSyncOnClient from '../components/AsyncOnServerSyncOnClient';
55

66
export default wrapServerComponentRenderer(AsyncOnServerSyncOnClient);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import wrapServerComponentRenderer from 'react-on-rails/wrapServerComponentRenderer/server';
3+
import wrapServerComponentRenderer from 'react-on-rails-pro/wrapServerComponentRenderer/server';
44
import AsyncOnServerSyncOnClient from '../components/AsyncOnServerSyncOnClient';
55

66
export default wrapServerComponentRenderer(AsyncOnServerSyncOnClient);

react_on_rails_pro/spec/dummy/client/app/ror-auto-load-components/ServerComponentRouter.client.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import * as React from 'react';
44
import { BrowserRouter } from 'react-router-dom';
5-
import wrapServerComponentRenderer from 'react-on-rails/wrapServerComponentRenderer/client';
5+
import wrapServerComponentRenderer from 'react-on-rails-pro/wrapServerComponentRenderer/client';
66
import App from '../components/ServerComponentRouter';
77

88
function ClientComponentRouter(props: object) {

react_on_rails_pro/spec/dummy/client/app/ror-auto-load-components/ServerComponentRouter.server.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import * as React from 'react';
44
import { StaticRouter } from 'react-router-dom/server.js';
55
import { RailsContext, ReactComponentOrRenderFunction } from 'react-on-rails-pro';
6-
import wrapServerComponentRenderer from 'react-on-rails/wrapServerComponentRenderer/server';
6+
import wrapServerComponentRenderer from 'react-on-rails-pro/wrapServerComponentRenderer/server';
77
import App from '../components/ServerComponentRouter';
88

99
function ServerComponentRouter(props: object, railsContext: RailsContext) {

react_on_rails_pro/spec/dummy/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"scripts": {
9696
"test": "yarn run build:test && yarn run lint && rspec",
9797
"lint": "cd ../.. && nps lint",
98-
"preinstall": "yarn run link-source && yalc add --link react-on-rails-pro && yalc add --link @shakacode-tools/react-on-rails-pro-node-renderer",
98+
"preinstall": "yarn run link-source && yalc add --link react-on-rails-pro && cd .yalc/react-on-rails-pro && yalc add --link react-on-rails && cd ../.. && yalc add --link @shakacode-tools/react-on-rails-pro-node-renderer",
9999
"link-source": "cd ../../.. && yarn && yarn run yalc:publish",
100100
"postinstall": "test -f post-yarn-install.local && ./post-yarn-install.local || true",
101101
"build:test": "rm -rf public/webpack/test && rm -rf ssr-generated && RAILS_ENV=test NODE_ENV=test bin/shakapacker",

react_on_rails_pro/spec/dummy/yarn.lock

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5357,10 +5357,9 @@ react-on-rails-rsc@^19.0.2:
53575357
neo-async "^2.6.1"
53585358
webpack-sources "^3.2.0"
53595359

5360-
react-on-rails@*:
5361-
version "16.1.1"
5362-
resolved "https://registry.yarnpkg.com/react-on-rails/-/react-on-rails-16.1.1.tgz#bf5e752c44381252204482342ae5722d9f45f715"
5363-
integrity sha512-Ntw/4HSB/p9QJ1V2kc0aETzK0W0Vy0suSh0Ugs3Ctfso2ovIT2YUegJJyPtFzX9jUZSR6Q/tkmkgNgzASkO0pw==
5360+
"react-on-rails@link:.yalc/react-on-rails-pro/.yalc/react-on-rails":
5361+
version "0.0.0"
5362+
uid ""
53645363

53655364
react-proptypes@^1.0.0:
53665365
version "1.0.0"

0 commit comments

Comments
 (0)