Skip to content

Commit

Permalink
✨ devtools are unactive in production
Browse files Browse the repository at this point in the history
  • Loading branch information
fabienjuif committed Jan 24, 2019
1 parent f008f4b commit 314a8a9
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/k-ramel/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ All options are optionals to keep the default usage as simple as possible.
| key | type | default | description |
|---|---|---|---|
| listeners | `array` | `undefined` | array of all listeners. Listeners are a big part of this lib, you can [click here for detail](./doc/LISTENERS.md). |
| devtools | `boolean` | `true` | [redux-devtools](https://github.com/zalmoxisus/redux-devtools-extension) is activated. |
| devtools | `boolean` | `undefined` | [redux-devtools](https://github.com/zalmoxisus/redux-devtools-extension) is activated. If the value is `undefined` (default value) then devtools will be desactivated in production build (NODE_ENV === 'production'). |
| hideRedux | `boolean` | `true` | actions and selectors from [`k-redux-factory`](https://github.com/alakarteio/k-redux-factory) are used without specifying `dispatch` or `getState`. |
| enhancer | redux enhancer | `undefined` | usual `compose` and `applyMiddlare` you already use with Redux can be injected here (like router, redux-saga, etc). <br />`compose` and `applyMiddleware` from Redux are exposed by the lib. To use them:<br /> ```import { compose, applyMiddleware } from 'k-ramel'```. |
| init | `object` | `undefined` | the default value of your store. |
Expand Down
9 changes: 8 additions & 1 deletion packages/k-ramel/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,14 @@ Object {
}
`;

exports[`redux devtools should not add redux devtools to middlewares 1`] = `
exports[`redux devtools should not add redux devtools to middlewares [explicitely disabled] 1`] = `
Object {
"devToolsEnhancer": Array [],
"devToolsExtension": Array [],
}
`;

exports[`redux devtools should not add redux devtools to middlewares [production build] 1`] = `
Object {
"devToolsEnhancer": Array [],
"devToolsExtension": Array [],
Expand Down
2 changes: 1 addition & 1 deletion packages/k-ramel/dist/index.es.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/k-ramel/dist/index.umd.js

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion packages/k-ramel/misc/test-suite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-env jest */
/* eslint-disable no-underscore-dangle */
export default (lib) => {
const {
createStore,
Expand Down Expand Up @@ -492,8 +493,11 @@ export default (lib) => {

describe('redux devtools', () => {
const { __REDUX_DEVTOOLS_EXTENSION__ } = window
const { NODE_ENV } = process.env

afterEach(() => {
window.__REDUX_DEVTOOLS_EXTENSION__ = __REDUX_DEVTOOLS_EXTENSION__
process.env.NODE_ENV = NODE_ENV
})

it('should add redux devtools to middlewares [w/o name, w/o enhancer]', () => {
Expand Down Expand Up @@ -531,7 +535,7 @@ export default (lib) => {
}).toMatchSnapshot()
})

it('should not add redux devtools to middlewares', () => {
it('should not add redux devtools to middlewares [explicitely disabled]', () => {
const enhancer = jest.fn(f => f)
window.__REDUX_DEVTOOLS_EXTENSION__ = jest.fn(() => enhancer)

Expand All @@ -541,6 +545,18 @@ export default (lib) => {
devToolsEnhancer: enhancer.mock.calls,
}).toMatchSnapshot()
})

it('should not add redux devtools to middlewares [production build]', () => {
const enhancer = jest.fn(f => f)
window.__REDUX_DEVTOOLS_EXTENSION__ = jest.fn(() => enhancer)
process.env.NODE_ENV = 'production'

createStore({ dumb: { type: 'simple.object' } }, { devtools: undefined })
expect({
devToolsExtension: window.__REDUX_DEVTOOLS_EXTENSION__.mock.calls,
devToolsEnhancer: enhancer.mock.calls,
}).toMatchSnapshot()
})
})

describe('dispatch', () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/k-ramel/src/__snapshots__/index.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,14 @@ Object {
}
`;

exports[`redux devtools should not add redux devtools to middlewares 1`] = `
exports[`redux devtools should not add redux devtools to middlewares [explicitely disabled] 1`] = `
Object {
"devToolsEnhancer": Array [],
"devToolsExtension": Array [],
}
`;

exports[`redux devtools should not add redux devtools to middlewares [production build] 1`] = `
Object {
"devToolsEnhancer": Array [],
"devToolsExtension": Array [],
Expand Down
2 changes: 1 addition & 1 deletion packages/k-ramel/src/createStore/createStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const defaultOptions = {
enhancer: undefined,
init: {},
listeners: undefined,
devtools: true,
devtools: undefined, // meaning activated if not in production
trace: false,
traceLimit: 25,
name: 'store',
Expand Down
14 changes: 12 additions & 2 deletions packages/k-ramel/src/createStore/enhanceRedux/getDevTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ const getReduxDevToolsEnhancer = (options) => {
export default (options) => {
const { devtools } = options

// no devtool enable
if (!devtools) return undefined
// devtool is explicitely disabled
if (devtools === false) return undefined

// production build
if (
devtools === undefined // devtools is not explicitely enabled
&& typeof process !== 'undefined' // process exists
&& process.env // env exists
&& process.env.NODE_ENV === 'production' // production build -> unactivate
) {
return undefined
}

// return enhancer with devtools
return getReduxDevToolsEnhancer(options)
Expand Down

0 comments on commit 314a8a9

Please sign in to comment.