@@ -4,19 +4,21 @@ import * as path from "path";
44import * as esbuild from "esbuild" ;
55import type { BuildOptions , Format , Loader , Plugin } from "esbuild" ;
66import { umdWrapper } from "esbuild-plugin-umd-wrapper" ;
7+ import * as copyStaticFiles from "esbuild-copy-static-files" ;
78import { inlineCSS } from "./inline-css.ts" ;
89import { rebuildLogger } from "./rebuild-logger.ts" ;
910
10- //@ts -ignore
11- import _copyStaticFiles from "esbuild-copy-static-files" ;
12- export const copyStaticFiles : Plugin = _copyStaticFiles ;
11+ export { copyStaticFiles } ;
1312
1413export const pkg = JSON . parse ( readFileSync ( path . join ( process . cwd ( ) , "./package.json" ) , "utf8" ) ) ;
1514export const NODE_MJS = pkg . type === "module" ? "js" : "mjs" ;
1615export const NODE_CJS = pkg . type === "module" ? "cjs" : "js" ;
1716
18- export async function buildWatch ( input : string , format : Format | "umd" = "esm" , external : string [ ] = [ ] , config : BuildOptions , isDevelopment : boolean = process . argv . includes ( "--development" ) , isWatch : boolean = process . argv . includes ( "--watch" ) ) : Promise < void > {
17+ export async function buildWatch ( inputs : string [ ] | Record < string , string > | { in : string , out : string } [ ] , config : BuildOptions ) : Promise < void > {
18+ const isDevelopment = process . argv . includes ( "--development" ) ;
19+ const isWatch = process . argv . includes ( "--watch" ) ;
1920 const isProduction = ! isDevelopment ;
21+
2022 if ( isProduction && existsSync ( path . join ( process . cwd ( ) , "../../package.json" ) ) ) {
2123 const rootPkg = JSON . parse ( readFileSync ( path . join ( process . cwd ( ) , "../../package.json" ) , "utf8" ) ) ;
2224 writeFileSync ( path . join ( process . cwd ( ) , "src/__package__.ts" ) , `\
@@ -26,20 +28,39 @@ export const BUILD_VERSION = "${rootPkg.version}";
2628` , "utf8" ) ;
2729 }
2830
29- const ctx = await esbuild . context ( {
30- entryPoints : [ input ] ,
31- format : format as Format ,
31+ config = {
32+ entryPoints : inputs ,
33+ format : "esm" ,
3234 bundle : true ,
3335 minify : isProduction ,
3436 sourcemap : true ,
35- external,
37+ external : [
38+ ...config . external ?? [ ]
39+ ] ,
3640 ...config ,
41+ loader : {
42+ ...config . loader
43+ } ,
44+ outExtension : {
45+ ...config . outExtension
46+ } ,
47+ banner : {
48+ ...config . banner
49+ } ,
50+ footer : {
51+ ...config . footer
52+ } ,
3753 plugins : [
3854 ...( isWatch ? [ rebuildLogger ( config ) ] : [ ] ) ,
39- ...( config . plugins ?? [ ] ) ,
55+ ...config . plugins ?? [ ] ,
4056 inlineCSS ( )
57+ ] ,
58+ nodePaths : [
59+ ...config . nodePaths ?? [ ]
4160 ]
42- } ) ;
61+ } ;
62+ console . log ( "config" , config ) ;
63+ const ctx = await esbuild . context ( config ) ;
4364
4465 if ( isWatch ) {
4566 await ctx . watch ( ) ;
@@ -68,35 +89,54 @@ export type TplOptions = {
6889 supported ?: Record < string , boolean > ;
6990 alias ?: Record < string , string > ;
7091 define ?: { [ key : string ] : string } ;
92+ packages ?: "bundle" | "external" | "auto" ;
7193} ;
7294
73- export function browserTpl ( input : string , output : string , { format = "esm" , globalName, libraryName, keepNames, external = [ ] , plugins = [ ] , alias = { } , define = { } , loader = { } } : TplOptions = { } ) {
74- return buildWatch ( input , format , external , {
75- outfile : `${ output } .${ format === "esm" ? "js" : `${ format } .js` } ` ,
95+ function autoExternal ( external ?: string [ ] ) : string [ ] {
96+ return [
97+ ...pkg . dependencies ? Object . keys ( pkg . dependencies ) : [ ] , ...pkg . peerDependencies ? Object . keys ( pkg . peerDependencies ) : [ ] ,
98+ ...external ?? [ ]
99+ ] ;
100+ }
101+
102+ export function browserTpl ( input : string , output : string , options : TplOptions = { } ) {
103+ options . format = options . format ?? "esm" ;
104+
105+ return buildWatch ( [ input ] , {
106+ format : options . format === "umd" ? "esm" : options . format ,
107+ external : options . external ?? [ ] ,
108+ outfile : `${ output } .${ options . format === "esm" ? "js" : `${ options . format } .js` } ` ,
76109 platform : "browser" ,
77110 target : "es2022" ,
78- globalName,
79- keepNames,
80- plugins : format === "umd" ? [ umdWrapper ( { libraryName } ) , ...plugins ] : [ ...plugins ] ,
81- alias,
82- define,
83- loader
84- } as BuildOptions ) ;
111+ globalName : options . globalName ,
112+ keepNames : options . keepNames ,
113+ plugins : options . format === "umd" ? [ umdWrapper ( { libraryName : options . libraryName } ) , ...options . plugins ?? [ ] ] : [ ...options . plugins ?? [ ] ] ,
114+ alias : options . alias ,
115+ define : options . define ,
116+ loader : options . loader ,
117+ } ) ;
85118}
86119
87- export function nodeTpl ( input : string , output : string , { format = "esm" , external = [ ] , supported = { } } : TplOptions = { } ) {
88- return buildWatch ( input , format , external , {
89- outfile : `${ output } .${ format === "esm" ? NODE_MJS : NODE_CJS } ` ,
120+ export function nodeTpl ( input : string , output : string , options : TplOptions = { } ) {
121+ options . packages = options . packages ?? "external" ;
122+ if ( options . packages === "auto" ) {
123+ options . external = autoExternal ( options . external ) ;
124+ }
125+
126+ return buildWatch ( [ input ] , {
127+ format : options . format === "umd" ? "esm" : options . format ,
128+ outfile : `${ output } .${ options . format === "esm" ? NODE_MJS : NODE_CJS } ` ,
90129 platform : "node" ,
91- target : "node20" ,
92- packages : "external" ,
93- supported
130+ target : "node22" ,
131+ packages : options . packages === "auto" ? "bundle" : options . packages ,
94132 } ) ;
95133}
96134
97- export function neutralTpl ( input : string , output : string , { format = "esm" , globalName, libraryName, keepNames, external = [ ] } : TplOptions = { } ) {
135+ export function neutralTpl ( input : string , output : string , options : TplOptions = { } ) {
136+ options . format = options . format ?? "esm" ;
137+
98138 let postfix = "" ;
99- switch ( format ) {
139+ switch ( options . format ) {
100140 case "iife" :
101141 postfix = "iife.js" ;
102142 break ;
@@ -110,35 +150,36 @@ export function neutralTpl(input: string, output: string, { format = "esm", glob
110150 postfix = "umd.js" ;
111151 break ;
112152 default :
113- throw new Error ( `Unknown format: ${ format } ` ) ;
153+ throw new Error ( `Unknown format: ${ options . format } ` ) ;
114154 }
115- return buildWatch ( input , format , external , {
116- outfile : `${ output } .${ format === "esm" ? "js" : `${ format } .js` } ` ,
155+ return buildWatch ( [ input ] , {
156+ format : options . format === "umd" ? "esm" : options . format ,
157+ outfile : `${ output } .${ options . format === "esm" ? "js" : `${ options . format } .js` } ` ,
117158 platform : "neutral" ,
118159 target : "es2022" ,
119- globalName,
120- keepNames,
121- plugins : format === "umd" ? [ umdWrapper ( { libraryName } ) ] : [ ] as Plugin [ ]
122- } as BuildOptions ) ;
160+ globalName : options . globalName ,
161+ keepNames : options . keepNames ,
162+ plugins : options . format === "umd" ? [ umdWrapper ( { libraryName : options . libraryName } ) ] : [ ] as Plugin [ ]
163+ } ) ;
123164}
124165
125- export function browserBoth ( input : string , output : string , globalName ?: string , libraryName ?: string , external : string [ ] = [ ] ) {
166+ export function browserBoth ( input : string , output : string , options : TplOptions = { } ) {
126167 return Promise . all ( [
127- browserTpl ( input , output , { format : "esm" , globalName , libraryName , external } ) ,
128- browserTpl ( input , output , { format : "umd" , globalName , libraryName , external } )
168+ browserTpl ( input , output , { format : "esm" , ... options } ) ,
169+ browserTpl ( input , output , { format : "umd" , ... options } )
129170 ] ) ;
130171}
131172
132- export function nodeBoth ( input : string , output : string , external : string [ ] = [ ] ) {
173+ export function nodeBoth ( input : string , output : string , options : TplOptions = { } ) {
133174 return Promise . all ( [
134- nodeTpl ( input , output , { format : "esm" , external } ) ,
135- nodeTpl ( input , output , { format : "cjs" , external } )
175+ nodeTpl ( input , output , { format : "esm" , ... options } ) ,
176+ nodeTpl ( input , output , { format : "cjs" , ... options } )
136177 ] ) ;
137178}
138179
139- export function bothTpl ( input : string , output : string , globalName ?: string , libraryName ?: string , external : string [ ] = [ ] ) {
180+ export function bothTpl ( input : string , output : string , options : TplOptions = { } ) {
140181 return Promise . all ( [
141- browserBoth ( input , output , globalName , libraryName , external ) ,
142- nodeTpl ( input , output , { format : "cjs" , external } )
182+ browserBoth ( input , output , { ... options } ) ,
183+ nodeTpl ( input , output , { format : "cjs" , ... options } )
143184 ] ) ;
144185}
0 commit comments