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

archetype-react-app: Fix webpack-dev-server HMR #128

Merged
merged 4 commits into from
Jan 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,34 @@ var mergeWebpackConfig = require("webpack-partial").default;
var archetype = require("../../archetype");
var AppMode = archetype.AppMode;
var Path = archetype.Path;
var _ = require("lodash");

module.exports = function (babel) {
// regex \b for word boundaries
var babelExcludeRegex = new RegExp(`(node_modules|\b${Path.join(AppMode.src.client, "vendor")}\b)`);
return function (config) {
var hmr = process.env.HMR !== undefined;

return mergeWebpackConfig(config, {
module: {
loaders: [{
name: "babel",
test: /\.jsx?$/,
exclude: babelExcludeRegex,
// NOTE: webpack.config.hot.js inserts "react-hot" into loaders array
loader: "babel-loader",
query: babel
},
loaders: [
{
name: "babel",
test: /\.jsx?$/,
include: hmr && Path.resolve(AppMode.src.client),
exclude: babelExcludeRegex,
loaders: [
hmr && "react-hot-loader",
"babel-loader"
].filter(_.identity),
query: babel
},
{
name: "json",
test: /\.json$/,
loader: "json"
}]
}
]
}
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var archetype = require("../../archetype");
var mergeWebpackConfig = require("webpack-partial").default;
var webpack = require("webpack");

var getDefaultEntry = function (entry) {
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
/**
* Webpack hot configuration
*/

process.env.HMR = "true";

var _ = require("lodash");
var path = require("path");
var mergeWebpackConfig = require("webpack-partial").default;
var hotConfig = require("./partial/hot");
var baseConfig = require("./base.js");
var defineConfig = require("./partial/define.js");
var devConfig = require("./partial/dev.js");
var WebpackConfig = require("webpack-config").default;
var getRootConfig = require("./get-root-config");
var fs = require("fs");
var archetype = require("../archetype");

var config = module.exports = _.flow(
mergeWebpackConfig.bind(null, {}, baseConfig),
Expand All @@ -21,13 +21,6 @@ var config = module.exports = _.flow(
hotConfig()
)();

/****
* Hot Mods
*/
var babel = _.find(config.module.loaders, {name: "babel"});

// update babel loaders for hot loading
babel.loaders = [].concat(["react-hot"], babel.loaders);
babel.include = path.resolve(archetype.AppMode.src.client);

module.exports = new WebpackConfig().merge(config).merge(getRootConfig("webpack.config.hot.js"));
module.exports = new WebpackConfig()
.merge(config)
.merge(getRootConfig("webpack.config.hot.js"));
9 changes: 8 additions & 1 deletion packages/electrode-archetype-react-app/arch-gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ function setProductionEnv() {
process.env.NODE_ENV = "production";
}

function setDevelopmentEnv() {
process.env.NODE_ENV = "development";
}

function setStaticFilesEnv() {
process.env.STATIC_FILES = "true";
}
Expand All @@ -52,7 +56,7 @@ function createGitIgnoreDir(dir, comment) {
const dirFP = Path.resolve(dir);
try {
mkdirp.sync(dirFP);
} catch(e) {
} catch (e) {
console.log("mkdir", e);
}

Expand Down Expand Up @@ -254,6 +258,7 @@ function makeTasks(gulp) {
let tasks = {
".mk-prod-dir": () => createGitIgnoreDir(Path.resolve(archetype.prodDir), "Electrode production dir"),
".production-env": () => setProductionEnv(),
".development-env": () => setDevelopmentEnv(),
".webpack-dev": () => setWebpackDev(),
".static-files-env": () => setStaticFilesEnv(),
".optimize-stats": () => setOptimizeStats(),
Expand Down Expand Up @@ -406,6 +411,7 @@ INFO: Individual .babelrc files were generated for you in src/client and src/ser

"hot": {
desc: "Start server with watch in hot mode with webpack-dev-server",
dep: [".development-env"],
task: [".webpack-dev", ["server-hot", "server-watch", "generate-service-worker"]]
},

Expand Down Expand Up @@ -609,5 +615,6 @@ module.exports = function (gulp) {
setupPath();
createElectrodeTmpDir();
gulp = gulp || require("gulp");
process.env.FORCE_COLOR = "true"; // force color for chalk
gulpHelper.loadTasks(makeTasks(gulp), gulp);
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import {routes} from "./routes";
import {Router, browserHistory} from "react-router";
import {createStore} from "redux";
import {Provider} from "react-redux";
<% if (pwa) { %>import {notify} from "react-notify-toast";<% } %>
/* <% if (pwa) { %> */
import {notify} from "react-notify-toast";
/* <% } %> */
import "./styles/base.css";
import rootReducer from "./reducers";

// Add the client app start up code to a function as window.webappStart.
// The webapp's full HTML will check and call it once the js-content
// DOM is created.
<% if (pwa) { %>
/*<% if (pwa) { %>*/
require.ensure(["./sw-registration"], (require) => {
require("./sw-registration")(notify);
}, "sw-registration");
<% } %>
/*<% } %>*/
window.webappStart = () => {
const initialState = window.__PRELOADED_STATE__;
const store = createStore(rootReducer, initialState);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import React, {PropTypes} from "react";
import {connect} from "react-redux";<%if(pwa){%>
import Notifications from "react-notify-toast";<%}%>
import {connect} from "react-redux";
/*<% if (pwa) { %>*/
import Notifications from "react-notify-toast";
/*<% } %>*/
import {toggleCheck, incNumber, decNumber} from "../actions";

class Home extends React.Component {
render() {
const props = this.props;
const {checked, value} = props;
return (
<div><%if(pwa){%>
<Notifications /><%}%>
<div>
{/*<% if (pwa) { %>*/}
<Notifications />
{/*<% } %>*/}
<h1>Hello <a href={"https://github.com/electrode-io"}>{"Electrode"}</a></h1>
<div>
<h2>Managing States with Redux</h2>
Expand Down