Skip to content

Commit

Permalink
Migrate Redux package to be full ESM
Browse files Browse the repository at this point in the history
- 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`
  • Loading branch information
markerikson committed Jan 28, 2023
1 parent d0a74ed commit 007992f
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 29 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@
"Dan Abramov <dan.abramov@me.com> (https://github.com/gaearon)",
"Andrew Clark <acdlite@me.com> (https://github.com/acdlite)"
],
"main": "lib/redux.js",
"module": "es/redux.js",
"type": "module",
"module": "dist/es/index.js",
"main": "dist/cjs/index.cjs",
"types": "types/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./types/index.d.ts",
"import": "./es/redux.js",
"default": "./lib/redux.js"
"import": "./dist/es/index.js",
"default": "./dist/cjs/index.cjs"
}
},
"files": [
Expand Down
26 changes: 8 additions & 18 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,18 @@ import nodeResolve from '@rollup/plugin-node-resolve'
import babel from '@rollup/plugin-babel'
import replace from '@rollup/plugin-replace'
import typescript from 'rollup-plugin-typescript2'
import { terser } from 'rollup-plugin-terser'

import pkg from './package.json'
import terser from '@rollup/plugin-terser'

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

const babelRuntimeVersion = pkg.dependencies['@babel/runtime'].replace(
/^[^0-9]*/,
''
)

const external = [
...Object.keys(pkg.dependencies || {}),
...Object.keys(pkg.peerDependencies || {})
].map(name => RegExp(`^${name}($|/)`))
const external = []

export default defineConfig([
// CommonJS
{
input: 'src/index.ts',
output: { file: 'lib/redux.js', format: 'cjs', indent: false },
output: { file: 'dist/cjs/index.cjs', format: 'cjs', indent: false },
external,
plugins: [
nodeResolve({
Expand All @@ -33,7 +23,7 @@ export default defineConfig([
typescript({ useTsconfigDeclarationDir: true }),
babel({
extensions,
plugins: [['./scripts/mangleErrors.js', { minify: false }]],
plugins: [['./scripts/mangleErrors.cjs', { minify: false }]],
babelHelpers: 'bundled'
})
]
Expand All @@ -42,7 +32,7 @@ export default defineConfig([
// ES
{
input: 'src/index.ts',
output: { file: 'es/redux.js', format: 'es', indent: false },
output: { file: 'dist/es/index.js', format: 'es', indent: false },
external,
plugins: [
nodeResolve({
Expand All @@ -51,7 +41,7 @@ export default defineConfig([
typescript({ tsconfigOverride: noDeclarationFiles }),
babel({
extensions,
plugins: [['./scripts/mangleErrors.js', { minify: false }]],
plugins: [['./scripts/mangleErrors.cjs', { minify: false }]],
babelHelpers: 'bundled'
})
]
Expand All @@ -60,7 +50,7 @@ export default defineConfig([
// ES for Browsers
{
input: 'src/index.ts',
output: { file: 'es/redux.mjs', format: 'es', indent: false },
output: { file: 'dist/es/redux.mjs', format: 'es', indent: false },
plugins: [
nodeResolve({
extensions
Expand All @@ -73,7 +63,7 @@ export default defineConfig([
babel({
extensions,
exclude: 'node_modules/**',
plugins: [['./scripts/mangleErrors.js', { minify: true }]],
plugins: [['./scripts/mangleErrors.cjs', { minify: true }]],
skipPreflightCheck: true,
babelHelpers: 'bundled'
}),
Expand Down
15 changes: 8 additions & 7 deletions scripts/mangleErrors.js → scripts/mangleErrors.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ module.exports = babel => {
},
visitor: {
ThrowStatement(path, file) {
const arguments = path.node.argument.arguments
const args = path.node.argument.arguments
const minify = file.opts.minify

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

// Import the error message function
const formatProdErrorMessageIdentifier = helperModuleImports.addDefault(
path,
'src/utils/formatProdErrorMessage',
{ nameHint: 'formatProdErrorMessage' }
)
const formatProdErrorMessageIdentifier =
helperModuleImports.addDefault(
path,
'src/utils/formatProdErrorMessage',
{ nameHint: 'formatProdErrorMessage' }
)

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

0 comments on commit 007992f

Please sign in to comment.