Skip to content

Commit

Permalink
Rework UMD build process to fix multiple issues
Browse files Browse the repository at this point in the history
- Added replace of "process" usage in dev build to fix crashes
- Added minified production UMD build
- Restructured conditional import of immutable middleware so that
  each build works correctly:
  - CJS/ESM: use conditional require in dev/prod, with shaking
  - UMD Dev: use import statement to pull in middleware
  - UMD Prod: drop middleware completely
  • Loading branch information
markerikson committed Jul 18, 2019
1 parent 96f733a commit d2fe46b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"repository": "https://github.com/markerikson/redux-starter-kit",
"main": "dist/redux-starter-kit.cjs.js",
"module": "dist/redux-starter-kit.esm.js",
"unpkg": "dist/redux-starter-kit.umd.js",
"unpkg": "dist/redux-starter-kit.umd.min.js",
"types": "dist/typings.d.ts",
"author": "Mark Erikson <mark@isquaredsoftware.com>",
"license": "MIT",
Expand Down
67 changes: 60 additions & 7 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,88 @@
import resolve from 'rollup-plugin-node-resolve'
import babel from 'rollup-plugin-babel'
import commonjs from 'rollup-plugin-commonjs'
import replace from 'rollup-plugin-replace'
import stripCode from "rollup-plugin-strip-code"
import { terser } from 'rollup-plugin-terser'

import pkg from './package.json'

const input = './src/index'
const input = './src/index.ts'
const exclude = 'node_modules/**'

const extensions = ['.ts', '.js']


export default [
// browser-friendly UMD build
// UMD Development
{
input,
output: {
name: 'redux-starter-kit',
file: pkg.unpkg,
format: 'umd'
file: 'dist/redux-starter-kit.umd.js',
format: 'umd',
name: 'RSK',
indent: false
},
plugins: [
resolve({
jsnext: true
}),
babel({
extensions,
exclude
exclude,
}),
replace({
'process.env.NODE_ENV': JSON.stringify('development'),
// Uncomment the import of redux-immutable-state-invariant so it gets pulled into this build
"// UMD-DEV-ONLY: ": "",
}),
stripCode({
// Remove the `require()` import of RISI so we use the import statement
start_comment: 'START_REMOVE_UMD',
end_comment: 'STOP_REMOVE_UMD'
}),
commonjs({
extensions,
namedExports: {
'node_modules/curriable/dist/curriable.js': ['curry', '__']
}
})
]
},

// UMD Production
{
input,
output: {
file: 'dist/redux-starter-kit.umd.min.js',
format: 'umd',
name: 'RSK',
indent: false
},
plugins: [
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
resolve({
extensions
jsnext: true
}),
babel({
extensions,
exclude,
}),
commonjs({
extensions,
namedExports: {
'node_modules/curriable/dist/curriable.js': ['curry', '__']
}
}),
terser({
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
warnings: false
},
})
]
},
Expand Down
12 changes: 7 additions & 5 deletions src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
} from 'redux'
import { composeWithDevTools, EnhancerOptions } from 'redux-devtools-extension'
import thunk, { ThunkDispatch, ThunkMiddleware } from 'redux-thunk'

// UMD-DEV-ONLY: import createImmutableStateInvariantMiddleware from 'redux-immutable-state-invariant'

import { createSerializableStateInvariantMiddleware } from './serializableStateInvariantMiddleware'

import isPlainObject from './isPlainObject'
Expand All @@ -34,14 +37,13 @@ export function getDefaultMiddleware<S = any, A extends Action = AnyAction>(): [
let middlewareArray: [ThunkMiddleware<S, A>, ...Middleware<{}, S>[]] = [thunk]

if (process.env.NODE_ENV !== 'production') {
/* START_REMOVE_UMD */
const createImmutableStateInvariantMiddleware = require('redux-immutable-state-invariant')
.default
middlewareArray.unshift(createImmutableStateInvariantMiddleware())
/* STOP_REMOVE_UMD */

middlewareArray = [
createImmutableStateInvariantMiddleware(),
thunk,
createSerializableStateInvariantMiddleware()
]
middlewareArray.push(createSerializableStateInvariantMiddleware())
}

return middlewareArray
Expand Down

0 comments on commit d2fe46b

Please sign in to comment.