Skip to content

Commit aae42aa

Browse files
committed
Rework build setup to hopefully really support ESM right this time
- Dropped `type: "module"` from package - Used `.cjs` and `.mjs` extensions for artifacts - Added Webpack 4 fallback artifact
1 parent d150205 commit aae42aa

File tree

10 files changed

+52
-33
lines changed

10 files changed

+52
-33
lines changed

.github/workflows/tests.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,16 @@ jobs:
190190
run: yarn add ./package.tgz
191191

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

195195
- name: Build example
196196
run: yarn build
197197

198198
- name: Run test step
199199
run: yarn test
200+
if: matrix.example != 'are-the-types-wrong'
201+
202+
- name: Run test step (attw)
203+
# Ignore "FalseCJS" errors in the `attw` job
204+
run: yarn test -n FalseCJS
205+
if: matrix.example == 'are-the-types-wrong'

examples/publish-ci/node-esm/test-cjs.cjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ for (let [fn, name, category] of entries) {
3030
}
3131

3232
const moduleNames = [
33-
['@reduxjs/toolkit', 'dist/index.js'],
34-
['@reduxjs/toolkit/query', 'dist/query/index.js'],
35-
['@reduxjs/toolkit/query/react', 'dist/query/react/index.js'],
33+
['@reduxjs/toolkit', 'dist/cjs/index.js'],
34+
['@reduxjs/toolkit/query', 'dist/query/cjs/index.js'],
35+
['@reduxjs/toolkit/query/react', 'dist/query/react/cjs/index.js'],
3636
]
3737

3838
for (let [moduleName, expectedFilename] of moduleNames) {

examples/publish-ci/node-esm/test-esm.mjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ for (let [fn, name, category] of entries) {
3333
}
3434

3535
const moduleNames = [
36-
['@reduxjs/toolkit', 'dist/index.js'],
37-
['@reduxjs/toolkit/query', 'dist/query/index.js'],
38-
['@reduxjs/toolkit/query/react', 'dist/query/react/index.js'],
36+
['@reduxjs/toolkit', 'dist/redux-toolkit.modern.mjs'],
37+
['@reduxjs/toolkit/query', 'dist/query/rtk-query.modern.mjs'],
38+
[
39+
'@reduxjs/toolkit/query/react',
40+
'dist/query/react/rtk-query-react.modern.mjs',
41+
],
3942
]
4043

4144
;(async () => {

examples/publish-ci/node-standard/test-cjs.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ for (let [fn, name, category] of entries) {
3030
}
3131

3232
const moduleNames = [
33-
['@reduxjs/toolkit', 'dist/index.js'],
34-
['@reduxjs/toolkit/query', 'dist/query/index.js'],
35-
['@reduxjs/toolkit/query/react', 'dist/query/react/index.js'],
33+
['@reduxjs/toolkit', 'dist/cjs/index.js'],
34+
['@reduxjs/toolkit/query', 'dist/query/cjs/index.js'],
35+
['@reduxjs/toolkit/query/react', 'dist/query/react/cjs/index.js'],
3636
]
3737

3838
for (let [moduleName, expectedFilename] of moduleNames) {

examples/publish-ci/node-standard/test-esm.mjs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ for (let [fn, name, category] of entries) {
3333
}
3434

3535
const moduleNames = [
36-
['@reduxjs/toolkit', 'dist/index.js'],
37-
['@reduxjs/toolkit/query', 'dist/query/index.js'],
38-
['@reduxjs/toolkit/query/react', 'dist/query/react/index.js'],
36+
['@reduxjs/toolkit', 'dist/redux-toolkit.modern.mjs'],
37+
['@reduxjs/toolkit/query', 'dist/query/rtk-query.modern.mjs'],
38+
[
39+
'@reduxjs/toolkit/query/react',
40+
'dist/query/react/rtk-query-react.modern.mjs',
41+
],
3942
]
4043

4144
;(async () => {

packages/toolkit/package.json

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,24 @@
2222
"publishConfig": {
2323
"access": "public"
2424
},
25-
"type": "module",
26-
"module": "dist/redux-toolkit.modern.js",
25+
"module": "dist/redux-toolkit.esm.mjs",
2726
"main": "dist/cjs/index.js",
2827
"types": "dist/index.d.ts",
2928
"exports": {
3029
"./package.json": "./package.json",
3130
".": {
3231
"types": "./dist/index.d.ts",
33-
"import": "./dist/redux-toolkit.modern.js",
32+
"import": "./dist/redux-toolkit.modern.mjs",
3433
"default": "./dist/cjs/index.js"
3534
},
3635
"./query": {
3736
"types": "./dist/query/index.d.ts",
38-
"import": "./dist/query/rtk-query.modern.js",
37+
"import": "./dist/query/rtk-query.modern.mjs",
3938
"default": "./dist/query/cjs/index.js"
4039
},
4140
"./query/react": {
4241
"types": "./dist/query/react/index.d.ts",
43-
"import": "./dist/query/react/rtk-query-react.modern.js",
42+
"import": "./dist/query/react/rtk-query-react.modern.mjs",
4443
"default": "./dist/query/react/cjs/index.js"
4544
}
4645
},
@@ -109,10 +108,7 @@
109108
"prepack": "npm run build-prepare"
110109
},
111110
"files": [
112-
"dist/**/*.js",
113-
"dist/**/*.js.map",
114-
"dist/**/*.d.ts",
115-
"dist/**/package.json",
111+
"dist/",
116112
"src/",
117113
"query"
118114
],

packages/toolkit/query/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"version": "1.0.0",
44
"description": "",
55
"type": "module",
6-
"module": "../dist/query/rtk-query.modern.js",
6+
"module": "../dist/query/rtk-query.esm.mjs",
77
"main": "../dist/query/cjs/index.js",
88
"types": "./../dist/query/index.d.ts",
99
"exports": {
1010
"./package.json": "./package.json",
1111
".": {
1212
"types": "./../dist/query/index.d.ts",
13-
"import": "./../dist/query/rtk-query.modern.js",
13+
"import": "./../dist/query/rtk-query.modern.mjs",
1414
"default": "./../dist/query/cjs/index.js"
1515
}
1616
},

packages/toolkit/query/react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"version": "1.0.0",
44
"description": "",
55
"type": "module",
6-
"module": "../../dist/query/react/rtk-query-react.modern.js",
6+
"module": "../../dist/query/react/rtk-query-react.esm.mjs",
77
"main": "../../dist/query/react/cjs/index.js",
88
"types": "./../../dist/query/react/index.d.ts",
99
"exports": {
1010
"./package.json": "./package.json",
1111
".": {
1212
"types": "./../../dist/query/react/index.d.ts",
13-
"import": "./../../dist/query/react/rtk-query-react.modern.js",
13+
"import": "./../../dist/query/react/rtk-query-react.modern.mjs",
1414
"default": "./../../dist/query/react/cjs/index.js"
1515
}
1616
},

packages/toolkit/scripts/build.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ const outputDir = path.join(__dirname, '../dist')
3939
const buildTargets: BuildOptions[] = [
4040
{
4141
format: 'cjs',
42-
name: 'cjs.development',
42+
name: 'development',
4343
target: 'esnext',
4444
minify: false,
4545
env: 'development',
4646
},
4747
{
4848
format: 'cjs',
49-
name: 'cjs.production.min',
49+
name: 'production.min',
5050
target: 'esnext',
5151
minify: true,
5252
env: 'production',
@@ -59,6 +59,15 @@ const buildTargets: BuildOptions[] = [
5959
minify: false,
6060
env: '',
6161
},
62+
// ESM, embedded `process`: fallback for Webpack 4,
63+
// which doesn't support `exports` field or optional chaining
64+
{
65+
format: 'esm',
66+
name: 'esm',
67+
target: 'esnext',
68+
minify: false,
69+
env: '',
70+
},
6271
// ESM, pre-compiled "dev": browser development
6372
{
6473
format: 'esm',
@@ -145,8 +154,10 @@ async function bundle(options: BuildOptions & EntryPointOptions) {
145154
folderSegments.push('cjs')
146155
}
147156

157+
const extension = format === 'esm' ? 'mjs' : 'cjs'
158+
148159
const outputFolder = path.join(...folderSegments)
149-
const outputFilename = `${prefix}.${name}.js`
160+
const outputFilename = `${prefix}.${name}.${extension}`
150161

151162
await fs.ensureDir(outputFolder)
152163

@@ -306,9 +317,9 @@ async function writeCommonJSEntry(folder: string, prefix: string) {
306317
path.join(folder, 'index.js'),
307318
`'use strict'
308319
if (process.env.NODE_ENV === 'production') {
309-
module.exports = require('./${prefix}.cjs.production.min.js')
320+
module.exports = require('./${prefix}.production.min.cjs')
310321
} else {
311-
module.exports = require('./${prefix}.cjs.development.js')
322+
module.exports = require('./${prefix}.development.cjs')
312323
}`
313324
)
314325

packages/toolkit/scripts/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
export interface BuildOptions {
22
format: 'cjs' | 'umd' | 'esm'
33
name:
4-
| 'cjs.development'
5-
| 'cjs.production.min'
4+
| 'development'
5+
| 'production.min'
66
| 'esm'
77
| 'modern'
88
| 'modern.development'

0 commit comments

Comments
 (0)