From c847b19825989f93b5cfefc064dbc4569c365c2d Mon Sep 17 00:00:00 2001 From: Marco Scabbiolo Date: Thu, 23 Mar 2017 22:40:26 -0300 Subject: [PATCH] Optionally include redux-thunk, resolve #73 --- generators/app/index.js | 25 +++++++++++++++++++++--- generators/root/index.js | 4 ++-- generators/root/templates/store-thunk.js | 25 ++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 generators/root/templates/store-thunk.js diff --git a/generators/app/index.js b/generators/app/index.js index 5e1cdc5..61c6bbc 100644 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -8,6 +8,18 @@ class AppGenerator extends Generator { this.option('skip-install'); } + prompting() { + return this.prompt([{ + type: 'confirm', + name: 'thunk', + message: 'Would you like to include redux-thunk?', + default: false, + store: true, + }]).then(answers => { + this.thunk = answers.thunk; + }); + } + install() { if(!this.options['skip-install']) { this.installDependencies({ bower: false }); @@ -29,11 +41,18 @@ class AppGenerator extends Generator { // Run the create root method this.composeWith('react-webpack-redux:root', { - args: ['Root'] + args: ['Root'], + thunk: this.thunk, }); - // Install redux and react bindings as requirement - this.npmInstall(['redux', 'react-redux'], { 'save': true }); + // Install redux and react bindings as requirement and redux-thunk optionally + let packages = ['redux', 'react-redux']; + + if (this.thunk) { + packages.push('redux-thunk'); + } + + this.npmInstall(packages, { 'save': true }); }); } }; diff --git a/generators/root/index.js b/generators/root/index.js index 0c7b615..13014e5 100644 --- a/generators/root/index.js +++ b/generators/root/index.js @@ -1,6 +1,5 @@ 'use strict'; const Generator = require('yeoman-generator'); -const fs = require('fs'); class RootGenerator extends Generator { writing() { @@ -12,7 +11,7 @@ class RootGenerator extends Generator { // Copy the store this.fs.copy( - this.templatePath('store.js'), + this.options.thunk ? this.templatePath('store-thunk.js') : this.templatePath('store.js'), this.destinationPath('src/stores/index.js') ); @@ -52,6 +51,7 @@ class RootGenerator extends Generator { this.destinationPath('.eslintrc') ); } + }; module.exports = RootGenerator; diff --git a/generators/root/templates/store-thunk.js b/generators/root/templates/store-thunk.js new file mode 100644 index 0000000..b1583a3 --- /dev/null +++ b/generators/root/templates/store-thunk.js @@ -0,0 +1,25 @@ + +import { createStore, compose, applyMiddleware } from 'redux'; +import thunk from 'redux-thunk'; +import reducers from '../reducers'; + +function reduxStore(initialState) { + const store = createStore(reducers, initialState, compose( + applyMiddleware(thunk), + window.devToolsExtension ? window.devToolsExtension() : f => f + )); + + if (module.hot) { + // Enable Webpack hot module replacement for reducers + module.hot.accept('../reducers', function () { + // We need to require for hot reloading to work properly. + const nextReducer = require('../reducers'); // eslint-disable-line global-require + + store.replaceReducer(nextReducer); + }); + } + + return store; +} + +export default reduxStore;