@@ -9,18 +9,22 @@ import { log, parallel, sequential, createTimer } from '@/utils';
99 * Copy required files of module in there folder
1010 */
1111export async function copyRequiredFiles ( ) {
12- const { assets, srcPath, buildPath } = getConfig ( ) ;
13- if ( ! assets || ( Array . isArray ( assets ) && Boolean ( assets . length ) ) ) {
12+ const { assets, srcPath, buildPath, lib } = getConfig ( ) ;
13+ if ( ! assets || ( Array . isArray ( assets ) && assets . length === 0 ) ) {
1414 return ;
1515 }
1616
1717 if ( ! ( await fse . pathExists ( buildPath ) ) ) {
18- log . warn ( `[assets] path ${ buildPath } does not exists to copy` ) ;
18+ log . warn ( `build path ${ buildPath } does not exists to copy assets ` ) ;
1919 return [ ] ;
2020 }
2121
22- const hasCJS = await fse . pathExists ( `${ buildPath } /cjs` ) ;
23- const hasESM = await fse . pathExists ( `${ buildPath } /esm` ) ;
22+ // get custom paths from config or use defaults
23+ const cjsPath = lib ?. cjs ?. output ?. path ;
24+ const esmPath = lib ?. esm ?. output ?. path ;
25+
26+ const hasCJS = await fse . pathExists ( cjsPath ) ;
27+ const hasESM = await fse . pathExists ( esmPath ) ;
2428
2529 const files = await glob ( assets , {
2630 cwd : srcPath ,
@@ -32,29 +36,26 @@ export async function copyRequiredFiles() {
3236 files . forEach ( file => {
3337 if ( hasCJS ) {
3438 task . push (
35- fse . copy (
36- path . resolve ( srcPath , file ) ,
37- path . resolve ( `${ buildPath } /cjs` , file ) ,
38- ) ,
39+ fse . copy ( path . resolve ( srcPath , file ) , path . resolve ( cjsPath , file ) ) ,
3940 ) ;
4041 }
4142 if ( hasESM ) {
4243 task . push (
43- fse . copy (
44- path . resolve ( srcPath , file ) ,
45- path . resolve ( `${ buildPath } /esm` , file ) ,
46- ) ,
44+ fse . copy ( path . resolve ( srcPath , file ) , path . resolve ( esmPath , file ) ) ,
4745 ) ;
4846 }
49- task . push (
50- fse . copy ( path . resolve ( srcPath , file ) , path . resolve ( buildPath , file ) ) ,
51- ) ;
5247 } ) ;
5348 await Promise . all ( task ) ;
5449}
5550
5651async function postbuild ( getBuildTime : ReturnType < typeof createTimer > ) {
57- const { root, srcPath, buildPath } = getConfig ( ) ;
52+ const { root, srcPath, buildPath, lib } = getConfig ( ) ;
53+
54+ const cjsPath = lib ?. cjs ?. output ?. path ;
55+ const esmPath = lib ?. esm ?. output ?. path ;
56+
57+ const hasCJS = fse . pathExistsSync ( cjsPath ) ;
58+ const hasESM = fse . pathExistsSync ( esmPath ) ;
5859
5960 /**
6061 * Following function help to move project files like
@@ -73,9 +74,24 @@ async function postbuild(getBuildTime: ReturnType<typeof createTimer>) {
7374 }
7475
7576 /**
76- * used to create tree-shakeable package.json in each module of project
77+ * createModulePackages used to create tree-shakeable package.json in each module of project
78+ * in case of esm and cjs co exist and esm path is in root then we create module packages
79+ * ```js
80+ * import { someModule } from 'your-lib';
81+ * const { someModule } = require('your-lib');
82+ *
83+ * import someModule from 'your-lib/some-module';
84+ * const someModule = require('your-lib/some-module');
85+ * ```
7786 */
7887 async function createModulePackages ( ) {
88+ // in case esm and cjs co exist then we create module packages
89+ if ( ! ( hasESM && hasCJS ) ) return ;
90+
91+ // if esm path is not in root then we don't create module packages
92+ // in case or root it will be empty string which is falsy
93+ if ( path . relative ( buildPath , esmPath ) ) return ;
94+
7995 const packageData = JSON . parse (
8096 await fse . readFile ( path . resolve ( root , './package.json' ) , 'utf8' ) ,
8197 ) ;
@@ -92,11 +108,15 @@ async function postbuild(getBuildTime: ReturnType<typeof createTimer>) {
92108 'package.json' ,
93109 ) ;
94110
95- const packageJson : Record < string , any > = {
111+ const esmDir = path . join ( esmPath , directoryPackage ) ;
112+ const cjsDir = path . join ( cjsPath , directoryPackage ) ;
113+
114+ const packageJson = {
115+ name : `${ packageData . name } /${ directoryPackage } ` ,
96116 version : packageData . version ,
97117 sideEffects : false ,
98118 module : './index.js' ,
99- main : path . posix . join ( '../cjs' , directoryPackage , 'index.js' ) ,
119+ main : path . posix . join ( path . relative ( esmDir , cjsDir ) , 'index.js' ) ,
100120 types : './index.d.ts' ,
101121 } ;
102122
@@ -167,14 +187,17 @@ async function postbuild(getBuildTime: ReturnType<typeof createTimer>) {
167187 fse . readFileSync ( path . resolve ( root , './package.json' ) , 'utf8' ) ,
168188 ) ;
169189
190+ const cjsDir = path . relative ( buildPath , cjsPath ) ;
191+ const esmDir = path . relative ( buildPath , esmPath ) ;
192+
170193 const newPackageData = {
171194 ...restPackageData ,
172195 private : false ,
173- main : fse . existsSync ( path . resolve ( buildPath , './cjs/ index.js' ) )
174- ? './cjs /index.js'
196+ main : fse . existsSync ( path . join ( cjsPath , 'index.js' ) )
197+ ? `. ${ cjsDir ? `/ ${ cjsDir } ` : '' } /index.js`
175198 : './index.js' ,
176- module : fse . existsSync ( path . resolve ( buildPath , './esm/ index.js' ) )
177- ? './esm /index.js'
199+ module : fse . existsSync ( path . join ( esmPath , 'index.js' ) )
200+ ? `. ${ esmDir ? `/ ${ esmDir } ` : '' } /index.js`
178201 : './index.js' ,
179202 } ;
180203
@@ -190,13 +213,13 @@ async function postbuild(getBuildTime: ReturnType<typeof createTimer>) {
190213 delete newPackageData . main ;
191214 }
192215
193- const hasDefinitionsFile = fse . pathExistsSync (
194- path . resolve ( buildPath , './index.d.ts' ) ,
195- ) ;
196-
197- if ( hasDefinitionsFile ) {
198- newPackageData . types = './index.d.ts' ;
216+ // if esm path exists then by default types will be in esm path else in cjs path
217+ const dtsIndex = path . resolve ( hasESM ? esmPath : cjsPath , './index.d.ts' ) ;
218+ // if dts file exists then add it to package.json
219+ if ( fse . pathExistsSync ( dtsIndex ) ) {
220+ newPackageData . types = `./${ path . relative ( buildPath , dtsIndex ) } ` ;
199221 }
222+
200223 fse . writeFileSync (
201224 path . resolve ( buildPath , './package.json' ) ,
202225 JSON . stringify ( newPackageData , null , 2 ) ,
0 commit comments