11const webpack = require ( 'webpack' )
22let { join } = require ( 'path' )
33
4- const suffixes = [ 'cjs.production.min.js' , 'esm.js' ]
4+ const esmSuffixes = [ 'modern.mjs' , 'browser.mjs' , 'legacy-esm.js' ]
5+ const cjsSuffixes = [ 'development.cjs' , 'production.min.cjs' ]
56
6- function withRtkPath ( suffix ) {
7+ function withRtkPath ( suffix , cjs = false ) {
8+ /**
9+ * @param {webpack.Configuration } config
10+ */
711 return ( config ) => {
812 config . plugins . push (
913 new webpack . NormalModuleReplacementPlugin (
1014 / @ r e d u x j s \/ t o o l k i t \/ q u e r y \/ r e a c t / ,
11- join ( __dirname , ` query/react` ) ,
15+ require . resolve ( './ query/react' ) ,
1216 ) ,
1317 new webpack . NormalModuleReplacementPlugin (
1418 / @ r e d u x j s \/ t o o l k i t \/ q u e r y / ,
15- join ( __dirname , `query` ) ,
19+ require . resolve ( './query' ) ,
20+ ) ,
21+ new webpack . NormalModuleReplacementPlugin (
22+ / @ r e d u x j s \/ t o o l k i t \/ r e a c t / ,
23+ require . resolve ( './react' ) ,
1624 ) ,
1725 new webpack . NormalModuleReplacementPlugin (
1826 / @ r e d u x j s \/ t o o l k i t / ,
19- join ( __dirname ) ,
27+ require . resolve ( '.' ) ,
2028 ) ,
2129 new webpack . NormalModuleReplacementPlugin (
2230 / r t k - q u e r y - r e a c t .m o d e r n .j s / ,
2331 ( r ) => {
2432 const old = r . request
2533 r . request = r . request . replace (
2634 / r t k - q u e r y - r e a c t .m o d e r n .j s $ / ,
27- `rtk-query-react.${ suffix } ` ,
35+ `${ cjs ? 'cjs/' : '' } rtk-query-react.${ suffix } ` ,
2836 )
2937 // console.log(old, '=>', r.request)
3038 } ,
@@ -33,26 +41,37 @@ function withRtkPath(suffix) {
3341 const old = r . request
3442 r . request = r . request . replace (
3543 / r t k - q u e r y .m o d e r n .j s $ / ,
36- `rtk-query.${ suffix } ` ,
44+ `${ cjs ? 'cjs/' : '' } rtk-query.${ suffix } ` ,
3745 )
3846 // console.log(old, '=>', r.request)
3947 } ) ,
48+ new webpack . NormalModuleReplacementPlugin (
49+ / r e d u x - t o o l k i t - r e a c t .m o d e r n .j s $ / ,
50+ ( r ) => {
51+ const old = r . request
52+ r . request = r . request . replace (
53+ / r e d u x - t o o l k i t - r e a c t .m o d e r n .j s $ / ,
54+ `${ cjs ? 'cjs/' : '' } redux-toolkit-react.${ suffix } ` ,
55+ )
56+ // console.log(old, '=>', r.request)
57+ } ,
58+ ) ,
4059 new webpack . NormalModuleReplacementPlugin (
4160 / r e d u x - t o o l k i t .m o d e r n .j s $ / ,
4261 ( r ) => {
4362 const old = r . request
4463 r . request = r . request . replace (
4564 / r e d u x - t o o l k i t .m o d e r n .j s $ / ,
46- `redux-toolkit.${ suffix } ` ,
65+ `${ cjs ? 'cjs/' : '' } redux-toolkit.${ suffix } ` ,
4766 )
4867 // console.log(old, '=>', r.request)
4968 } ,
5069 ) ,
5170 )
52- if ( suffix === 'cjs. production.min.js ' ) {
53- config . resolve . mainFields = [ 'main' , 'module' ]
71+ if ( suffix === 'production.min.cjs ' ) {
72+ ; ( config . resolve ??= { } ) . mainFields = [ 'main' , 'module' ]
5473 }
55- config . optimization . nodeEnv = 'production'
74+ ; ( config . optimization ??= { } ) . nodeEnv = 'production'
5675 return config
5776 }
5877}
@@ -66,42 +85,62 @@ const ignoreAll = [
6685 'redux-thunk' ,
6786]
6887
69- module . exports = [
88+ const entryPoints = [
7089 {
7190 name : `1. entry point: @reduxjs/toolkit` ,
72- path : 'dist/redux-toolkit.modern.js' ,
91+ path : 'dist/redux-toolkit.modern.mjs' ,
92+ } ,
93+ {
94+ name : `1. entry point: @reduxjs/toolkit/react` ,
95+ path : 'dist/react/redux-toolkit-react.modern.mjs' ,
7396 } ,
7497 {
7598 name : `1. entry point: @reduxjs/toolkit/query` ,
76- path : 'dist/query/rtk-query.modern.js ' ,
99+ path : 'dist/query/rtk-query.modern.mjs ' ,
77100 } ,
78101 {
79102 name : `1. entry point: @reduxjs/toolkit/query/react` ,
80- path : 'dist/query/react/rtk-query-react.modern.js ' ,
103+ path : 'dist/query/react/rtk-query-react.modern.mjs ' ,
81104 } ,
82105 {
83106 name : `2. entry point: @reduxjs/toolkit (without dependencies)` ,
84- path : 'dist/redux-toolkit.modern.js' ,
107+ path : 'dist/redux-toolkit.modern.mjs' ,
108+ ignore : ignoreAll ,
109+ } ,
110+ {
111+ name : `2. entry point: @reduxjs/toolkit/react (without dependencies)` ,
112+ path : 'dist/react/redux-toolkit-react.modern.mjs' ,
85113 ignore : ignoreAll ,
86114 } ,
87115 {
88116 name : `2. entry point: @reduxjs/toolkit/query (without dependencies)` ,
89- path : 'dist/query/rtk-query.modern.js ' ,
117+ path : 'dist/query/rtk-query.modern.mjs ' ,
90118 ignore : ignoreAll ,
91119 } ,
92120 {
93121 name : `2. entry point: @reduxjs/toolkit/query/react (without dependencies)` ,
94- path : 'dist/query/react/rtk-query-react.modern.js ' ,
122+ path : 'dist/query/react/rtk-query-react.modern.mjs ' ,
95123 ignore : ignoreAll ,
96124 } ,
97125]
126+
127+ module . exports = entryPoints
98128 . flatMap ( ( e ) =>
99- suffixes . map ( ( suffix ) => ( {
129+ esmSuffixes . map ( ( suffix ) => ( {
100130 ...e ,
101131 name : e . name + ` (${ suffix } )` ,
102132 modifyWebpackConfig : withRtkPath ( suffix ) ,
103133 } ) ) ,
104134 )
135+ . concat (
136+ entryPoints . flatMap ( ( e ) =>
137+ cjsSuffixes . map ( ( suffix ) => ( {
138+ ...e ,
139+ name : e . name + ` (cjs, ${ suffix } )` ,
140+ modifyWebpackConfig : withRtkPath ( suffix , true ) ,
141+ } ) ) ,
142+ ) ,
143+ )
105144 . concat (
106145 ...[
107146 {
@@ -138,7 +177,7 @@ module.exports = [
138177 } ,
139178 ] . map ( ( e ) => ( {
140179 ...e ,
141- name : e . name + ` (esm.js )` ,
142- modifyWebpackConfig : withRtkPath ( 'esm.js ' ) ,
180+ name : e . name + ` (.modern.mjs )` ,
181+ modifyWebpackConfig : withRtkPath ( '.modern.mjs ' ) ,
143182 } ) ) ,
144183 )
0 commit comments