Skip to content

Commit 1ad6087

Browse files
committed
Migrate Redux package to be full ESM
- Added `type: "module"` and `exports` section - Moved build output to `/dist` - Updated Rollup to 3.x - Changed CommonJS artifact to have `.cjs` extension to placate `publint` - Renamed config files and `mangleErrors` script to `.cjs`
1 parent d0a74ed commit 1ad6087

File tree

8 files changed

+22
-30
lines changed

8 files changed

+22
-30
lines changed
File renamed without changes.
File renamed without changes.

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Set up Node
1919
uses: actions/setup-node@v3
2020
with:
21-
node-version: 14.x
21+
node-version: 16.x
2222
cache: 'npm'
2323

2424
- name: Install dependencies
File renamed without changes.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,16 @@
2323
"Dan Abramov <[email protected]> (https://github.com/gaearon)",
2424
"Andrew Clark <[email protected]> (https://github.com/acdlite)"
2525
],
26-
"main": "lib/redux.js",
27-
"module": "es/redux.js",
26+
"type": "module",
27+
"module": "dist/es/index.js",
28+
"main": "dist/cjs/index.cjs",
2829
"types": "types/index.d.ts",
2930
"exports": {
3031
"./package.json": "./package.json",
3132
".": {
3233
"types": "./types/index.d.ts",
33-
"import": "./es/redux.js",
34-
"default": "./lib/redux.js"
34+
"import": "./dist/es/index.js",
35+
"default": "./dist/cjs/index.cjs"
3536
}
3637
},
3738
"files": [

rollup.config.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,18 @@ import nodeResolve from '@rollup/plugin-node-resolve'
33
import babel from '@rollup/plugin-babel'
44
import replace from '@rollup/plugin-replace'
55
import typescript from 'rollup-plugin-typescript2'
6-
import { terser } from 'rollup-plugin-terser'
7-
8-
import pkg from './package.json'
6+
import terser from '@rollup/plugin-terser'
97

108
const extensions = ['.ts']
119
const noDeclarationFiles = { compilerOptions: { declaration: false } }
1210

13-
const babelRuntimeVersion = pkg.dependencies['@babel/runtime'].replace(
14-
/^[^0-9]*/,
15-
''
16-
)
17-
18-
const external = [
19-
...Object.keys(pkg.dependencies || {}),
20-
...Object.keys(pkg.peerDependencies || {})
21-
].map(name => RegExp(`^${name}($|/)`))
11+
const external = []
2212

2313
export default defineConfig([
2414
// CommonJS
2515
{
2616
input: 'src/index.ts',
27-
output: { file: 'lib/redux.js', format: 'cjs', indent: false },
17+
output: { file: 'dist/cjs/index.cjs', format: 'cjs', indent: false },
2818
external,
2919
plugins: [
3020
nodeResolve({
@@ -33,7 +23,7 @@ export default defineConfig([
3323
typescript({ useTsconfigDeclarationDir: true }),
3424
babel({
3525
extensions,
36-
plugins: [['./scripts/mangleErrors.js', { minify: false }]],
26+
plugins: [['./scripts/mangleErrors.cjs', { minify: false }]],
3727
babelHelpers: 'bundled'
3828
})
3929
]
@@ -42,7 +32,7 @@ export default defineConfig([
4232
// ES
4333
{
4434
input: 'src/index.ts',
45-
output: { file: 'es/redux.js', format: 'es', indent: false },
35+
output: { file: 'dist/es/index.js', format: 'es', indent: false },
4636
external,
4737
plugins: [
4838
nodeResolve({
@@ -51,7 +41,7 @@ export default defineConfig([
5141
typescript({ tsconfigOverride: noDeclarationFiles }),
5242
babel({
5343
extensions,
54-
plugins: [['./scripts/mangleErrors.js', { minify: false }]],
44+
plugins: [['./scripts/mangleErrors.cjs', { minify: false }]],
5545
babelHelpers: 'bundled'
5646
})
5747
]
@@ -60,7 +50,7 @@ export default defineConfig([
6050
// ES for Browsers
6151
{
6252
input: 'src/index.ts',
63-
output: { file: 'es/redux.mjs', format: 'es', indent: false },
53+
output: { file: 'dist/es/redux.mjs', format: 'es', indent: false },
6454
plugins: [
6555
nodeResolve({
6656
extensions
@@ -73,7 +63,7 @@ export default defineConfig([
7363
babel({
7464
extensions,
7565
exclude: 'node_modules/**',
76-
plugins: [['./scripts/mangleErrors.js', { minify: true }]],
66+
plugins: [['./scripts/mangleErrors.cjs', { minify: true }]],
7767
skipPreflightCheck: true,
7868
babelHelpers: 'bundled'
7969
}),

scripts/mangleErrors.js renamed to scripts/mangleErrors.cjs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ module.exports = babel => {
7171
},
7272
visitor: {
7373
ThrowStatement(path, file) {
74-
const arguments = path.node.argument.arguments
74+
const args = path.node.argument.arguments
7575
const minify = file.opts.minify
7676

77-
if (arguments && arguments[0]) {
77+
if (args && args[0]) {
7878
// Skip running this logic when certain types come up:
7979
// Identifier comes up when a variable is thrown (E.g. throw new error(message))
8080
// NumericLiteral, CallExpression, and ConditionalExpression is code we have already processed
@@ -103,11 +103,12 @@ module.exports = babel => {
103103
}
104104

105105
// Import the error message function
106-
const formatProdErrorMessageIdentifier = helperModuleImports.addDefault(
107-
path,
108-
'src/utils/formatProdErrorMessage',
109-
{ nameHint: 'formatProdErrorMessage' }
110-
)
106+
const formatProdErrorMessageIdentifier =
107+
helperModuleImports.addDefault(
108+
path,
109+
'src/utils/formatProdErrorMessage',
110+
{ nameHint: 'formatProdErrorMessage' }
111+
)
111112

112113
// Creates a function call to output the message to the error code page on the website
113114
const prodMessage = t.callExpression(
File renamed without changes.

0 commit comments

Comments
 (0)