Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework build setup and hopefully fix ESM compat issues #3318

Merged
merged 4 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ jobs:

# Read existing version, reuse that, add a Git short hash
- name: Set build version to Git commit
run: node scripts/writeGitVersion.js $(git rev-parse --short HEAD)
run: node scripts/writeGitVersion.mjs $(git rev-parse --short HEAD)

- name: Check updated version
run: jq .version package.json
Expand Down Expand Up @@ -190,10 +190,16 @@ jobs:
run: yarn add ./package.tgz

- name: Show installed RTK versions
run: yarn info @reduxjs/toolkit
run: yarn info @reduxjs/toolkit && yarn why @reduxjs/toolkit

- name: Build example
run: yarn build

- name: Run test step
run: yarn test
if: matrix.example != 'are-the-types-wrong'

- name: Run test step (attw)
# Ignore "FalseCJS" errors in the `attw` job
run: yarn test -n FalseCJS
if: matrix.example == 'are-the-types-wrong'
2 changes: 1 addition & 1 deletion examples/publish-ci/node-esm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"type": "module",
"scripts": {
"build": "echo Done",
"test": "node test-cjs.cjs"
"test": "node test-cjs.cjs && node test-esm.mjs"
},
"dependencies": {
"@reduxjs/toolkit": "^1.9.3",
Expand Down
6 changes: 3 additions & 3 deletions examples/publish-ci/node-esm/test-cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ for (let [fn, name, category] of entries) {
}

const moduleNames = [
['@reduxjs/toolkit', 'dist/index.js'],
['@reduxjs/toolkit/query', 'dist/query/index.js'],
['@reduxjs/toolkit/query/react', 'dist/query/react/index.js'],
['@reduxjs/toolkit', 'dist/cjs/index.js'],
['@reduxjs/toolkit/query', 'dist/query/cjs/index.js'],
['@reduxjs/toolkit/query/react', 'dist/query/react/cjs/index.js'],
]

for (let [moduleName, expectedFilename] of moduleNames) {
Expand Down
9 changes: 6 additions & 3 deletions examples/publish-ci/node-esm/test-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ for (let [fn, name, category] of entries) {
}

const moduleNames = [
['@reduxjs/toolkit', 'dist/index.js'],
['@reduxjs/toolkit/query', 'dist/query/index.js'],
['@reduxjs/toolkit/query/react', 'dist/query/react/index.js'],
['@reduxjs/toolkit', 'dist/redux-toolkit.modern.mjs'],
['@reduxjs/toolkit/query', 'dist/query/rtk-query.modern.mjs'],
[
'@reduxjs/toolkit/query/react',
'dist/query/react/rtk-query-react.modern.mjs',
],
]

;(async () => {
Expand Down
2 changes: 1 addition & 1 deletion examples/publish-ci/node-standard/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"scripts": {
"build": "echo Done",
"test": "node test-cjs.js"
"test": "node test-cjs.js && node test-esm.mjs"
},
"dependencies": {
"@reduxjs/toolkit": "^1.9.3",
Expand Down
6 changes: 3 additions & 3 deletions examples/publish-ci/node-standard/test-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ for (let [fn, name, category] of entries) {
}

const moduleNames = [
['@reduxjs/toolkit', 'dist/index.js'],
['@reduxjs/toolkit/query', 'dist/query/index.js'],
['@reduxjs/toolkit/query/react', 'dist/query/react/index.js'],
['@reduxjs/toolkit', 'dist/cjs/index.js'],
['@reduxjs/toolkit/query', 'dist/query/cjs/index.js'],
['@reduxjs/toolkit/query/react', 'dist/query/react/cjs/index.js'],
]

for (let [moduleName, expectedFilename] of moduleNames) {
Expand Down
9 changes: 6 additions & 3 deletions examples/publish-ci/node-standard/test-esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ for (let [fn, name, category] of entries) {
}

const moduleNames = [
['@reduxjs/toolkit', 'dist/index.js'],
['@reduxjs/toolkit/query', 'dist/query/index.js'],
['@reduxjs/toolkit/query/react', 'dist/query/react/index.js'],
['@reduxjs/toolkit', 'dist/redux-toolkit.modern.mjs'],
['@reduxjs/toolkit/query', 'dist/query/rtk-query.modern.mjs'],
[
'@reduxjs/toolkit/query/react',
'dist/query/react/rtk-query-react.modern.mjs',
],
]

;(async () => {
Expand Down
20 changes: 8 additions & 12 deletions packages/toolkit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,24 @@
"publishConfig": {
"access": "public"
},
"type": "module",
"module": "dist/redux-toolkit.modern.js",
"module": "dist/redux-toolkit.legacy-esm.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",
"import": "./dist/redux-toolkit.modern.mjs",
"default": "./dist/cjs/index.js"
},
"./query": {
"types": "./dist/query/index.d.ts",
"import": "./dist/query/rtk-query.modern.js",
"import": "./dist/query/rtk-query.modern.mjs",
"default": "./dist/query/cjs/index.js"
},
"./query/react": {
"types": "./dist/query/react/index.d.ts",
"import": "./dist/query/react/rtk-query-react.modern.js",
"import": "./dist/query/react/rtk-query-react.modern.mjs",
"default": "./dist/query/react/cjs/index.js"
}
},
Expand Down Expand Up @@ -99,7 +98,7 @@
"run-build": "tsx ./scripts/build.ts",
"build-ci": "yarn rimraf dist && yarn tsc && yarn run-build --skipExtraction",
"build-prepare": "npm run build-ci",
"build": "yarn rimraf dist && yarn tsc && yarn run-build --local --skipExtraction",
"build": "yarn rimraf dist && echo Compiling... && yarn tsc && yarn run-build --local --skipExtraction",
"build-only": "yarn rimraf dist && yarn tsc && yarn run-build --skipExtraction",
"format": "prettier --write \"(src|examples)/**/*.{ts,tsx}\" \"**/*.md\"",
"format:check": "prettier --list-different \"(src|examples)/**/*.{ts,tsx}\" \"docs/*/**.md\"",
Expand All @@ -109,17 +108,14 @@
"prepack": "npm run build-prepare"
},
"files": [
"dist/**/*.js",
"dist/**/*.js.map",
"dist/**/*.d.ts",
"dist/**/package.json",
"dist/",
"src/",
"query"
],
"dependencies": {
"immer": "^9.0.16",
"redux": "5.0.0-alpha.2",
"redux-thunk": "3.0.0-alpha.1",
"redux": "5.0.0-alpha.4",
"redux-thunk": "3.0.0-alpha.3",
"reselect": "^4.1.7"
},
"peerDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/query/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"version": "1.0.0",
"description": "",
"type": "module",
"module": "../dist/query/rtk-query.modern.js",
"module": "../dist/query/rtk-query.legacy-esm.js",
"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",
"import": "./../dist/query/rtk-query.modern.mjs",
"default": "./../dist/query/cjs/index.js"
}
},
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/query/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
"version": "1.0.0",
"description": "",
"type": "module",
"module": "../../dist/query/react/rtk-query-react.modern.js",
"module": "../../dist/query/react/rtk-query-react.legacy-esm.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",
"import": "./../../dist/query/react/rtk-query-react.modern.mjs",
"default": "./../../dist/query/react/cjs/index.js"
}
},
Expand Down
22 changes: 17 additions & 5 deletions packages/toolkit/scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ const outputDir = path.join(__dirname, '../dist')
const buildTargets: BuildOptions[] = [
{
format: 'cjs',
name: 'cjs.development',
name: 'development',
target: 'esnext',
minify: false,
env: 'development',
},
{
format: 'cjs',
name: 'cjs.production.min',
name: 'production.min',
target: 'esnext',
minify: true,
env: 'production',
Expand All @@ -59,6 +59,15 @@ const buildTargets: BuildOptions[] = [
minify: false,
env: '',
},
// ESM, embedded `process`: fallback for Webpack 4,
// which doesn't support `exports` field or optional chaining
{
format: 'esm',
name: 'legacy-esm',
target: 'esnext',
minify: false,
env: '',
},
// ESM, pre-compiled "dev": browser development
{
format: 'esm',
Expand Down Expand Up @@ -145,8 +154,11 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
folderSegments.push('cjs')
}

const extension =
name === 'legacy-esm' ? 'js' : format === 'esm' ? 'mjs' : 'cjs'

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

await fs.ensureDir(outputFolder)

Expand Down Expand Up @@ -306,9 +318,9 @@ async function writeCommonJSEntry(folder: string, prefix: string) {
path.join(folder, 'index.js'),
`'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./${prefix}.cjs.production.min.js')
module.exports = require('./${prefix}.production.min.cjs')
} else {
module.exports = require('./${prefix}.cjs.development.js')
module.exports = require('./${prefix}.development.cjs')
}`
)

Expand Down
6 changes: 3 additions & 3 deletions packages/toolkit/scripts/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export interface BuildOptions {
format: 'cjs' | 'umd' | 'esm'
name:
| 'cjs.development'
| 'cjs.production.min'
| 'esm'
| 'development'
| 'production.min'
| 'legacy-esm'
| 'modern'
| 'modern.development'
| 'modern.production.min'
Expand Down
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6781,8 +6781,8 @@ __metadata:
node-fetch: ^2.6.1
prettier: ^2.2.1
query-string: ^7.0.1
redux: 5.0.0-alpha.2
redux-thunk: 3.0.0-alpha.1
redux: 5.0.0-alpha.4
redux-thunk: 3.0.0-alpha.3
reselect: ^4.1.7
rimraf: ^3.0.2
rollup: ^2.47.0
Expand Down Expand Up @@ -24477,12 +24477,12 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"redux-thunk@npm:3.0.0-alpha.1":
version: 3.0.0-alpha.1
resolution: "redux-thunk@npm:3.0.0-alpha.1"
"redux-thunk@npm:3.0.0-alpha.3":
version: 3.0.0-alpha.3
resolution: "redux-thunk@npm:3.0.0-alpha.3"
peerDependencies:
redux: ^4
checksum: 280e0d399c96d071030f6dfa5bdb0b05b9f228dd0b840ffbb213e778b773efc25e6ba24f7ed13818696ea54a359ecc06a9e5e718e07d960a772101b14c0dd8c7
checksum: a5be77887b422b3182ff7fae617ec552cd5f830afb326d83af32a430c3eb439c942a38c3691e5c975119e37787974172dbc0139f7782cbfaeea5c1292fa123ed
languageName: node
linkType: hard

Expand All @@ -24504,10 +24504,10 @@ fsevents@^1.2.7:
languageName: node
linkType: hard

"redux@npm:5.0.0-alpha.2":
version: 5.0.0-alpha.2
resolution: "redux@npm:5.0.0-alpha.2"
checksum: fbae31c55bab62a210a5a24a64721f593080fb94808c547b646ddff91515f8ab4e3363af5ece16491f0179d43729fc0350235c9fea7500b37b67d762a55b6945
"redux@npm:5.0.0-alpha.4":
version: 5.0.0-alpha.4
resolution: "redux@npm:5.0.0-alpha.4"
checksum: ebc98a74d84341df6db87222b6e54a658d68233924315ff67b4b2988ca0ea359e39632f7a43b65ec361ea7ba5714de4e34d76cb0b20089e785d11dc9e5a9e85e
languageName: node
linkType: hard

Expand Down