Skip to content

Commit 736f2ce

Browse files
committed
Presumably, working version of the spawned server
...still needs further restructuring of the code
1 parent d52933b commit 736f2ce

File tree

13 files changed

+86
-70
lines changed

13 files changed

+86
-70
lines changed

config/webpack/app-base.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ module.exports = function configFactory(ops) {
5151
* directory. */
5252
const buildInfo = {
5353
/* A random 32-bit key, that can be used for encryption. */
54-
rndkey: forge.random.getBytesSync(32),
54+
key: forge.random.getBytesSync(32),
55+
56+
/* This will be equal to "development" or "production" */
57+
mode: ops.babelEnv,
5558

5659
/* Build timestamp. */
5760
timestamp: moment.utc().toISOString(),
@@ -69,6 +72,7 @@ module.exports = function configFactory(ops) {
6972
entry.polyfills = _.union(entry.polyfills, [
7073
'babel-polyfill',
7174
'nodelist-foreach-polyfill',
75+
'topcoder-react-utils/src/client',
7276
]);
7377
return {
7478
context: o.context,
@@ -88,11 +92,7 @@ module.exports = function configFactory(ops) {
8892
filename: '[name].css',
8993
}),
9094
new webpack.DefinePlugin({
91-
global: {
92-
BUILD_RNDKEY: JSON.stringify(buildInfo.rndkey),
93-
BUILD_TIMESTAMP: JSON.stringify(buildInfo.timestamp),
94-
FRONT_END: true,
95-
},
95+
BUILD_INFO: JSON.stringify(buildInfo),
9696
}),
9797
],
9898
resolve: {
@@ -107,9 +107,6 @@ module.exports = function configFactory(ops) {
107107
symlinks: false,
108108
},
109109
module: {
110-
noParse: [
111-
/node_modules[\\/]config/,
112-
],
113110
rules: [{
114111
/* Loads font resources from "src/assets/fonts" folder. */
115112
test: /\.(eot|otf|svg|ttf|woff2?)$/,

config/webpack/lib-base.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ module.exports = function configFactory(ops) {
3939
'lodash',
4040
'moment',
4141
'prop-types',
42-
'raf',
4342
'react',
4443
'react-css-super-themr',
4544
'react-dom',
@@ -59,6 +58,7 @@ module.exports = function configFactory(ops) {
5958
}),
6059
],
6160
module: {
61+
noParse: [],
6262
rules: [{
6363
/* Handles font imports in url(..) instructions in CSS. Effectively,
6464
* with such configuration it just rewrites those URLs to point to

package-lock.json

Lines changed: 2 additions & 41 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@
9595
"url": "git+https://github.com/topcoder-platform/topcoder-react-utils.git"
9696
},
9797
"scripts": {
98-
"build": "npm run clean && ./node_modules/.bin/webpack --env=production --colors --display-optimization-bailout --profile --progress && BABEL_END=production babel src --out-dir dist/src",
99-
"build:dev": "npm run clean && (./node_modules/.bin/webpack --env=development --bail --colors --display-optimization-bailout --profile --progress --watch | BABEL_ENV=development babel src --out-dir dist/src --watch)",
98+
"build": "npm run clean && ./node_modules/.bin/webpack --env=production --colors --display-optimization-bailout --profile --progress && BABEL_ENV=production babel src/server --out-dir dist/server",
99+
"build:dev": "npm run clean && (./node_modules/.bin/webpack --env=development --bail --colors --display-optimization-bailout --profile --progress --watch | BABEL_ENV=development babel src/server --out-dir dist/server --watch)",
100100
"clean": "rm -rf dist",
101101
"jest": "jest --no-cache --maxWorkers=4 --config config/jest/default.js",
102102
"lint": "npm run lint:js && npm run lint:scss",

src/client/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* Initialization of client-side code.
3+
*/
4+
5+
/* global BUILD_INFO window */
6+
window.TRU_CONSTANTS = BUILD_INFO;
7+
window.TRU_FRONT_END = true;

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,16 @@ import utils from 'utils';
77

88
import 'styles/global.scss';
99

10+
import { requireWeak } from 'utils/webpack';
11+
12+
const server = utils.isomorphy.isServerSide() ? requireWeak('topcoder-react-utils/dist/server') : null;
13+
1014
export {
1115
Avatar,
1216
Button,
1317
Link,
1418
NavLink,
1519
ScalableRect,
20+
server,
1621
utils,
1722
};

src/server/index.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,11 @@
55
import _ from 'lodash';
66
import http from 'http';
77

8-
import { isServerSide } from 'utils/isomorphy';
9-
108
/* Polyfill required by ReactJS. */
119
import 'raf/polyfill';
1210

1311
import serverFactory from './server';
1412

15-
/* TODO: Should use the isClientSide(..) method from isomorphy module, once that
16-
* module is moved to topcoder-react-utils. */
17-
if (!isServerSide()) {
18-
throw new Error('Cannot execute server-side code in other environment');
19-
}
20-
2113
/**
2214
* Normalizes a port into a number, string, or false.
2315
* @param {String} value Port name or number.

src/server/renderer.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const sanitizedConfig = _.omit(config, 'SECRET');
2525
* @param {String} context Webpack context path used during the build.
2626
* @return {Promise} Resolves to the build-time information.
2727
*/
28+
/* TODO: Use sync read, to simplify related code. */
2829
function getBuildInfo(context) {
2930
const url = path.resolve(context, '.build-info');
3031
return new Promise((resolve, reject) => {
@@ -48,6 +49,7 @@ function prepareCipher(key) {
4849
forge.random.getBytes(32, (err, iv) => {
4950
if (err) reject(err);
5051
else {
52+
console.log('KEY', key);
5153
const cipher = forge.cipher.createCipher('AES-CBC', key);
5254
cipher.start({ iv });
5355
resolve({ cipher, iv });
@@ -65,13 +67,14 @@ function prepareCipher(key) {
6567
export default async function factory(webpackConfig, options) {
6668
const buildInfo = await getBuildInfo(webpackConfig.context);
6769

70+
global.TRU_CONSTANTS = buildInfo;
71+
console.log('BUILD INFO', buildInfo);
72+
6873
const ops = _.defaults(_.clone(options), {
6974
beforeRender: () => Promise.resolve({}),
7075
});
7176

7277
return async (req, res) => {
73-
global.BUILD_INFO = buildInfo;
74-
7578
const [{
7679
configToInject,
7780
extraScripts,

src/shared/utils/config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
/* eslint-env browser */
99

1010
import { isClientSide } from './isomorphy';
11+
import { requireWeak } from './webpack';
1112

1213
/* eslint-disable global-require */
13-
module.exports = isClientSide() ? window.CONFIG : require('config');
14+
console.log('IS CLIENT SIDE', isClientSide());
15+
module.exports = isClientSide() ? window.CONFIG : requireWeak('config');
1416
/* eslint-enable global-require */

src/shared/utils/isomorphy.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,22 @@
77
* @return {Boolean}
88
*/
99
export function isClientSide() {
10-
return Boolean(global.FRONT_END);
10+
return Boolean(global.TRU_FRONT_END);
1111
}
1212

1313
/**
1414
* Returns `true` if executed at the server side; `false` otherwise.
1515
* @return {Boolean}
1616
*/
1717
export function isServerSide() {
18-
return !global.FRONT_END;
18+
return !global.TRU_FRONT_END;
19+
}
20+
21+
/**
22+
* @return {String} Code mode: "development" or "production".
23+
*/
24+
function getMode() {
25+
return isClientSide() ? global.TRU_CONSTANTS.mode : process.env.BABEL_ENV;
1926
}
2027

2128
/**
@@ -24,7 +31,7 @@ export function isServerSide() {
2431
* @return {Boolean}
2532
*/
2633
export function isDevBuild() {
27-
return process.env.BABEL_ENV === 'development';
34+
return getMode() === 'development';
2835
}
2936

3037
/**
@@ -33,14 +40,13 @@ export function isDevBuild() {
3340
* @return {Boolean}
3441
*/
3542
export function isProdBuild() {
36-
return process.env.BABEL_ENV === 'production';
43+
return getMode() === 'production';
3744
}
3845

3946
/**
4047
* Returns build timestamp of the front-end JS bundle.
4148
* @return {String} ISO date/time string.
4249
*/
4350
export function buildTimestamp() {
44-
if (isClientSide()) return global.BUILD_TIMESTAMP;
45-
return global.BUILD_INFO.timestamp;
51+
return global.TRU_CONSTANTS.timestamp;
4652
}

src/shared/utils/webpack/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Requires the specified module without including it into the bundle during
3+
* Webpack build. This function should be executed only server-side.
4+
* @param {String} modulePath
5+
* @return Required module.
6+
*/
7+
export function requireWeak(modulePath) {
8+
/* eslint-disable global-require, import/no-dynamic-require */
9+
const mod = require('./require')(modulePath);
10+
/* eslint-enable global-require, import/no-dynamic-require */
11+
return mod.default || mod;
12+
}
13+
14+
export default undefined;

src/shared/utils/webpack/require.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* This helper module is a part of requireWeak(..) implementation. Webpack is
3+
* configured to ignore this file, thus the code:
4+
*
5+
* const m = require('utils/router/require')('my-module-path');
6+
*
7+
* will load the module at the specified path, when exectuted at the server
8+
* side, and will crush, if executed at the client side. Though, the crush at
9+
* the client side can be easily prevented by adding condition:
10+
*
11+
* import { isServerSide } from 'utils/isomorphy';
12+
*
13+
* if (isServerSide()) {
14+
* const m = require('utils/router/require')('my-module-path');
15+
* // Some client-side only code.
16+
* }
17+
*
18+
* The key point here is that the module at 'my-module-path' will not be bundled
19+
* by Webpack if required this way. Thus, this way we can use static resolution
20+
* of the module at the server-side, while using alternative code at the client
21+
* side (dynamic module loading from the server).
22+
*/
23+
24+
/* eslint-disable global-require */
25+
/* eslint-disable import/no-dynamic-require */
26+
27+
module.exports = require;

webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ module.exports = function buildConfig(env) {
1414
config.module.rules.find(x => x.loader === 'babel-loader');
1515
babelLoader.options.presets[0] = `${__dirname}/config/babel/webpack`;
1616

17+
config.module.noParse.push(/src\/shared\/utils\/webpack\/require/);
18+
1719
return config;
1820
};

0 commit comments

Comments
 (0)