Skip to content

Commit

Permalink
Merge pull request #3095 from reduxjs/feature/2.0-module-exports
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Jan 21, 2023
2 parents d717c90 + 6f9591e commit aca78ec
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 71 deletions.
2 changes: 1 addition & 1 deletion packages/toolkit/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
testEnvironment: 'jest-environment-jsdom',
setupFilesAfterEnv: ['./jest.setup.js'],
testMatch: ['<rootDir>/src/**/*.(spec|test).[jt]s?(x)'],
Expand Down
24 changes: 21 additions & 3 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,28 @@
"publishConfig": {
"access": "public"
},
"main": "dist/index.js",
"type": "module",
"module": "dist/redux-toolkit.modern.js",
"unpkg": "dist/redux-toolkit.umd.min.js",
"main": "dist/cjs/index.js",
"types": "dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/redux-toolkit.modern.js",
"default": "./dist/cjs/index.js"
},
"./query": {
"types": "./dist/query/index.d.ts",
"import": "./dist/query/rtk-query.modern.js",
"default": "./dist/query/cjs/index.js"
},
"./query/react": {
"types": "./dist/query/react/index.d.ts",
"import": "./dist/query/react/rtk-query-react.modern.js",
"default": "./dist/query/react/cjs/index.js"
}
},
"devDependencies": {
"@microsoft/api-extractor": "^7.13.2",
"@size-limit/preset-small-lib": "^4.11.0",
Expand Down Expand Up @@ -101,7 +119,7 @@
"dependencies": {
"immer": "^9.0.16",
"redux": "^4.2.0",
"redux-thunk": "^2.4.2",
"redux-thunk": "3.0.0-alpha.1",
"reselect": "^4.1.7"
},
"peerDependencies": {
Expand Down
14 changes: 11 additions & 3 deletions packages/toolkit/query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
"name": "@reduxjs/toolkit-query",
"version": "1.0.0",
"description": "",
"main": "../dist/query/index.js",
"type": "module",
"module": "../dist/query/rtk-query.modern.js",
"unpkg": "../dist/query/rtk-query.umd.min.js",
"types": "../dist/query/index.d.ts",
"main": "../dist/query/cjs/index.js",
"types": "./../dist/query/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./../dist/query/index.d.ts",
"import": "./../dist/query/rtk-query.modern.js",
"default": "./../dist/query/cjs/index.js"
}
},
"author": "Mark Erikson <mark@isquaredsoftware.com>",
"license": "MIT",
"sideEffects": false
Expand Down
14 changes: 11 additions & 3 deletions packages/toolkit/query/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
"name": "@reduxjs/toolkit-query-react",
"version": "1.0.0",
"description": "",
"main": "../../dist/query/react/index.js",
"type": "module",
"module": "../../dist/query/react/rtk-query-react.modern.js",
"unpkg": "../../dist/query/react/rtk-query-react.umd.min.js",
"main": "../../dist/query/react/cjs/index.js",
"types": "./../../dist/query/react/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./../../dist/query/react/index.d.ts",
"import": "./../../dist/query/react/rtk-query-react.modern.js",
"default": "./../../dist/query/react/cjs/index.js"
}
},
"author": "Mark Erikson <mark@isquaredsoftware.com>",
"license": "MIT",
"types": "../../dist/query/react/index.d.ts",
"sideEffects": false
}
85 changes: 54 additions & 31 deletions packages/toolkit/scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable import/first */
import { fileURLToPath } from 'url'

// @ts-check
import { build } from 'esbuild'
import terser from 'terser'
import { minify as terserMinify } from 'terser'
import { rollup } from 'rollup'
import path from 'path'
import fs from 'fs-extra'
Expand All @@ -16,7 +18,9 @@ import { extractInlineSourcemap, removeInlineSourceMap } from './sourcemap'
import type { BuildOptions, EntryPointOptions } from './types'
import { appendInlineSourceMap, getLocation } from './sourcemap'

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

const { argv } = yargs(process.argv)
.option('local', {
Expand All @@ -36,55 +40,55 @@ const buildTargets: BuildOptions[] = [
{
format: 'cjs',
name: 'cjs.development',
target: 'es2018',
target: 'esnext',
minify: false,
env: 'development',
},
{
format: 'cjs',
name: 'cjs.production.min',
target: 'es2018',
target: 'esnext',
minify: true,
env: 'production',
},
// ESM, embedded `process`, ES2017 syntax: modern Webpack dev
{
format: 'esm',
name: 'modern',
target: 'es2018',
target: 'esnext',
minify: false,
env: '',
},
// ESM, pre-compiled "dev", ES2017 syntax: browser development
{
format: 'esm',
name: 'modern.development',
target: 'es2018',
target: 'esnext',
minify: false,
env: 'development',
},
// ESM, pre-compiled "prod", ES2017 syntax: browser prod
{
format: 'esm',
name: 'modern.production.min',
target: 'es2018',
minify: true,
env: 'production',
},
{
format: 'umd',
name: 'umd',
target: 'es2018',
minify: false,
env: 'development',
},
{
format: 'umd',
name: 'umd.min',
target: 'es2018',
target: 'esnext',
minify: true,
env: 'production',
},
// {
// format: 'umd',
// name: 'umd',
// target: 'es2018',
// minify: false,
// env: 'development',
// },
// {
// format: 'umd',
// name: 'umd.min',
// target: 'es2018',
// minify: true,
// env: 'production',
// },
]

const entryPoints: EntryPointOptions[] = [
Expand Down Expand Up @@ -118,6 +122,9 @@ const esVersionMappings = {
es2018: ts.ScriptTarget.ES2018,
es2019: ts.ScriptTarget.ES2019,
es2020: ts.ScriptTarget.ES2020,
es2021: ts.ScriptTarget.ES2021,
es2022: ts.ScriptTarget.ES2022,
esnext: ts.ScriptTarget.ESNext,
}

async function bundle(options: BuildOptions & EntryPointOptions) {
Expand All @@ -132,10 +139,23 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
entryPoint,
} = options

const outputFolder = path.join('dist', folder)
const folderSegments = [outputDir, folder]

if (format === 'cjs') {
folderSegments.push('cjs')
}

const outputFolder = path.join(...folderSegments)
const outputFilename = `${prefix}.${name}.js`

await fs.ensureDir(outputFolder)

const outputFilePath = path.join(outputFolder, outputFilename)

if (format === 'cjs') {
await writeCommonJSEntry(outputFolder, prefix)
}

const result = await build({
entryPoints: [entryPoint],
outfile: outputFilePath,
Expand Down Expand Up @@ -212,7 +232,7 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
let mapping: RawSourceMap = mergedSourcemap

if (minify) {
const transformResult = await terser.minify(
const transformResult = await terserMinify(
appendInlineSourceMap(code, mapping),
{
sourceMap: {
Expand All @@ -237,12 +257,14 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
}

const relativePath = path.relative(process.cwd(), chunk.path)
console.log(`Build artifact: ${relativePath}, settings: `, {
target,
output: ts.ScriptTarget[esVersion],
})
await fs.writeFile(chunk.path, code)
await fs.writeJSON(chunk.path + '.map', mapping)

if (!chunk.path.includes('.map')) {
console.log(`Build artifact: ${relativePath}, settings: `, {
target,
})
}
}
}

Expand Down Expand Up @@ -279,16 +301,18 @@ async function buildUMD(
}

// Generates an index file to handle importing CJS dev/prod
async function writeEntry(folder: string, prefix: string) {
async function writeCommonJSEntry(folder: string, prefix: string) {
await fs.writeFile(
path.join('dist', folder, 'index.js'),
path.join(folder, 'index.js'),
`'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./${prefix}.cjs.production.min.js')
} else {
module.exports = require('./${prefix}.cjs.development.js')
}`
)

await fs.writeFile(path.join(folder, 'package.json'), `{"type": "commonjs"}`)
}

interface BuildArgs {
Expand All @@ -313,14 +337,13 @@ async function main({ skipExtraction = false, local = false }: BuildArgs) {
})
)
await Promise.all(bundlePromises)
await writeEntry(folder, prefix)
}

// Run UMD builds after everything else so we don't have to sleep after each set
for (let entryPoint of entryPoints) {
const { folder } = entryPoint
const outputPath = path.join('dist', folder)
await buildUMD(outputPath, entryPoint.prefix, entryPoint.globalName)
// await buildUMD(outputPath, entryPoint.prefix, entryPoint.globalName)
}

if (!skipExtraction) {
Expand Down
9 changes: 8 additions & 1 deletion packages/toolkit/scripts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ export interface BuildOptions {
| 'umd.min'
minify: boolean
env: 'development' | 'production' | ''
target?: 'es2017' | 'es2018' | 'es2019' | 'es2020'
target?:
| 'es2017'
| 'es2018'
| 'es2019'
| 'es2020'
| 'es2021'
| 'es2022'
| 'esnext'
}

export interface EntryPointOptions {
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const IS_PRODUCTION = process.env.NODE_ENV === 'production'
* @public
*/
export type ConfigureEnhancersCallback<E extends Enhancers = Enhancers> = (
defaultEnhancers: readonly StoreEnhancer[]
defaultEnhancers: readonly StoreEnhancer[]
) => [...E]

/**
Expand Down Expand Up @@ -107,7 +107,7 @@ type Enhancers = ReadonlyArray<StoreEnhancer>
export interface ToolkitStore<
S = any,
A extends Action = AnyAction,
M extends Middlewares<S> = Middlewares<S>,
M extends Middlewares<S> = Middlewares<S>
> extends Store<S, A> {
/**
* The `dispatch` method of your store, enhanced by all its middlewares.
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/createReducer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Draft } from 'immer'
import createNextState, { isDraft, isDraftable } from 'immer'
import { produce as createNextState, isDraft, isDraftable } from 'immer'
import type { AnyAction, Action, Reducer } from 'redux'
import type { ActionReducerMapBuilder } from './mapBuilders'
import { executeReducerBuilderCallback } from './mapBuilders'
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/src/entities/state_adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createNextState, { isDraft } from 'immer'
import { produce as createNextState, isDraft } from 'immer'
import type { EntityState, PreventAny } from './models'
import type { PayloadAction } from '../createAction'
import { isFSA } from '../createAction'
Expand Down
6 changes: 2 additions & 4 deletions packages/toolkit/src/getDefaultMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Middleware, AnyAction } from 'redux'
import type { ThunkMiddleware } from 'redux-thunk'
import thunkMiddleware from 'redux-thunk'
import { thunk as thunkMiddleware, withExtraArgument } from 'redux-thunk'
import type { ImmutableStateInvariantMiddlewareOptions } from './immutableStateInvariantMiddleware'
/* PROD_START_REMOVE_UMD */
import { createImmutableStateInvariantMiddleware } from './immutableStateInvariantMiddleware'
Expand Down Expand Up @@ -88,9 +88,7 @@ export function getDefaultMiddleware<
if (isBoolean(thunk)) {
middlewareArray.push(thunkMiddleware)
} else {
middlewareArray.push(
thunkMiddleware.withExtraArgument(thunk.extraArgument)
)
middlewareArray.push(withExtraArgument(thunk.extraArgument))
}
}

Expand Down
Loading

0 comments on commit aca78ec

Please sign in to comment.