@@ -207,60 +207,49 @@ async function build({
207
207
return ;
208
208
}
209
209
210
+ validatePackageJson ( pkg , {
211
+ includesCommonJS : config ?. commonjs ?? true ,
212
+ } ) ;
213
+
210
214
const declarations = await globby ( '**/*.d.ts' , {
211
215
cwd : getBuildPath ( 'esm' ) ,
212
216
absolute : false ,
213
217
ignore : filesToExcludeFromDist ,
214
218
} ) ;
215
219
220
+ await fse . ensureDir ( join ( distPath , 'typings' ) ) ;
221
+ await Promise . all (
222
+ declarations . map ( filePath =>
223
+ limit ( ( ) =>
224
+ fse . copy ( join ( getBuildPath ( 'esm' ) , filePath ) , join ( distPath , 'typings' , filePath ) ) ,
225
+ ) ,
226
+ ) ,
227
+ ) ;
228
+
216
229
const esmFiles = await globby ( '**/*.js' , {
217
230
cwd : getBuildPath ( 'esm' ) ,
218
231
absolute : false ,
219
232
ignore : filesToExcludeFromDist ,
220
233
} ) ;
221
234
222
- // Check whether al esm files are empty, if not - probably a types only build
223
- let emptyEsmFiles = true ;
235
+ // all files that export nothing, should be completely empty
236
+ // this way we wont have issues with linters: <link to issue>
237
+ // and we will also make all type-only packages happy
224
238
for ( const file of esmFiles ) {
225
239
const src = await fse . readFile ( join ( getBuildPath ( 'esm' ) , file ) ) ;
226
- if ( src . toString ( ) . trim ( ) !== 'export {};' ) {
227
- emptyEsmFiles = false ;
228
- break ;
240
+ if ( src . toString ( ) . trim ( ) === 'export {};' ) {
241
+ await fse . writeFile ( join ( getBuildPath ( 'esm' ) , file ) , '' ) ;
229
242
}
230
243
}
231
244
232
- // Empty ESM files with existing declarations is a types-only package
233
- const typesOnly = emptyEsmFiles && declarations . length > 0 ;
234
-
235
- validatePackageJson ( pkg , {
236
- typesOnly,
237
- includesCommonJS : config ?. commonjs ?? true ,
238
- } ) ;
239
-
240
- // remove <project>/dist
241
- await fse . remove ( distPath ) ;
242
-
243
- // Copy type definitions
244
- await fse . ensureDir ( join ( distPath , 'typings' ) ) ;
245
+ await fse . ensureDir ( join ( distPath , 'esm' ) ) ;
245
246
await Promise . all (
246
- declarations . map ( filePath =>
247
- limit ( ( ) =>
248
- fse . copy ( join ( getBuildPath ( 'esm' ) , filePath ) , join ( distPath , 'typings' , filePath ) ) ,
249
- ) ,
247
+ esmFiles . map ( filePath =>
248
+ limit ( ( ) => fse . copy ( join ( getBuildPath ( 'esm' ) , filePath ) , join ( distPath , 'esm' , filePath ) ) ) ,
250
249
) ,
251
250
) ;
252
251
253
- // If ESM files are not empty, copy them to dist/esm
254
- if ( ! emptyEsmFiles ) {
255
- await fse . ensureDir ( join ( distPath , 'esm' ) ) ;
256
- await Promise . all (
257
- esmFiles . map ( filePath =>
258
- limit ( ( ) => fse . copy ( join ( getBuildPath ( 'esm' ) , filePath ) , join ( distPath , 'esm' , filePath ) ) ) ,
259
- ) ,
260
- ) ;
261
- }
262
-
263
- if ( ! emptyEsmFiles && config ?. commonjs === undefined ) {
252
+ if ( config ?. commonjs === undefined ) {
264
253
// Transpile ESM to CJS and move CJS to dist/cjs only if there's something to transpile
265
254
await fse . ensureDir ( join ( distPath , 'cjs' ) ) ;
266
255
@@ -270,6 +259,20 @@ async function build({
270
259
ignore : filesToExcludeFromDist ,
271
260
} ) ;
272
261
262
+ // all files that export nothing, should be completely empty
263
+ // this way we wont have issues with linters: <link to issue>
264
+ // and we will also make all type-only packages happy
265
+ for ( const file of cjsFiles ) {
266
+ const src = await fse . readFile ( join ( getBuildPath ( 'cjs' ) , file ) ) ;
267
+ if (
268
+ // TODO: will this always be the case with empty cjs files
269
+ src . toString ( ) . trim ( ) ===
270
+ '"use strict";\nObject.defineProperty(exports, "__esModule", { value: true });'
271
+ ) {
272
+ await fse . writeFile ( join ( getBuildPath ( 'cjs' ) , file ) , '' ) ;
273
+ }
274
+ }
275
+
273
276
await Promise . all (
274
277
cjsFiles . map ( filePath =>
275
278
limit ( ( ) => fse . copy ( join ( getBuildPath ( 'cjs' ) , filePath ) , join ( distPath , 'cjs' , filePath ) ) ) ,
@@ -305,7 +308,7 @@ async function build({
305
308
// move the package.json to dist
306
309
await fse . writeFile (
307
310
join ( distPath , 'package.json' ) ,
308
- JSON . stringify ( rewritePackageJson ( pkg , typesOnly ) , null , 2 ) ,
311
+ JSON . stringify ( rewritePackageJson ( pkg ) , null , 2 ) ,
309
312
) ;
310
313
311
314
// move README.md and LICENSE and other specified files
@@ -327,7 +330,7 @@ async function build({
327
330
reporter . success ( `Built ${ pkg . name } ` ) ;
328
331
}
329
332
330
- function rewritePackageJson ( pkg : Record < string , any > , typesOnly : boolean ) {
333
+ function rewritePackageJson ( pkg : Record < string , any > ) {
331
334
const newPkg : Record < string , any > = { } ;
332
335
const fields = [
333
336
'name' ,
@@ -360,14 +363,8 @@ function rewritePackageJson(pkg: Record<string, any>, typesOnly: boolean) {
360
363
361
364
const distDirStr = `${ DIST_DIR } /` ;
362
365
363
- if ( typesOnly ) {
364
- newPkg . main = '' ;
365
- delete newPkg . module ;
366
- delete newPkg . type ;
367
- } else {
368
- newPkg . main = newPkg . main . replace ( distDirStr , '' ) ;
369
- newPkg . module = newPkg . module . replace ( distDirStr , '' ) ;
370
- }
366
+ newPkg . main = newPkg . main . replace ( distDirStr , '' ) ;
367
+ newPkg . module = newPkg . module . replace ( distDirStr , '' ) ;
371
368
newPkg . typings = newPkg . typings . replace ( distDirStr , '' ) ;
372
369
newPkg . typescript = {
373
370
definition : newPkg . typescript . definition . replace ( distDirStr , '' ) ,
@@ -376,7 +373,7 @@ function rewritePackageJson(pkg: Record<string, any>, typesOnly: boolean) {
376
373
if ( ! pkg . exports ) {
377
374
newPkg . exports = presetFields . exports ;
378
375
}
379
- newPkg . exports = rewriteExports ( pkg . exports , DIST_DIR , typesOnly ) ;
376
+ newPkg . exports = rewriteExports ( pkg . exports , DIST_DIR ) ;
380
377
381
378
if ( pkg . bin ) {
382
379
newPkg . bin = { } ;
@@ -392,7 +389,6 @@ function rewritePackageJson(pkg: Record<string, any>, typesOnly: boolean) {
392
389
export function validatePackageJson (
393
390
pkg : any ,
394
391
opts : {
395
- typesOnly : boolean ;
396
392
includesCommonJS : boolean ;
397
393
} ,
398
394
) {
@@ -407,19 +403,6 @@ export function validatePackageJson(
407
403
) ;
408
404
}
409
405
410
- // Type only packages have simpler rules (following the style of https://github.com/DefinitelyTyped/DefinitelyTyped packages)
411
- if ( opts . typesOnly ) {
412
- expect ( 'main' , '' ) ;
413
- expect ( 'module' , undefined ) ;
414
- expect ( 'typings' , presetFields . typings ) ;
415
- expect ( 'typescript.definition' , presetFields . typescript . definition ) ;
416
- expect ( "exports['.'].default" , {
417
- // only the "types" field is required
418
- types : presetFields . exports [ '.' ] . default . types ,
419
- } ) ;
420
- return ;
421
- }
422
-
423
406
// If the package has NO binary we need to check the exports map.
424
407
// a package should either
425
408
// 1. have a bin property
0 commit comments