Skip to content

Commit b08ba9a

Browse files
committed
Updates the way to pack library for releases
1 parent 5237634 commit b08ba9a

16 files changed

+81
-26
lines changed

CHANGELOG.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
# Topcoder React Utils Changelog
22

3+
### v0.6.0
4+
A better way to build the library: both dev and prod builds are created; and
5+
the proper version is selected depending on `NODE_ENV` value at the buildtime
6+
(client-side) or runtime (server-side) of the host code.
7+
8+
**BREAKING CHANGE:** You should update the way you include style the global
9+
stylesheet into the host code, from:
10+
```jsx
11+
require('topcoder-react-utils/dist/style.css');
12+
```
13+
to
14+
```jsx
15+
/* eslint-disable global-require */
16+
if (process.env.NODE_ENV === 'production') {
17+
require('topcoder-react-utils/dist/prod/style.css');
18+
} else {
19+
require('topcoder-react-utils/dist/dev/style.css');
20+
}
21+
/* eslint-enable global-require */
22+
```
23+
This will include the proper version of compiled global styles into the host
24+
code.
25+
326
### v0.5.0
427
All dependencies are force-updated to their latest versions. It might introduce
528
breaking changes.

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,14 @@ $ npm install --save topcoder-react-utils
2626
$ ./node_modules/.bin/topcoder-lib-setup
2727
```
2828
Then import the global stylesheet into the root ReactJS component of your app:
29-
```js
30-
import 'topcoder-react-utils/dist/style.css';
29+
```jsx
30+
/* eslint-disable global-require */
31+
if (process.env.NODE_ENV === 'production') {
32+
require('topcoder-react-utils/dist/prod/style.css');
33+
} else {
34+
require('topcoder-react-utils/dist/dev/style.css');
35+
}
36+
/* eslint-enable global-require */
3137
```
3238

3339
To upgrade this library to the latest version just execute again

__tests__/shared/utils/isomorphy.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ afterEach(() => {
99
delete window.TRU_BUILD_INFO;
1010
delete window.TRU_FRONT_END;
1111
delete process.env.BABEL_ENV;
12+
process.env.NODE_ENV = 'test';
1213
});
1314

1415
beforeEach(() => jest.resetModules());
@@ -25,27 +26,27 @@ test('Server-side detection', () => {
2526
});
2627

2728
test('Dev mode detection - client side', () => {
28-
window.TRU_BUILD_INFO = { mode: 'development' };
29+
process.env.NODE_ENV = 'development';
2930
window.TRU_FRONT_END = true;
3031
expect(m().isDevBuild()).toBe(true);
3132
expect(m().isProdBuild()).toBe(false);
3233
});
3334

3435
test('Dev mode detection - server side', () => {
35-
global.TRU_BUILD_INFO = { mode: 'development' };
36+
process.env.NODE_ENV = 'development';
3637
expect(m().isDevBuild()).toBe(true);
3738
expect(m().isProdBuild()).toBe(false);
3839
});
3940

4041
test('Prod mode - client side', () => {
41-
window.TRU_BUILD_INFO = { mode: 'production' };
42+
process.env.NODE_ENV = 'production';
4243
window.TRU_FRONT_END = true;
4344
expect(m().isDevBuild()).toBe(false);
4445
expect(m().isProdBuild()).toBe(true);
4546
});
4647

4748
test('Prod mode - server side', () => {
48-
global.TRU_BUILD_INFO = { mode: 'production' };
49+
process.env.NODE_ENV = 'production';
4950
expect(m().isDevBuild()).toBe(false);
5051
expect(m().isProdBuild()).toBe(true);
5152
});

config/webpack/app-base.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ module.exports = function configFactory(ops) {
6767
/* A random 32-bit key, that can be used for encryption. */
6868
key: forge.random.getBytesSync(32),
6969

70-
/* This will be equal to "development" or "production" */
71-
mode: ops.babelEnv,
72-
7370
/* Build timestamp. */
7471
timestamp: now.utc().toISOString(),
7572
};
@@ -83,10 +80,10 @@ module.exports = function configFactory(ops) {
8380
else if (!_.isArray(entry.polyfills)) {
8481
entry.polyfills = [entry.polyfills];
8582
}
83+
8684
entry.polyfills = _.union(entry.polyfills, [
8785
'babel-polyfill',
8886
'nodelist-foreach-polyfill',
89-
'topcoder-react-utils/dist/client/init',
9087
]);
9188

9289
return {

config/webpack/app-development.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,6 @@ module.exports = function configFactory(ops) {
5353
'react-hot-loader/patch',
5454
'webpack-hot-middleware/client?reload=true',
5555
].concat(res.entry.main);
56+
res.entry.polyfills.push('topcoder-react-utils/dist/dev/client/init');
5657
return res;
5758
};

config/webpack/app-production.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const webpackMerge = require('webpack-merge');
2828
* @param {String} ops.publicPath Base URL for the output of the build assets.
2929
*/
3030
module.exports = function configFactory(ops) {
31-
return webpackMerge.smart(baseFactory({
31+
const res = webpackMerge.smart(baseFactory({
3232
...ops,
3333
babelEnv: 'production',
3434
mode: 'production',
@@ -52,4 +52,6 @@ module.exports = function configFactory(ops) {
5252
}),
5353
],
5454
});
55+
res.entry.polyfills.push('topcoder-react-utils/dist/prod/client/init');
56+
return res;
5557
};

config/webpack/lib-base.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const webpack = require('webpack');
2828
*
2929
* @param {String} ops.library Name of the library.
3030
*
31+
* @param {String} ops.outputPath Output path.
32+
*
3133
* @return {Object} Webpack config.
3234
*/
3335
module.exports = function configFactory(ops) {
@@ -65,7 +67,7 @@ module.exports = function configFactory(ops) {
6567
globalObject: "typeof self !== 'undefined' ? self : this",
6668

6769
library: ops.library,
68-
path: path.resolve(__dirname, ops.context, 'dist'),
70+
path: ops.outputPath,
6971
libraryTarget: 'umd',
7072
},
7173
plugins: [

config/webpack/lib-development.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
const baseFactory = require('./lib-base');
6+
const path = require('path');
67

78
/**
89
* Creates a new Webpack config.
@@ -25,5 +26,6 @@ module.exports = function configFactory(ops) {
2526
babelEnv: 'development',
2627
cssLocalIdent: '[path][name]___[local]___[hash:base64:6]',
2728
mode: 'development',
29+
outputPath: path.resolve(__dirname, ops.context, 'dist/dev'),
2830
});
2931
};

config/webpack/lib-production.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44

55
const baseFactory = require('./lib-base');
6+
const path = require('path');
67

78
/**
89
* Creates a new Webpack config.
@@ -25,5 +26,6 @@ module.exports = function configFactory(ops) {
2526
babelEnv: 'production',
2627
cssLocalIdent: '[hash:base64:6]',
2728
mode: 'production',
29+
outputPath: path.resolve(__dirname, ops.context, 'dist/prod'),
2830
});
2931
};

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Entry point of the library.
3+
*
4+
* Depending on NODE_ENV variable, it proxies production or development build of
5+
* the library.
6+
*/
7+
/* eslint-disable global-require, import/no-unresolved */
8+
9+
if (process.env.NODE_ENV === 'production') {
10+
module.exports = require('./dist/prod');
11+
} else {
12+
module.exports = require('./dist/dev');
13+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,25 @@
101101
"React"
102102
],
103103
"license": "MIT",
104-
"main": "dist/index.js",
104+
"main": "index.js",
105105
"name": "topcoder-react-utils",
106106
"repository": {
107107
"type": "git",
108108
"url": "git+https://github.com/topcoder-platform/topcoder-react-utils.git"
109109
},
110110
"scripts": {
111-
"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 && BABEL_ENV=production babel src/shared/utils/jest.jsx --out-file dist/shared/utils/jest.js && BABEL_ENV=production babel src/client/init.js --out-file dist/client/init.js",
112-
"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 | BABEL_ENV=development babel src/shared/utils/jest.jsx --out-file dist/shared/utils/jest.js --watch | BABEL_ENV=development babel src/client/init.js --out-file dist/client/init.js --watch)",
113-
"clean": "./node_modules/.bin/rimraf dist && mkdir -p dist/shared/utils && mkdir -p dist/client",
111+
"build": "npm run clean && npm run build:dev && npm run build:prod",
112+
"build:dev": "npm run mkDistDir:dev && ./node_modules/.bin/webpack --env=development --bail --colors --display-optimization-bailout --profile --progress && BABEL_ENV=development babel src/server --out-dir dist/dev/server && BABEL_ENV=development babel src/shared/utils/jest.jsx --out-file dist/dev/shared/utils/jest.js && BABEL_ENV=development babel src/client/init.js --out-file dist/dev/client/init.js",
113+
"build:dev:watch": "npm run clean && npm run mkDistDir:dev && (./node_modules/.bin/webpack --env=development --bail --colors --display-optimization-bailout --profile --progress --watch | BABEL_ENV=development babel src/server --out-dir dist/dev/server --watch | BABEL_ENV=development babel src/shared/utils/jest.jsx --out-file dist/dev/shared/utils/jest.js --watch | BABEL_ENV=development babel src/client/init.js --out-file dist/dev/client/init.js --watch)",
114+
"build:prod": "npm run mkDistDir:prod && ./node_modules/.bin/webpack --env=production --colors --display-optimization-bailout --profile --progress && BABEL_ENV=production babel src/server --out-dir dist/prod/server && BABEL_ENV=production babel src/shared/utils/jest.jsx --out-file dist/prod/shared/utils/jest.js && BABEL_ENV=production babel src/client/init.js --out-file dist/prod/client/init.js",
115+
"clean": "./node_modules/.bin/rimraf dist",
114116
"jest": "NODE_CONFIG_ENV=test jest --no-cache --maxWorkers=4 --config config/jest/default.js",
115117
"lint": "npm run lint:js && npm run lint:scss",
116118
"lint:js": "eslint --ext .js,.jsx .",
117119
"lint:scss": "./node_modules/.bin/stylelint **/*.scss --syntax scss",
120+
"mkDistDir:dev": "mkdir -p dist/dev/shared/utils && mkdir -p dist/dev/client",
121+
"mkDistDir:prod": "mkdir -p dist/prod/shared/utils && mkdir -p dist/prod/client",
118122
"test": "npm run lint && npm run jest"
119123
},
120-
"version": "0.5.3"
124+
"version": "0.6.0"
121125
}

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import * as utils from 'utils';
1313

1414
import 'styles/global.scss';
1515

16+
import { isProdBuild } from 'utils/isomorphy';
1617
import { requireWeak } from 'utils/webpack';
1718

18-
const server = utils.isomorphy.isServerSide() ?
19-
requireWeak('topcoder-react-utils/dist/server') : null;
19+
let serverUrl = isProdBuild() ? 'prod' : 'dev';
20+
serverUrl = `topcoder-react-utils/dist/${serverUrl}/server`;
21+
const server = utils.isomorphy.isServerSide() ? requireWeak(serverUrl) : null;
2022

2123
module.exports = {
2224
actions,

src/shared/utils/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import * as isomorphy from './isomorphy';
33
import * as redux from './redux';
44
import * as webpack from './webpack';
55

6-
const JU = isomorphy.isServerSide() ?
7-
webpack.requireWeak('topcoder-react-utils/dist/shared/utils/jest') : null;
6+
let juUrl = isomorphy.isProdBuild() ? 'prod' : 'dev';
7+
juUrl = `topcoder-react-utils/dist/${juUrl}/shared/utils/jest`;
8+
9+
const JU = isomorphy.isServerSide() ? webpack.requireWeak(juUrl) : null;
810

911
export {
1012
config,

src/shared/utils/isomorphy.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
* Collection of helpers to deal with isomorphic aspects of the code.
33
*/
44

5-
import _ from 'lodash';
6-
75
/* global window */
86

97
/**
@@ -26,7 +24,7 @@ export function isServerSide() {
2624
* @return {String} Code mode: "development" or "production".
2725
*/
2826
function getMode() {
29-
return _.get(global.TRU_BUILD_INFO, 'mode');
27+
return process.env.NODE_ENV;
3028
}
3129

3230
/**

src/shared/utils/webpack/__mocks__/require.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ const ROOT = path.resolve(__dirname, '../../../..');
55

66
/* eslint-disable global-require, import/no-dynamic-require */
77
module.exports = modulePath =>
8-
require(modulePath.replace(/^topcoder-react-utils\/dist/, ROOT));
8+
require(modulePath.replace(/^topcoder-react-utils\/dist\/dev/, ROOT));

0 commit comments

Comments
 (0)