Skip to content
This repository was archived by the owner on May 21, 2020. It is now read-only.

Commit 4cc812f

Browse files
committed
chore: upgrade webpack configuration and setup
1 parent 431e374 commit 4cc812f

File tree

29 files changed

+187
-169
lines changed

29 files changed

+187
-169
lines changed

.babelrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
"async-to-promises",
88
"transform-export-extensions",
99
["transform-object-rest-spread", { "useBuiltIns": true }],
10-
["transform-runtime", { "polyfill": false, "regenerator": false }]
10+
["transform-runtime", { "polyfill": false, "regenerator": false }],
11+
["module-resolver", {
12+
"root": ["./src/shared"]
13+
}]
1114
]
1215
}

.eslintrc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
},
1414
"globals": {
1515
"__DEV__": true,
16-
"__BROWSER__": true,
1716
"__INITIAL_STATE__": true,
1817
"google": true,
19-
"jest": true
2018
},
2119
"parserOptions": {
2220
"ecmaVersion": 6,
@@ -151,7 +149,7 @@
151149
"block-spacing": [2, "always"],
152150
"brace-style": [2, "1tbs", { "allowSingleLine": true }],
153151
"callback-return": 2,
154-
"camelcase": 2,
152+
"camelcase": [2, { "properties": "never" }],
155153
"comma-dangle": 0,
156154
"comma-spacing": [2, { "before": false, "after": true }],
157155
"comma-style": [2, "last"],

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ build/Release
2626
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
2727
node_modules
2828
build
29+
dist
2930
_tmp
3031
public
3132
.DS_Store

config/config.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

config/paths.js

Lines changed: 0 additions & 14 deletions
This file was deleted.

postcss.config.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

scripts/runServer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
const config = require('../config/config')
2+
const { signal } = require('../webpack/env')
3+
const { spawn } = require('child_process')
34
const path = require('path')
4-
const spawn = require('child_process').spawn
55
const webpackConfig = require('../webpack')
66

7-
const output = webpackConfig.find((x) => x.target === 'node').output
7+
const { output } = webpackConfig.find((x) => x.target === 'node')
88
const serverFile = path.join(output.path, output.filename)
9-
const IS_RUNNING = new RegExp(config.signal, 'g')
9+
const IS_RUNNING = new RegExp(signal, 'g')
1010

1111
function runServer() {
1212
const pool = []

scripts/start.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

2-
const merge = require('lodash.merge')
2+
const { appName, HOST, PORT } = require('../webpack/env')
33
const browsersync = require('browser-sync')
4-
const { appName, host, port } = require('../config/config')
54
const runServer = require('./runServer')
65
const webpack = require('webpack')
76
const webpackConfigs = require('../webpack')
@@ -17,7 +16,7 @@ function start() {
1716
stats: configs[0].stats,
1817
})
1918
const hotMiddlewares = createHotMiddlewares(multicompiler.compilers)
20-
const middlewares = [wpMiddleware, ...hotMiddlewares]
19+
const middleware = [wpMiddleware, ...hotMiddlewares]
2120
const restartServer = runServer()
2221

2322
return onPluginDone(multicompiler)
@@ -28,8 +27,8 @@ function start() {
2827
bs.init({
2928
notify: false,
3029
proxy: {
31-
target: `${host}:${port}`,
32-
middleware: middlewares,
30+
target: `${HOST}:${PORT}`,
31+
middleware,
3332
},
3433
}, resolve)
3534
}))
@@ -41,12 +40,8 @@ const onlyBrowserCompiler = (compiler) => onlyBrowser(compiler.options)
4140
const createHotMiddlewares = (compilers) => compilers.filter(onlyBrowserCompiler).map(webpackHotMiddleware)
4241
const addHotMiddleware = (config) => {
4342
if (onlyBrowser(config)) {
44-
return merge({}, config, {
45-
entry: {
46-
[appName]: ['react-hot-loader/patch', 'webpack-hot-middleware/client?reload=true', config.entry[appName]],
47-
},
48-
plugins: [new webpack.HotModuleReplacementPlugin()],
49-
})
43+
config.entry[appName] = ['react-hot-loader/patch', 'webpack-hot-middleware/client?reload=true', config.entry[appName]]
44+
config.plugins = config.plugins.concat([new webpack.HotModuleReplacementPlugin()])
5045
}
5146
return config
5247
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
export * from './route'
33
export * from './update'
4-
export * from './view'
4+
export * from './view.jsx'

src/Application/components/Authorized/components/Dashboard/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { view as component } from './view'
2+
import { view as component } from './view.jsx'
33
import { update as reducer } from './update'
44

55
export const route = {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
export * from './route'
33
// export * from './update'
4-
export * from './view'
4+
export * from './view.jsx'

src/Application/components/Authorized/components/Profile/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
// import { update as reducer } from './update'
3-
import { view as component } from './view'
3+
import { view as component } from './view.jsx'
44

55
export const route = {
66
path: 'profile',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
export * from './route'
33
export * from './update'
4-
export * from './view'
4+
export * from './view.jsx'

src/Application/components/Authorized/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { route as dashboardRoute } from './components/Dashboard'
33
import { route as profileRoute } from './components/Profile'
44
import { update as reducer } from './update'
5-
import { view as component } from './view'
5+
import { view as component } from './view.jsx'
66

77
export const route = {
88
path: 'authorized',
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
export * from './route'
33
export * from './update'
4-
export * from './view'
4+
export * from './view.jsx'

src/Application/components/Login/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { update as reducer } from './update'
3-
import { view as component } from './view'
3+
import { view as component } from './view.jsx'
44

55
export const route = {
66
path: 'login',

src/Application/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

22
export * from './route'
3-
export * from './view'
3+
export * from './view.jsx'

src/Application/route.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { route as authorizedRoute } from './components/Authorized'
3-
import { view as component } from './view'
3+
import { view as component } from './view.jsx'
44
import { route as loginRoute } from './components/Login'
55

66
export const route = {

src/client/index.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
import { Router, browserHistory } from 'react-router'
33
import { AppContainer } from 'react-hot-loader'
44
import { Provider } from 'react-redux'
5+
import { render } from 'react-dom'
56
import React from 'react'
67
import configureStore from 'configureStore'
7-
import reducers from '../shared/modules'
8-
import { render } from 'react-dom'
8+
99
import { route as routes } from '../Application'
10+
import reducers from 'modules'
1011

1112
if (__DEV__) {
1213
// Export React and Performance Utility for debugging

src/server/components/Html.jsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

2-
import { DEBUG, appName, inlineName, statsName, vendorName } from '../../../config/config'
2+
import { appName, inlineName, isDev, outputPath, statsName, vendorName } from '../../../webpack/env'
33
import React, { PropTypes as PT, PureComponent } from 'react'
44
import fs from 'fs'
5-
import { publicDir } from '../../../config/paths'
65
import { renderToStaticMarkup } from 'react-dom/server'
76

87
class Html extends PureComponent {
@@ -12,19 +11,19 @@ class Html extends PureComponent {
1211
}
1312

1413
static getScript(name) {
15-
if (DEBUG) return `/${name}.js`
14+
if (isDev) return `/${name}.js`
1615

17-
const file = fs.readFileSync(`${publicDir}/${statsName}.json`)
16+
const file = fs.readFileSync(`${outputPath}/${statsName}.json`)
1817
const stats = JSON.parse(file)
1918

20-
return name === 'app' && !DEBUG
19+
return name === 'app' && !isDev
2120
? `/${stats.assetsByChunkName[name][0]}`
2221
: `/${stats.assetsByChunkName[name]}`
2322
}
2423

2524
static getWebpackJsonpInlineScript() {
26-
if (DEBUG) return false
27-
return fs.readFileSync(`${publicDir}/${inlineName}.js`)
25+
if (isDev) return false
26+
return fs.readFileSync(`${outputPath}/${inlineName}.js`)
2827
}
2928

3029
static renderToStaticMarkup(props) {
@@ -83,7 +82,7 @@ class Html extends PureComponent {
8382
<meta content='/ms-icon-144x144.png' name='msapplication-TileImage'/>
8483
<meta content='#ffffff' name='theme-color'/>
8584

86-
{!DEBUG && <link href='/style.css' rel='stylesheet'/>}
85+
{!isDev && <link href='/style.css' rel='stylesheet'/>}
8786
</head>
8887
<body>
8988
<div id='app'>
@@ -105,7 +104,7 @@ Html.propTypes = {
105104
content: PT.string.isRequired,
106105
initalState: PT.object.isRequired,
107106
vendor: PT.string.isRequired,
108-
inline: DEBUG ? PT.bool : PT.string,
107+
inline: isDev ? PT.bool : PT.string,
109108
}
110109

111110
Html.defaultProps = {

src/server/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable no-console */
22

33
import * as m from './middlewares'
4-
import { DEBUG, host, port, signal } from '../../config/config'
4+
import { HOST, PORT, isDev, signal } from '../../webpack/env'
55
import compression from 'compression'
66
import express from 'express'
77

@@ -15,7 +15,7 @@ const app = express()
1515
app.disable('x-powered-by')
1616

1717
// Compression
18-
if (!DEBUG) app.use(compression())
18+
if (!isDev) app.use(compression())
1919

2020
// I. Static Assets
2121
m.staticMiddleware(app)
@@ -28,8 +28,8 @@ m.viewMiddleware(app)
2828
// Initialise Express
2929
// -----------------------------------------------------------------------------
3030

31-
app.listen(port, host, (err) => {
31+
app.listen(PORT, HOST, (err) => {
3232
if (err) return console.error('[server.js]: Error: ', err)
3333

34-
return console.log(`${signal} http://%s:%s`, host, port)
34+
return console.log(`${signal} http://%s:%s`, HOST, PORT)
3535
})

src/server/middlewares/staticMiddleware.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
// Static Middleware
44
//
55

6-
import { DEBUG } from '../../../config/config'
6+
import { isDev, outputPath } from '../../../webpack/env'
77
import express from 'express'
8-
import { publicDir } from '../../../config/paths'
98

10-
const MAX_AGE = DEBUG ? 0 : '1 year'
9+
const MAX_AGE = isDev ? 0 : '1 year'
1110
const staticCache = { maxAge: MAX_AGE, etag: true, lastModified: false }
1211

1312
export default (app) => {
1413
// root dir assets
15-
app.use(express.static(publicDir, staticCache))
14+
app.use(express.static(outputPath, staticCache))
1615
}

src/server/middlewares/viewMiddleware.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//
55

66
import { RouterContext, match } from 'react-router'
7-
import Html from '../components/Html'
7+
import Html from '../components/Html.jsx'
88
import { Provider } from 'react-redux'
99
import React from 'react'
1010
import configureStore from 'configureStore'

src/shared/configureStore/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export default (reducers = {}) => (initalState = {}) => {
99

1010
return compose(
1111
applyMiddleware(...middleware),
12-
__BROWSER__ ? window.devToolsExtension ? window.devToolsExtension() : (f) => f : (f) => f
12+
typeof window === 'object' ? window.devToolsExtension ? window.devToolsExtension() : (f) => f : (f) => f
1313
)(createStore)(rootReducer, initalState)
1414
}

webpack/env.es6

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
import path from 'path'
3+
4+
/**
5+
* ENV
6+
*/
7+
8+
// dev mode or prod
9+
export const isDev = process.env.NODE_ENV !== 'production'
10+
// port used by express
11+
export const PORT = process.env.PORT || 8080
12+
// webpack and server host
13+
export const HOST = process.env.HOST || 'localhost'
14+
15+
export const signal = 'server is running at'
16+
17+
/**
18+
* WEBPACK
19+
*/
20+
21+
// style.css
22+
export const cssName = 'style.css'
23+
// app.js
24+
export const appName = 'app'
25+
// vendor.js
26+
export const vendorName = 'vendor'
27+
// inline.js
28+
export const inlineName = 'inline'
29+
// stats.json
30+
export const statsName = 'stats'
31+
32+
/**
33+
* PATHS
34+
*/
35+
36+
export const contextPath = path.join(__dirname, '..')
37+
export const distPath = path.join(contextPath, 'build')
38+
export const outputPath = path.join(distPath, 'public')

0 commit comments

Comments
 (0)