1
1
const webpack = require ( 'webpack' )
2
2
let { join } = require ( 'path' )
3
3
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' ]
5
6
6
- function withRtkPath ( suffix ) {
7
+ function withRtkPath ( suffix , cjs = false ) {
8
+ /**
9
+ * @param {webpack.Configuration } config
10
+ */
7
11
return ( config ) => {
8
12
config . plugins . push (
9
13
new webpack . NormalModuleReplacementPlugin (
@@ -14,6 +18,10 @@ function withRtkPath(suffix) {
14
18
/ @ r e d u x j s \/ t o o l k i t \/ q u e r y / ,
15
19
join ( __dirname , `query` )
16
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
+ join ( __dirname , 'react' )
24
+ ) ,
17
25
new webpack . NormalModuleReplacementPlugin (
18
26
/ @ r e d u x j s \/ t o o l k i t / ,
19
27
join ( __dirname )
@@ -24,7 +32,7 @@ function withRtkPath(suffix) {
24
32
const old = r . request
25
33
r . request = r . request . replace (
26
34
/ 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 } `
28
36
)
29
37
// console.log(old, '=>', r.request)
30
38
}
@@ -33,26 +41,37 @@ function withRtkPath(suffix) {
33
41
const old = r . request
34
42
r . request = r . request . replace (
35
43
/ 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 } `
37
45
)
38
46
// console.log(old, '=>', r.request)
39
47
} ) ,
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
+ ) ,
40
59
new webpack . NormalModuleReplacementPlugin (
41
60
/ r e d u x - t o o l k i t .m o d e r n .j s $ / ,
42
61
( r ) => {
43
62
const old = r . request
44
63
r . request = r . request . replace (
45
64
/ 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 } `
47
66
)
48
67
// console.log(old, '=>', r.request)
49
68
}
50
69
)
51
70
)
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' ]
54
73
}
55
- config . optimization . nodeEnv = 'production'
74
+ ; ( config . optimization ??= { } ) . nodeEnv = 'production'
56
75
return config
57
76
}
58
77
}
@@ -66,42 +85,62 @@ const ignoreAll = [
66
85
'redux-thunk' ,
67
86
]
68
87
69
- module . exports = [
88
+ const entryPoints = [
70
89
{
71
90
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' ,
73
96
} ,
74
97
{
75
98
name : `1. entry point: @reduxjs/toolkit/query` ,
76
- path : 'dist/query/rtk-query.modern.js ' ,
99
+ path : 'dist/query/rtk-query.modern.mjs ' ,
77
100
} ,
78
101
{
79
102
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 ' ,
81
104
} ,
82
105
{
83
106
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' ,
85
113
ignore : ignoreAll ,
86
114
} ,
87
115
{
88
116
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 ' ,
90
118
ignore : ignoreAll ,
91
119
} ,
92
120
{
93
121
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 ' ,
95
123
ignore : ignoreAll ,
96
124
} ,
97
125
]
126
+
127
+ module . exports = entryPoints
98
128
. flatMap ( ( e ) =>
99
- suffixes . map ( ( suffix ) => ( {
129
+ esmSuffixes . map ( ( suffix ) => ( {
100
130
...e ,
101
131
name : e . name + ` (${ suffix } )` ,
102
132
modifyWebpackConfig : withRtkPath ( suffix ) ,
103
133
} ) )
104
134
)
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
+ )
105
144
. concat (
106
145
...[
107
146
{
@@ -138,7 +177,7 @@ module.exports = [
138
177
} ,
139
178
] . map ( ( e ) => ( {
140
179
...e ,
141
- name : e . name + ` (esm.js )` ,
142
- modifyWebpackConfig : withRtkPath ( 'esm.js ' ) ,
180
+ name : e . name + ` (.modern.mjs )` ,
181
+ modifyWebpackConfig : withRtkPath ( '.modern.mjs ' ) ,
143
182
} ) )
144
183
)
0 commit comments