@@ -6,18 +6,20 @@ import glob from 'glob';
6
6
import type { Input } from '../types' ;
7
7
8
8
type Options = Input & {
9
+ esm ?: boolean ;
9
10
babelrc ?: boolean | null ;
10
11
configFile ?: string | false | null ;
11
12
sourceMaps ?: boolean ;
12
13
copyFlow ?: boolean ;
13
- modules : 'commonjs' | false ;
14
+ modules : 'commonjs' | 'preserve' ;
14
15
exclude : string ;
15
16
} ;
16
17
17
18
export default async function compile ( {
18
19
root,
19
20
source,
20
21
output,
22
+ esm = false ,
21
23
babelrc = false ,
22
24
configFile = false ,
23
25
exclude,
@@ -63,7 +65,11 @@ export default async function compile({
63
65
}
64
66
}
65
67
66
- const outputExtension = modules === 'commonjs' ? '.cjs' : '.mjs' ;
68
+ const outputExtension = esm
69
+ ? modules === 'commonjs'
70
+ ? '.cjs'
71
+ : '.mjs'
72
+ : '.js' ;
67
73
68
74
await Promise . all (
69
75
files . map ( async ( filepath ) => {
@@ -91,7 +97,9 @@ export default async function compile({
91
97
...( babelrc || configFile
92
98
? null
93
99
: {
94
- presets : [ [ require . resolve ( '../../babel-preset' ) , { modules } ] ] ,
100
+ presets : [
101
+ [ require . resolve ( '../../babel-preset' ) , { modules, esm } ] ,
102
+ ] ,
95
103
} ) ,
96
104
} ) ;
97
105
@@ -144,14 +152,40 @@ export default async function compile({
144
152
145
153
const fields =
146
154
modules === 'commonjs'
147
- ? [
148
- { name : 'main' , value : pkg . main } ,
149
- { name : "exports['.'].require" , value : pkg . exports ?. [ '.' ] ?. require } ,
150
- ]
151
- : [
152
- { name : 'module' , value : pkg . module } ,
153
- { name : "exports['.'].import" , value : pkg . exports ?. [ '.' ] ?. import } ,
154
- ] ;
155
+ ? [ { name : 'main' , value : pkg . main } ]
156
+ : [ { name : 'module' , value : pkg . module } ] ;
157
+
158
+ if ( esm ) {
159
+ if ( modules === 'commonjs' ) {
160
+ fields . push ( {
161
+ name : "exports['.'].require" ,
162
+ value : pkg . exports ?. [ '.' ] ?. require ,
163
+ } ) ;
164
+ } else {
165
+ fields . push ( {
166
+ name : "exports['.'].import" ,
167
+ value : pkg . exports ?. [ '.' ] ?. import ,
168
+ } ) ;
169
+ }
170
+ } else {
171
+ if ( modules === 'commonjs' && pkg . exports ?. [ '.' ] ?. require ) {
172
+ report . warn (
173
+ `The ${ kleur . blue ( 'esm' ) } option is disabled, but the ${ kleur . blue (
174
+ "exports['.'].require"
175
+ ) } field is set in ${ kleur . blue (
176
+ 'package.json'
177
+ ) } . This is likely a mistake.`
178
+ ) ;
179
+ } else if ( modules === 'preserve' && pkg . exports ?. [ '.' ] ?. import ) {
180
+ report . warn (
181
+ `The ${ kleur . blue ( 'esm' ) } option is disabled, but the ${ kleur . blue (
182
+ "exports['.'].import"
183
+ ) } field is set in ${ kleur . blue (
184
+ 'package.json'
185
+ ) } . This is likely a mistake.`
186
+ ) ;
187
+ }
188
+ }
155
189
156
190
if ( fields . some ( ( field ) => field . value ) ) {
157
191
await Promise . all (
0 commit comments