1
+ // @ts -check
2
+ import fs from "node:fs/promises" ;
3
+ import path from "node:path" ;
4
+ import { fileURLToPath } from "node:url" ;
5
+
1
6
import chalk from "chalk" ;
2
7
import { analyzeMetafile , build } from "esbuild" ;
3
- import { readFile } from "fs/promises" ;
4
- import path from "path" ;
5
- import { fileURLToPath } from "url" ;
6
-
7
- const nodeTargets = [ "node14" , "node16" , "node18" ] ;
8
8
9
9
const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
10
- const packageJsonPath = path . resolve ( __dirname , ".." , "package.json" ) ;
11
10
12
- async function getExternals ( ) {
13
- const packageJson = JSON . parse ( await readFile ( packageJsonPath ) ) ;
11
+ /**
12
+ * Available formats: https://esbuild.github.io/api/#format
13
+ *
14
+ * @type {Array<"cjs" | "esm"> }
15
+ */
16
+ const FORMATS = [ "cjs" , "esm" ] ;
17
+ /**
18
+ * @type {import('esbuild').BuildOptions['target'] }
19
+ */
20
+ const TARGETS = [ "node14" , "node16" , "node18" , "node20" , "esnext" ] ;
21
+
22
+ const ROOT = path . resolve ( __dirname , ".." ) ;
23
+ const SOURCES_ROOT = path . resolve ( ROOT , "./lib" ) ;
24
+
25
+ /**
26
+ * @param {"cjs" | "esm" } format
27
+ * @return {import('esbuild').Plugin }
28
+ */
29
+ function addExtension ( format ) {
30
+ const newExtension = format === "cjs" ? ".cjs" : ".mjs" ;
31
+ return {
32
+ name : "add-extension" ,
33
+ setup ( build ) {
34
+ build . onResolve ( { filter : / .* / } , ( args ) => {
35
+ if ( args . importer )
36
+ return {
37
+ path : args . path . replace ( / \. j s $ / , newExtension ) ,
38
+ external : true ,
39
+ } ;
40
+ } ) ;
41
+ } ,
42
+ } ;
43
+ }
44
+
45
+ /**
46
+ * @param {string[] } entryPoints
47
+ * @param {"cjs" | "esm" } format
48
+ */
49
+ async function buildSources ( entryPoints , format ) {
50
+ return build ( {
51
+ entryPoints : entryPoints ,
52
+ outdir : `${ ROOT } /dist/${ format } ` ,
53
+ target : TARGETS ,
54
+ platform : "node" ,
55
+ format : format ,
56
+ metafile : true ,
14
57
15
- return Object . keys ( packageJson . peerDependencies ) ;
58
+ // These allow us to build compliant exports and imports based on modern node
59
+ bundle : true ,
60
+ outExtension : { ".js" : format === "cjs" ? ".cjs" : ".mjs" } ,
61
+ plugins : [ addExtension ( format ) ] ,
62
+ } ) ;
63
+ }
64
+
65
+ async function getSourceEntryPoints ( ) {
66
+ const entryPoints = await fs . readdir ( SOURCES_ROOT ) ;
67
+
68
+ return entryPoints
69
+ . filter ( ( file ) => ! / _ _ f i x t u r e s _ _ | \. s p e c \. / . test ( file ) )
70
+ . map ( ( file ) => path . resolve ( SOURCES_ROOT , file ) ) ;
16
71
}
17
72
18
73
( async ( ) => {
@@ -24,27 +79,34 @@ async function getExternals() {
24
79
)
25
80
) ;
26
81
27
- const externalDeps = await getExternals ( ) ;
82
+ console . info ( "- Generate sources" ) ;
28
83
29
- const result = await build ( {
30
- entryPoints : [ "./lib/index.ts" ] ,
31
- outfile : "dist/cjs/index.js" ,
32
- metafile : true ,
33
- bundle : true ,
34
- format : "cjs" ,
35
- external : externalDeps ,
36
- platform : "node" ,
37
- target : nodeTargets ,
38
- treeShaking : true ,
39
- } ) ;
84
+ const sourceEntryPoints = await getSourceEntryPoints ( ) ;
40
85
41
- const analysis = await analyzeMetafile ( result . metafile ) ;
42
- console . info ( `📝 Bundle Analysis:${ analysis } ` ) ;
86
+ for ( const format of FORMATS ) {
87
+ console . info ( `- Generating ${ chalk . bold . greenBright ( format ) } sources` ) ;
88
+
89
+ const result = await buildSources ( sourceEntryPoints , format ) ;
90
+ const analysis = await analyzeMetafile ( result . metafile ) ;
91
+ console . info (
92
+ `${ analysis
93
+ . trim ( )
94
+ . split ( / \n \r / )
95
+ . map ( ( line ) => ` ${ line } ` )
96
+ . join ( ) } `
97
+ ) ;
98
+
99
+ console . info (
100
+ `${ chalk . bold . greenBright ( "✔" ) } Generating ${ chalk . bold . greenBright (
101
+ format
102
+ ) } sources completed!\n`
103
+ ) ;
104
+ }
43
105
44
106
console . info (
45
- ` ${ chalk . bold . green ( "✔ Bundled successfully!" ) } ( ${
46
- Date . now ( ) - startTime
47
- } ms)`
107
+ chalk . bold . green (
108
+ `✔ Generate sources completed! ( ${ Date . now ( ) - startTime } ms)`
109
+ )
48
110
) ;
49
111
} catch ( error ) {
50
112
console . error ( `🧨 ${ chalk . red . bold ( "Failed:" ) } ${ error . message } ` ) ;
0 commit comments