@@ -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,38 @@ 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+ const ctx = await esbuild . context ( config ) ;
4363
4464 if ( isWatch ) {
4565 await ctx . watch ( ) ;
@@ -68,35 +88,54 @@ export type TplOptions = {
6888 supported ?: Record < string , boolean > ;
6989 alias ?: Record < string , string > ;
7090 define ?: { [ key : string ] : string } ;
91+ packages ?: "bundle" | "external" | "auto" ;
7192} ;
7293
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` } ` ,
94+ function autoExternal ( external ?: string [ ] ) : string [ ] {
95+ return [
96+ ...pkg . dependencies ? Object . keys ( pkg . dependencies ) : [ ] , ...pkg . peerDependencies ? Object . keys ( pkg . peerDependencies ) : [ ] ,
97+ ...external ?? [ ]
98+ ] ;
99+ }
100+
101+ export function browserTpl ( input : string , output : string , options : TplOptions = { } ) {
102+ options . format = options . format ?? "esm" ;
103+
104+ return buildWatch ( [ input ] , {
105+ format : options . format === "umd" ? "esm" : options . format ,
106+ external : options . external ?? [ ] ,
107+ outfile : `${ output } .${ options . format === "esm" ? "js" : `${ options . format } .js` } ` ,
76108 platform : "browser" ,
77109 target : "es2022" ,
78- globalName,
79- keepNames,
80- plugins : format === "umd" ? [ umdWrapper ( { libraryName } ) , ...plugins ] : [ ...plugins ] ,
81- alias,
82- define,
83- loader
84- } as BuildOptions ) ;
110+ globalName : options . globalName ,
111+ keepNames : options . keepNames ,
112+ plugins : options . format === "umd" ? [ umdWrapper ( { libraryName : options . libraryName } ) , ...options . plugins ?? [ ] ] : [ ...options . plugins ?? [ ] ] ,
113+ alias : options . alias ,
114+ define : options . define ,
115+ loader : options . loader ,
116+ } ) ;
85117}
86118
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 } ` ,
119+ export function nodeTpl ( input : string , output : string , options : TplOptions = { } ) {
120+ options . packages = options . packages ?? "external" ;
121+ if ( options . packages === "auto" ) {
122+ options . external = autoExternal ( options . external ) ;
123+ }
124+
125+ return buildWatch ( [ input ] , {
126+ format : options . format === "umd" ? "esm" : options . format ,
127+ outfile : `${ output } .${ options . format === "esm" ? NODE_MJS : NODE_CJS } ` ,
90128 platform : "node" ,
91- target : "node20" ,
92- packages : "external" ,
93- supported
129+ target : "node22" ,
130+ packages : options . packages === "auto" ? "bundle" : options . packages ,
94131 } ) ;
95132}
96133
97- export function neutralTpl ( input : string , output : string , { format = "esm" , globalName, libraryName, keepNames, external = [ ] } : TplOptions = { } ) {
134+ export function neutralTpl ( input : string , output : string , options : TplOptions = { } ) {
135+ options . format = options . format ?? "esm" ;
136+
98137 let postfix = "" ;
99- switch ( format ) {
138+ switch ( options . format ) {
100139 case "iife" :
101140 postfix = "iife.js" ;
102141 break ;
@@ -110,35 +149,36 @@ export function neutralTpl(input: string, output: string, { format = "esm", glob
110149 postfix = "umd.js" ;
111150 break ;
112151 default :
113- throw new Error ( `Unknown format: ${ format } ` ) ;
152+ throw new Error ( `Unknown format: ${ options . format } ` ) ;
114153 }
115- return buildWatch ( input , format , external , {
116- outfile : `${ output } .${ format === "esm" ? "js" : `${ format } .js` } ` ,
154+ return buildWatch ( [ input ] , {
155+ format : options . format === "umd" ? "esm" : options . format ,
156+ outfile : `${ output } .${ options . format === "esm" ? "js" : `${ options . format } .js` } ` ,
117157 platform : "neutral" ,
118158 target : "es2022" ,
119- globalName,
120- keepNames,
121- plugins : format === "umd" ? [ umdWrapper ( { libraryName } ) ] : [ ] as Plugin [ ]
122- } as BuildOptions ) ;
159+ globalName : options . globalName ,
160+ keepNames : options . keepNames ,
161+ plugins : options . format === "umd" ? [ umdWrapper ( { libraryName : options . libraryName } ) ] : [ ] as Plugin [ ]
162+ } ) ;
123163}
124164
125- export function browserBoth ( input : string , output : string , globalName ?: string , libraryName ?: string , external : string [ ] = [ ] ) {
165+ export function browserBoth ( input : string , output : string , options : TplOptions = { } ) {
126166 return Promise . all ( [
127- browserTpl ( input , output , { format : "esm" , globalName , libraryName , external } ) ,
128- browserTpl ( input , output , { format : "umd" , globalName , libraryName , external } )
167+ browserTpl ( input , output , { format : "esm" , ... options } ) ,
168+ browserTpl ( input , output , { format : "umd" , ... options } )
129169 ] ) ;
130170}
131171
132- export function nodeBoth ( input : string , output : string , external : string [ ] = [ ] ) {
172+ export function nodeBoth ( input : string , output : string , options : TplOptions = { } ) {
133173 return Promise . all ( [
134- nodeTpl ( input , output , { format : "esm" , external } ) ,
135- nodeTpl ( input , output , { format : "cjs" , external } )
174+ nodeTpl ( input , output , { format : "esm" , ... options } ) ,
175+ nodeTpl ( input , output , { format : "cjs" , ... options } )
136176 ] ) ;
137177}
138178
139- export function bothTpl ( input : string , output : string , globalName ?: string , libraryName ?: string , external : string [ ] = [ ] ) {
179+ export function bothTpl ( input : string , output : string , options : TplOptions = { } ) {
140180 return Promise . all ( [
141- browserBoth ( input , output , globalName , libraryName , external ) ,
142- nodeTpl ( input , output , { format : "cjs" , external } )
181+ browserBoth ( input , output , { ... options } ) ,
182+ nodeTpl ( input , output , { format : "cjs" , ... options } )
143183 ] ) ;
144184}
0 commit comments