Skip to content

Commit

Permalink
examples: Add monorepo without workspaces
Browse files Browse the repository at this point in the history
This is similar to the monorepo example, but adapted for the case where you want
to build multiple separate applications that share the same dependencies (and
therefore share the same package.json in the root).
  • Loading branch information
squiddy committed May 5, 2021
1 parent f1fdf1c commit 3c6a24f
Show file tree
Hide file tree
Showing 22 changed files with 313 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/with-monorepo-without-workspaces/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
logs
*.log
npm-debug.log*
.DS_Store

coverage
node_modules
build
public/static
.env.*.local
21 changes: 21 additions & 0 deletions examples/with-monorepo-without-workspaces/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Razzle Monorepo without workspaces Example

## How to use

<!-- START install generated instructions please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN yarn update-examples TO UPDATE -->Create and start the example:

```bash
npx create-razzle-app --example with-monorepo-without-workspaces with-monorepo-without-workspaces

cd with-monorepo-without-workspaces
yarn start
```
<!-- END install generated instructions please keep comment here to allow auto update -->


## Idea behind the example

This is similar to the monorepo example, but adapted for the case where you want
to build multiple separate applications that share the same dependencies (and
therefore share the same package.json in the root).
1 change: 1 addition & 0 deletions examples/with-monorepo-without-workspaces/basic-1/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=3000
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *

13 changes: 13 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-1/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
padding: 0;
font-family: -apple-system,
BlinkMacSystemFont,
"Segoe UI",
Helvetica,
Arial,
sans-serif,
"Apple Color Emoji",
"Segoe UI Emoji",
"Segoe UI Symbol";
}
6 changes: 6 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-1/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import './App.css';

import React from 'react';
const App = () => <div>Welcome to Razzle.</div>;

export default App;
10 changes: 10 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-1/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import App from './App';
import React from 'react';
import ReactDOM from 'react-dom';

describe('<App />', () => {
test('renders without exploding', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import { hydrate } from 'react-dom';
import App from './App';

hydrate(<App />, document.getElementById('root'));

if (module.hot) {
module.hot.accept();
}
27 changes: 27 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-1/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import express from 'express';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}

const port = process.env.PORT || 3000;

export default express()
.use((req, res) => app.handle(req, res))
.listen(port, function(err) {
if (err) {
console.error(err);
return;
}
console.log(`> Started on port ${port}`);
});
56 changes: 56 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-1/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import path from 'path';
import App from './App';
import React from 'react';
import express from 'express';
import { renderToString } from 'react-dom/server';

const assets = require(process.env.RAZZLE_ASSETS_MANIFEST);

const cssLinksFromAssets = (assets, entrypoint) => {
return assets[entrypoint] ? assets[entrypoint].css ?
assets[entrypoint].css.map(asset=>
`<link rel="stylesheet" href="${asset}">`
).join('') : '' : '';
};

const jsScriptTagsFromAssets = (assets, entrypoint, extra = '') => {
return assets[entrypoint] ? assets[entrypoint].js ?
assets[entrypoint].js.map(asset=>
`<script src="${asset}"${extra}></script>`
).join('') : '' : '';
};

export const renderApp = (req, res) => {
const markup = renderToString(<App />);

const html =
// prettier-ignore
`<!doctype html>
<html lang="">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charSet='utf-8' />
<title>Welcome to Razzle</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${cssLinksFromAssets(assets, 'client')}
</head>
<body>
<div id="root">${markup}</div>
${jsScriptTagsFromAssets(assets, 'client', ' defer crossorigin')}
</body>
</html>`;

return { html };
};

const server = express();

server
.disable('x-powered-by')
.use(express.static(process.env.RAZZLE_PUBLIC_DIR))
.get('/*', (req, res) => {
const { html } = renderApp(req, res);
res.send(html);
});

export default server;
1 change: 1 addition & 0 deletions examples/with-monorepo-without-workspaces/basic-2/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=3002
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
User-agent: *

13 changes: 13 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-2/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
padding: 0;
font-family: -apple-system,
BlinkMacSystemFont,
"Segoe UI",
Helvetica,
Arial,
sans-serif,
"Apple Color Emoji",
"Segoe UI Emoji",
"Segoe UI Symbol";
}
6 changes: 6 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-2/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import './App.css';

import React from 'react';
const App = () => <div>Welcome to Razzle.</div>;

export default App;
10 changes: 10 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-2/src/App.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import App from './App';
import React from 'react';
import ReactDOM from 'react-dom';

describe('<App />', () => {
test('renders without exploding', () => {
const div = document.createElement('div');
ReactDOM.render(<App />, div);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import { hydrate } from 'react-dom';
import App from './App';

hydrate(<App />, document.getElementById('root'));

if (module.hot) {
module.hot.accept();
}
27 changes: 27 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-2/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import express from 'express';

let app = require('./server').default;

if (module.hot) {
module.hot.accept('./server', function() {
console.log('🔁 HMR Reloading `./server`...');
try {
app = require('./server').default;
} catch (error) {
console.error(error);
}
});
console.info('✅ Server-side HMR Enabled!');
}

const port = process.env.PORT || 3000;

export default express()
.use((req, res) => app.handle(req, res))
.listen(port, function(err) {
if (err) {
console.error(err);
return;
}
console.log(`> Started on port ${port}`);
});
56 changes: 56 additions & 0 deletions examples/with-monorepo-without-workspaces/basic-2/src/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import path from 'path';
import App from './App';
import React from 'react';
import express from 'express';
import { renderToString } from 'react-dom/server';

const assets = require(process.env.RAZZLE_ASSETS_MANIFEST);

const cssLinksFromAssets = (assets, entrypoint) => {
return assets[entrypoint] ? assets[entrypoint].css ?
assets[entrypoint].css.map(asset=>
`<link rel="stylesheet" href="${asset}">`
).join('') : '' : '';
};

const jsScriptTagsFromAssets = (assets, entrypoint, extra = '') => {
return assets[entrypoint] ? assets[entrypoint].js ?
assets[entrypoint].js.map(asset=>
`<script src="${asset}"${extra}></script>`
).join('') : '' : '';
};

export const renderApp = (req, res) => {
const markup = renderToString(<App />);

const html =
// prettier-ignore
`<!doctype html>
<html lang="">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta charSet='utf-8' />
<title>Welcome to Razzle</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
${cssLinksFromAssets(assets, 'client')}
</head>
<body>
<div id="root">${markup}</div>
${jsScriptTagsFromAssets(assets, 'client', ' defer crossorigin')}
</body>
</html>`;

return { html };
};

const server = express();

server
.disable('x-powered-by')
.use(express.static(process.env.RAZZLE_PUBLIC_DIR))
.get('/*', (req, res) => {
const { html } = renderApp(req, res);
res.send(html);
});

export default server;
33 changes: 33 additions & 0 deletions examples/with-monorepo-without-workspaces/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"private": true,
"name": "razzle-examples-with-monorepo-without-workspaces",
"version": "4.0.4",
"license": "MIT",
"scripts": {
"start-1": "cross-env RAZZLE_APP_PATH=basic-1 razzle start",
"build-1": "cross-env RAZZLE_APP_PATH=basic-1 razzle build",
"test-1": "cross-env RAZZLE_APP_PATH=basic-1 razzle test --env=jsdom",
"start:prod-1": "cross-env RAZZLE_APP_PATH=basic-1 NODE_ENV=production node basic-1/build/server.js",
"start-2": "cross-env RAZZLE_APP_PATH=basic-2 razzle start",
"build-2": "cross-env RAZZLE_APP_PATH=basic-2 razzle build",
"test-2": "cross-env RAZZLE_APP_PATH=basic-2 razzle test --env=jsdom",
"start:prod-2": "cross-env RAZZLE_APP_PATH=basic-2 NODE_ENV=production node basic-2/build/server.js",
"start": "concurrently \"yarn start-1\" \"yarn start-2\"",
"start:prod": "concurrently \"yarn start:prod-1\" \"yarn start:prod-2\"",
"build": "concurrently \"yarn build-1 --noninteractive\" \"yarn build-2 --noninteractive\""
},
"devDependencies": {
"concurrently": "^5.3.0",
"cross-env": "^7.0.3",
"razzle": "4.0.4",
"razzle-dev-utils": "4.0.4",
"mini-css-extract-plugin": "^0.9.0",
"html-webpack-plugin": "^4.5.2",
"webpack": "^4.44.1",
"babel-preset-razzle": "4.0.4",
"webpack-dev-server": "^3.11.2",
"express": "^4.17.1",
"react": "^17.0.1",
"react-dom": "^17.0.1"
}
}
1 change: 1 addition & 0 deletions test/examples/isomorphic-examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ let examples =
}
].filter(x=>x), complex: [
{ example: 'with-monorepo', path: 'examples/with-monorepo' }, // test timing ssues
{ example: 'with-monorepo-without-workspaces', path: 'examples/with-monorepo-without-workspaces' }, // test timing ssues
{
example: 'with-module-federation', // test timing ssues
path: 'examples/with-module-federation'
Expand Down

0 comments on commit 3c6a24f

Please sign in to comment.