-
-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
examples: Add monorepo without workspaces
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
Showing
22 changed files
with
313 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PORT=3000 |
Binary file not shown.
2 changes: 2 additions & 0 deletions
2
examples/with-monorepo-without-workspaces/basic-1/public/robots.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
User-agent: * | ||
|
13 changes: 13 additions & 0 deletions
13
examples/with-monorepo-without-workspaces/basic-1/src/App.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
examples/with-monorepo-without-workspaces/basic-1/src/App.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
examples/with-monorepo-without-workspaces/basic-1/src/client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
examples/with-monorepo-without-workspaces/basic-1/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
examples/with-monorepo-without-workspaces/basic-1/src/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
PORT=3002 |
Binary file not shown.
2 changes: 2 additions & 0 deletions
2
examples/with-monorepo-without-workspaces/basic-2/public/robots.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
User-agent: * | ||
|
13 changes: 13 additions & 0 deletions
13
examples/with-monorepo-without-workspaces/basic-2/src/App.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
examples/with-monorepo-without-workspaces/basic-2/src/App.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
examples/with-monorepo-without-workspaces/basic-2/src/client.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
examples/with-monorepo-without-workspaces/basic-2/src/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
examples/with-monorepo-without-workspaces/basic-2/src/server.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters