Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: webpack config changes #419

Merged
merged 13 commits into from
Apr 22, 2022
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ src/generated/
dist/
node_modules/
tsd/
webpack.config.ts
webpack.common.config.ts
anrusina marked this conversation as resolved.
Show resolved Hide resolved
webpack.dev.config.ts
webpack.prod.config.ts
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ yarn install
```
export ADMIN_API_URL=https://different.admin.service.com
export ADMIN_API_USE_SSL="https"
export LOCAL_DEV_HOST=localhost.different.admin.service.com
```

_NOTE:_ Add these to your local profile (e.g., `./profile`) to prevent having to do this step each time
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ More info on each section could be found at [CONTRIBUTING.md](CONTRIBUTING.md)
```bash
export ADMIN_API_URL=https://different.admin.service.com
export ADMIN_API_USE_SSL="https"
export LOCAL_DEV_HOST=localhost.different.admin.service.com
```

> **Hint:** Add these to your local profile (eg, `./profile`) to prevent having to do this step each time
Expand Down
3 changes: 3 additions & 0 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const ADMIN_API_URL = process.env.ADMIN_API_URL;
// Use this to create SSL server
const ADMIN_API_USE_SSL = process.env.ADMIN_API_USE_SSL || 'http';

const LOCAL_DEV_HOST = process.env.LOCAL_DEV_HOST;

const BASE_URL = process.env.BASE_URL || '';

// Defines a file to be required which will provide implementations for
Expand Down Expand Up @@ -42,5 +44,6 @@ module.exports = {
GA_TRACKING_ID,
NODE_ENV,
STATUS_URL,
LOCAL_DEV_HOST,
},
};
69 changes: 14 additions & 55 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
/* eslint-disable no-console */
const morgan = require('morgan');
const express = require('express');
const path = require('path');
const expressStaticGzip = require('express-static-gzip');
const serverRenderer = require('./dist/server.js').default;
const clientStats = require('./dist/client-stats.json');

const env = require('./env');
const { applyMiddleware } = require('./plugins');

Expand All @@ -21,61 +26,15 @@ if (typeof applyMiddleware === 'function') {
applyMiddleware(app);
}

if (process.env.NODE_ENV === 'production') {
const path = require('path');
const expressStaticGzip = require('express-static-gzip');
const serverRenderer = require('./dist/server.js').default;
const clientStats = require('./dist/client-stats.json');
const distPath = path.join(__dirname, 'dist');
app.use(
// This path should be in sync with the `publicPath` from webpack config.
`${env.BASE_URL}/assets`,
expressStaticGzip(distPath, {
maxAge: '1d',
}),
);
app.use(serverRenderer({ clientStats, currentDirectory: __dirname }));
} else {
process.env.NODE_ENV = 'development';
console.log('Server is running in development mode');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleWare = require('webpack-hot-middleware');
const webpackHotServerMiddleware = require('webpack-hot-server-middleware');

require('ts-node').register({
compilerOptions: { module: 'commonjs' },
cacheDirectory: '/tmp',
});
const { default: configs, clientConfig } = require('./webpack.config');

const compiler = webpack(
configs.map((c) =>
Object.assign({}, c, {
mode: 'development',
}),
),
);
const clientCompiler = compiler.compilers.find(({ name }) => name === 'client');

const devMiddleware = webpackDevMiddleware(compiler, {
serverSideRender: true,
publicPath: clientConfig.output.publicPath,
});

app.use(devMiddleware);
app.use(webpackHotMiddleWare(clientCompiler));
app.use(
webpackHotServerMiddleware(compiler, {
serverRendererOptions: {
// Send client compiler FS for reading index.html for emitted assets
fileSystem: clientCompiler.outputFileSystem,
// Helps with finding the output folder in memory-fs
currentDirectory: __dirname,
},
}),
);
}
const distPath = path.join(__dirname, 'dist');
app.use(
// This path should be in sync with the `publicPath` from webpack config.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this comment is redundant

`${env.BASE_URL}/assets`,
ursucarina marked this conversation as resolved.
Show resolved Hide resolved
expressStaticGzip(distPath, {
maxAge: '1d',
}),
);
app.use(serverRenderer({ clientStats, currentDirectory: __dirname }));

/* Set ADMIN_API_USE_SSL to https for CORS support */
let server;
Expand Down
16 changes: 6 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
"scripts": {
"clean": "rm -rf dist",
"prebuild": "yarn run clean",
"build": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\"}' webpack --config webpack.config.ts --mode=development",
"build:prod": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\"}' NODE_ENV=production webpack --config webpack.config.ts --mode=production",
"build": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\"}' webpack --config webpack.dev.config.ts --mode=development",
"build:prod": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\"}' NODE_ENV=production webpack --config webpack.prod.config.ts --mode=production --progress",
"build:storybook": "build-storybook",
"start": "node -r dotenv/config index.js",
"start": "webpack serve --open --config webpack.dev.config.ts --hot",
"start:start:prodmode": "NODE_ENV=production node -r dotenv/config index.js",
ursucarina marked this conversation as resolved.
Show resolved Hide resolved
"storybook": "start-storybook -p 6006",
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
"format": "prettier --ignore-path .eslintignore --write \"**/*.+(js|jsx|ts|tsx|json)\"",
Expand Down Expand Up @@ -104,7 +105,6 @@
"@testing-library/react": "^10.0.3",
"@testing-library/react-hooks": "^7.0.2",
"@types/cheerio": "^0.22.2",
"@types/compression-webpack-plugin": "^9.1.1",
"@types/d3-shape": "^1.2.6",
"@types/debug": "^0.0.30",
"@types/dom-helpers": "^3.4.1",
Expand Down Expand Up @@ -136,7 +136,6 @@
"@typescript-eslint/eslint-plugin": "^5.15.0",
"@typescript-eslint/parser": "^5.15.0",
"@xstate/react": "^1.0.0",
"add-asset-html-webpack-plugin": "^3.2.0",
"autoprefixer": "^8.3.0",
"axios": "^0.21.2",
"axios-mock-adapter": "^1.16.0",
Expand Down Expand Up @@ -165,7 +164,6 @@
"fork-ts-checker-webpack-plugin": "^7.2.1",
"html-webpack-plugin": "^5.5.0",
"husky": "^4.2.5",
"identity-obj-proxy": "^3.0.0",
"intersection-observer": "^0.7.0",
"jest": "^26.0.0",
"linkify-it": "^2.2.0",
Expand Down Expand Up @@ -205,11 +203,9 @@
"use-react-router": "^1.0.7",
"webpack": "^5.68.0",
"webpack-cli": "^4.9.2",
"webpack-dev-middleware": "^5.3.1",
"webpack-hot-middleware": "^2.25.1",
"webpack-hot-server-middleware": "^0.6.1",
"webpack-dev-server": "^4.8.1",
"webpack-merge": "^5.8.0",
"webpack-node-externals": "^3.0.0",
"webpack-plugin-serve": "^1.6.0",
"webpack-stats-plugin": "^1.0.3",
"xstate": "^4.11.0"
},
Expand Down
12 changes: 10 additions & 2 deletions src/assets/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css?family=Lato:300,400,700|Open+Sans:400,700" rel="stylesheet">
<meta charset="UTF-8" />
<link
href="https://fonts.googleapis.com/css?family=Lato:300,400,700|Open+Sans:400,700"
rel="stylesheet"
/>
</head>
<body>
<div id="react-app"></div>
<% if (htmlWebpackPlugin.options.cdnReact) { %>
<script crossorigin src="<%= htmlWebpackPlugin.options.cdnReact %>"></script>
<% } %> <% if (htmlWebpackPlugin.options.cdnReactDOM) { %>
<script crossorigin src="<%= htmlWebpackPlugin.options.cdnReactDOM %>"></script>
<% } %>
</body>
</html>
File renamed without changes.
Loading