Skip to content

Commit aca78ec

Browse files
authored
Merge pull request #3095 from reduxjs/feature/2.0-module-exports
2 parents d717c90 + 6f9591e commit aca78ec

14 files changed

+164
-71
lines changed

packages/toolkit/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = {
1+
export default {
22
testEnvironment: 'jest-environment-jsdom',
33
setupFilesAfterEnv: ['./jest.setup.js'],
44
testMatch: ['<rootDir>/src/**/*.(spec|test).[jt]s?(x)'],

packages/toolkit/package.json

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,28 @@
2222
"publishConfig": {
2323
"access": "public"
2424
},
25-
"main": "dist/index.js",
25+
"type": "module",
2626
"module": "dist/redux-toolkit.modern.js",
27-
"unpkg": "dist/redux-toolkit.umd.min.js",
27+
"main": "dist/cjs/index.js",
2828
"types": "dist/index.d.ts",
29+
"exports": {
30+
"./package.json": "./package.json",
31+
".": {
32+
"types": "./dist/index.d.ts",
33+
"import": "./dist/redux-toolkit.modern.js",
34+
"default": "./dist/cjs/index.js"
35+
},
36+
"./query": {
37+
"types": "./dist/query/index.d.ts",
38+
"import": "./dist/query/rtk-query.modern.js",
39+
"default": "./dist/query/cjs/index.js"
40+
},
41+
"./query/react": {
42+
"types": "./dist/query/react/index.d.ts",
43+
"import": "./dist/query/react/rtk-query-react.modern.js",
44+
"default": "./dist/query/react/cjs/index.js"
45+
}
46+
},
2947
"devDependencies": {
3048
"@microsoft/api-extractor": "^7.13.2",
3149
"@size-limit/preset-small-lib": "^4.11.0",
@@ -101,7 +119,7 @@
101119
"dependencies": {
102120
"immer": "^9.0.16",
103121
"redux": "^4.2.0",
104-
"redux-thunk": "^2.4.2",
122+
"redux-thunk": "3.0.0-alpha.1",
105123
"reselect": "^4.1.7"
106124
},
107125
"peerDependencies": {

packages/toolkit/query/package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
"name": "@reduxjs/toolkit-query",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "../dist/query/index.js",
5+
"type": "module",
66
"module": "../dist/query/rtk-query.modern.js",
7-
"unpkg": "../dist/query/rtk-query.umd.min.js",
8-
"types": "../dist/query/index.d.ts",
7+
"main": "../dist/query/cjs/index.js",
8+
"types": "./../dist/query/index.d.ts",
9+
"exports": {
10+
"./package.json": "./package.json",
11+
".": {
12+
"types": "./../dist/query/index.d.ts",
13+
"import": "./../dist/query/rtk-query.modern.js",
14+
"default": "./../dist/query/cjs/index.js"
15+
}
16+
},
917
"author": "Mark Erikson <[email protected]>",
1018
"license": "MIT",
1119
"sideEffects": false

packages/toolkit/query/react/package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
"name": "@reduxjs/toolkit-query-react",
33
"version": "1.0.0",
44
"description": "",
5-
"main": "../../dist/query/react/index.js",
5+
"type": "module",
66
"module": "../../dist/query/react/rtk-query-react.modern.js",
7-
"unpkg": "../../dist/query/react/rtk-query-react.umd.min.js",
7+
"main": "../../dist/query/react/cjs/index.js",
8+
"types": "./../../dist/query/react/index.d.ts",
9+
"exports": {
10+
"./package.json": "./package.json",
11+
".": {
12+
"types": "./../../dist/query/react/index.d.ts",
13+
"import": "./../../dist/query/react/rtk-query-react.modern.js",
14+
"default": "./../../dist/query/react/cjs/index.js"
15+
}
16+
},
817
"author": "Mark Erikson <[email protected]>",
918
"license": "MIT",
10-
"types": "../../dist/query/react/index.d.ts",
1119
"sideEffects": false
1220
}

packages/toolkit/scripts/build.ts

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
/* eslint-disable import/first */
2+
import { fileURLToPath } from 'url'
3+
24
// @ts-check
35
import { build } from 'esbuild'
4-
import terser from 'terser'
6+
import { minify as terserMinify } from 'terser'
57
import { rollup } from 'rollup'
68
import path from 'path'
79
import fs from 'fs-extra'
@@ -16,7 +18,9 @@ import { extractInlineSourcemap, removeInlineSourceMap } from './sourcemap'
1618
import type { BuildOptions, EntryPointOptions } from './types'
1719
import { appendInlineSourceMap, getLocation } from './sourcemap'
1820

19-
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))
21+
// No __dirname under Node ESM
22+
const __filename = fileURLToPath(import.meta.url)
23+
const __dirname = path.dirname(__filename)
2024

2125
const { argv } = yargs(process.argv)
2226
.option('local', {
@@ -36,55 +40,55 @@ const buildTargets: BuildOptions[] = [
3640
{
3741
format: 'cjs',
3842
name: 'cjs.development',
39-
target: 'es2018',
43+
target: 'esnext',
4044
minify: false,
4145
env: 'development',
4246
},
4347
{
4448
format: 'cjs',
4549
name: 'cjs.production.min',
46-
target: 'es2018',
50+
target: 'esnext',
4751
minify: true,
4852
env: 'production',
4953
},
5054
// ESM, embedded `process`, ES2017 syntax: modern Webpack dev
5155
{
5256
format: 'esm',
5357
name: 'modern',
54-
target: 'es2018',
58+
target: 'esnext',
5559
minify: false,
5660
env: '',
5761
},
5862
// ESM, pre-compiled "dev", ES2017 syntax: browser development
5963
{
6064
format: 'esm',
6165
name: 'modern.development',
62-
target: 'es2018',
66+
target: 'esnext',
6367
minify: false,
6468
env: 'development',
6569
},
6670
// ESM, pre-compiled "prod", ES2017 syntax: browser prod
6771
{
6872
format: 'esm',
6973
name: 'modern.production.min',
70-
target: 'es2018',
71-
minify: true,
72-
env: 'production',
73-
},
74-
{
75-
format: 'umd',
76-
name: 'umd',
77-
target: 'es2018',
78-
minify: false,
79-
env: 'development',
80-
},
81-
{
82-
format: 'umd',
83-
name: 'umd.min',
84-
target: 'es2018',
74+
target: 'esnext',
8575
minify: true,
8676
env: 'production',
8777
},
78+
// {
79+
// format: 'umd',
80+
// name: 'umd',
81+
// target: 'es2018',
82+
// minify: false,
83+
// env: 'development',
84+
// },
85+
// {
86+
// format: 'umd',
87+
// name: 'umd.min',
88+
// target: 'es2018',
89+
// minify: true,
90+
// env: 'production',
91+
// },
8892
]
8993

9094
const entryPoints: EntryPointOptions[] = [
@@ -118,6 +122,9 @@ const esVersionMappings = {
118122
es2018: ts.ScriptTarget.ES2018,
119123
es2019: ts.ScriptTarget.ES2019,
120124
es2020: ts.ScriptTarget.ES2020,
125+
es2021: ts.ScriptTarget.ES2021,
126+
es2022: ts.ScriptTarget.ES2022,
127+
esnext: ts.ScriptTarget.ESNext,
121128
}
122129

123130
async function bundle(options: BuildOptions & EntryPointOptions) {
@@ -132,10 +139,23 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
132139
entryPoint,
133140
} = options
134141

135-
const outputFolder = path.join('dist', folder)
142+
const folderSegments = [outputDir, folder]
143+
144+
if (format === 'cjs') {
145+
folderSegments.push('cjs')
146+
}
147+
148+
const outputFolder = path.join(...folderSegments)
136149
const outputFilename = `${prefix}.${name}.js`
150+
151+
await fs.ensureDir(outputFolder)
152+
137153
const outputFilePath = path.join(outputFolder, outputFilename)
138154

155+
if (format === 'cjs') {
156+
await writeCommonJSEntry(outputFolder, prefix)
157+
}
158+
139159
const result = await build({
140160
entryPoints: [entryPoint],
141161
outfile: outputFilePath,
@@ -212,7 +232,7 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
212232
let mapping: RawSourceMap = mergedSourcemap
213233

214234
if (minify) {
215-
const transformResult = await terser.minify(
235+
const transformResult = await terserMinify(
216236
appendInlineSourceMap(code, mapping),
217237
{
218238
sourceMap: {
@@ -237,12 +257,14 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
237257
}
238258

239259
const relativePath = path.relative(process.cwd(), chunk.path)
240-
console.log(`Build artifact: ${relativePath}, settings: `, {
241-
target,
242-
output: ts.ScriptTarget[esVersion],
243-
})
244260
await fs.writeFile(chunk.path, code)
245261
await fs.writeJSON(chunk.path + '.map', mapping)
262+
263+
if (!chunk.path.includes('.map')) {
264+
console.log(`Build artifact: ${relativePath}, settings: `, {
265+
target,
266+
})
267+
}
246268
}
247269
}
248270

@@ -279,16 +301,18 @@ async function buildUMD(
279301
}
280302

281303
// Generates an index file to handle importing CJS dev/prod
282-
async function writeEntry(folder: string, prefix: string) {
304+
async function writeCommonJSEntry(folder: string, prefix: string) {
283305
await fs.writeFile(
284-
path.join('dist', folder, 'index.js'),
306+
path.join(folder, 'index.js'),
285307
`'use strict'
286308
if (process.env.NODE_ENV === 'production') {
287309
module.exports = require('./${prefix}.cjs.production.min.js')
288310
} else {
289311
module.exports = require('./${prefix}.cjs.development.js')
290312
}`
291313
)
314+
315+
await fs.writeFile(path.join(folder, 'package.json'), `{"type": "commonjs"}`)
292316
}
293317

294318
interface BuildArgs {
@@ -313,14 +337,13 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) {
313337
})
314338
)
315339
await Promise.all(bundlePromises)
316-
await writeEntry(folder, prefix)
317340
}
318341

319342
// Run UMD builds after everything else so we don't have to sleep after each set
320343
for (let entryPoint of entryPoints) {
321344
const { folder } = entryPoint
322345
const outputPath = path.join('dist', folder)
323-
await buildUMD(outputPath, entryPoint.prefix, entryPoint.globalName)
346+
// await buildUMD(outputPath, entryPoint.prefix, entryPoint.globalName)
324347
}
325348

326349
if (!skipExtraction) {

packages/toolkit/scripts/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ export interface BuildOptions {
1111
| 'umd.min'
1212
minify: boolean
1313
env: 'development' | 'production' | ''
14-
target?: 'es2017' | 'es2018' | 'es2019' | 'es2020'
14+
target?:
15+
| 'es2017'
16+
| 'es2018'
17+
| 'es2019'
18+
| 'es2020'
19+
| 'es2021'
20+
| 'es2022'
21+
| 'esnext'
1522
}
1623

1724
export interface EntryPointOptions {

packages/toolkit/src/configureStore.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const IS_PRODUCTION = process.env.NODE_ENV === 'production'
3434
* @public
3535
*/
3636
export type ConfigureEnhancersCallback<E extends Enhancers = Enhancers> = (
37-
defaultEnhancers: readonly StoreEnhancer[]
37+
defaultEnhancers: readonly StoreEnhancer[]
3838
) => [...E]
3939

4040
/**
@@ -107,7 +107,7 @@ type Enhancers = ReadonlyArray<StoreEnhancer>
107107
export interface ToolkitStore<
108108
S = any,
109109
A extends Action = AnyAction,
110-
M extends Middlewares<S> = Middlewares<S>,
110+
M extends Middlewares<S> = Middlewares<S>
111111
> extends Store<S, A> {
112112
/**
113113
* The `dispatch` method of your store, enhanced by all its middlewares.

packages/toolkit/src/createReducer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Draft } from 'immer'
2-
import createNextState, { isDraft, isDraftable } from 'immer'
2+
import { produce as createNextState, isDraft, isDraftable } from 'immer'
33
import type { AnyAction, Action, Reducer } from 'redux'
44
import type { ActionReducerMapBuilder } from './mapBuilders'
55
import { executeReducerBuilderCallback } from './mapBuilders'

packages/toolkit/src/entities/state_adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import createNextState, { isDraft } from 'immer'
1+
import { produce as createNextState, isDraft } from 'immer'
22
import type { EntityState, PreventAny } from './models'
33
import type { PayloadAction } from '../createAction'
44
import { isFSA } from '../createAction'

packages/toolkit/src/getDefaultMiddleware.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Middleware, AnyAction } from 'redux'
22
import type { ThunkMiddleware } from 'redux-thunk'
3-
import thunkMiddleware from 'redux-thunk'
3+
import { thunk as thunkMiddleware, withExtraArgument } from 'redux-thunk'
44
import type { ImmutableStateInvariantMiddlewareOptions } from './immutableStateInvariantMiddleware'
55
/* PROD_START_REMOVE_UMD */
66
import { createImmutableStateInvariantMiddleware } from './immutableStateInvariantMiddleware'
@@ -88,9 +88,7 @@ export function getDefaultMiddleware<
8888
if (isBoolean(thunk)) {
8989
middlewareArray.push(thunkMiddleware)
9090
} else {
91-
middlewareArray.push(
92-
thunkMiddleware.withExtraArgument(thunk.extraArgument)
93-
)
91+
middlewareArray.push(withExtraArgument(thunk.extraArgument))
9492
}
9593
}
9694

0 commit comments

Comments
 (0)