Skip to content

Commit

Permalink
Improved distinguish between prod and dev version running via Hot Mod…
Browse files Browse the repository at this point in the history
…ule Replacement.
  • Loading branch information
BiosBoy committed Jun 11, 2019
1 parent 27ef471 commit 850a8e6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Webpack4 - React16.7 Templater App


# 2.1.1
* Update UjlifyJS plugin correspond to its internal update.
* Improved distinguish between prod and dev version running via Hot Module Replacement.

# 2.0.1
* Updated Webpack to 4.29.1 version.
* Removed .travis.ci file from git watch.
Expand Down
16 changes: 9 additions & 7 deletions server/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// import global vars for a whole app
require('../globals');
const globals = require('../globals');

const path = require('path');
const browserSync = require('browser-sync');
Expand All @@ -15,13 +15,19 @@ const bundler = webpack(webpackConfig);
// ========================================================
const devMiddlewareOptions = {
publicPath: webpackConfig.output.publicPath,
hot: true,
hot: globals.__DEV__ ? true : false,
headers: { 'Access-Control-Allow-Origin': '*' }
};

// ========================================================
// Server Configuration
// ========================================================
const webpackMiddleware = [webpackDevMiddleware(bundler, devMiddlewareOptions)];

if (globals.__DEV__) {
webpackMiddleware.push(webpackHotMiddleware(bundler));
}

browserSync({
open: false,
ghostMode: {
Expand All @@ -31,11 +37,7 @@ browserSync({
},
server: {
baseDir: path.resolve(__dirname, '../src'),
middleware: [
historyApiFallback(),
webpackDevMiddleware(bundler, devMiddlewareOptions),
webpackHotMiddleware(bundler)
]
middleware: [historyApiFallback(), ...webpackMiddleware]
},
files: [
'src/../*.tsx',
Expand Down
16 changes: 11 additions & 5 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ const optimization = {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
warnings: false,
compress: {
unused: true,
dead_code: true,
warnings: false
dead_code: true
}
},
sourceMap: true
Expand Down Expand Up @@ -213,10 +213,16 @@ const createConfig = () => {
// ------------------------------------
// Entry Points
// ------------------------------------
const appEntryPoint = ['babel-polyfill', path.resolve(__dirname, 'src/index.js')];

if (__DEV__) {
const HMRPath = 'webpack-hot-middleware/client?path=/__webpack_hmr';

appEntryPoint[1].concat(HMRPath);
}

webpackConfig.entry = {
app: ['babel-polyfill', path.resolve(__dirname, 'src/index.js')].concat(
'webpack-hot-middleware/client?path=/__webpack_hmr'
)
app: appEntryPoint
};

// ------------------------------------
Expand Down

0 comments on commit 850a8e6

Please sign in to comment.