1- import * as esbuild from 'esbuild' ;
2- import { readFileSync , writeFileSync , mkdirSync } from 'fs' ;
3- import { resolve , dirname } from 'path' ;
4- import postcss from 'postcss' ;
5- import postcssImport from 'postcss-import' ;
6- import cssnano from 'cssnano' ;
1+
2+ const esbuild = require ( 'esbuild' ) ;
3+ const fs = require ( 'fs' ) ;
4+ const path = require ( 'path' ) ;
5+
6+ const postcss = require ( 'postcss' ) ;
7+ const postcssImport = require ( 'postcss-import' ) ;
8+ const cssnano = require ( 'cssnano' ) ;
79
810const production = process . env . NODE_ENV === 'production' ;
911
@@ -12,7 +14,7 @@ const cssPlugin = {
1214 name : 'css' ,
1315 setup ( build ) {
1416 build . onLoad ( { filter : / \. c s s $ / } , async ( args ) => {
15- const css = readFileSync ( args . path , 'utf8' ) ;
17+ const css = fs . readFileSync ( args . path , 'utf8' ) ;
1618
1719 // Process CSS with PostCSS
1820 const result = await postcss ( [
@@ -29,79 +31,88 @@ const cssPlugin = {
2931} ;
3032
3133// Build configuration
32- const baseConfig = {
33- entryPoints : [ 'src/index.ts' ] ,
34- bundle : true ,
35- external : [ '@yasgui/yasr' , '@yasgui/utils' , 'tabulator-tables' ] ,
36- sourcemap : true ,
37- target : 'es2020' ,
38- plugins : [ cssPlugin ] ,
39- } ;
40-
41- async function build ( ) {
42- try {
43- // Create dist directory
44- mkdirSync ( 'dist' , { recursive : true } ) ;
45- mkdirSync ( 'dist/types' , { recursive : true } ) ;
46-
47- // Build UMD format
48- await esbuild . build ( {
49- ...baseConfig ,
50- outfile : 'dist/yasgui-table-plugin.umd.js' ,
51- format : 'iife' ,
52- globalName : 'YasguiTablePlugin' ,
53- footer : {
54- js : 'if (typeof module !== "undefined" && module.exports) { module.exports = YasguiTablePlugin; }'
55- } ,
56- } ) ;
34+ const buildConfigs = [
35+ // ES Module (for bundlers like webpack, vite, rollup)
36+ {
37+ entryPoints : [ 'src/index.ts' ] ,
38+ bundle : true ,
39+ minify : false ,
40+ sourcemap : true ,
41+ target : [ 'es2018' ] ,
42+ format : 'esm' ,
43+ outfile : 'dist/yasgui-table-plugin.esm.js' ,
44+ external : [ ] ,
45+ loader : {
46+ '.js' : 'js' ,
47+ } ,
48+ plugins : [ cssPlugin ] ,
49+ } ,
50+ // CommonJS (for Node.js)
51+ {
52+ entryPoints : [ 'src/index.ts' ] ,
53+ bundle : true ,
54+ minify : false ,
55+ sourcemap : true ,
56+ target : [ 'es2018' ] ,
57+ format : 'cjs' ,
58+ outfile : 'dist/yasgui-table-plugin.cjs.js' ,
59+ external : [ ] ,
60+ loader : {
61+ '.js' : 'js' ,
62+ } ,
63+ plugins : [ cssPlugin ] ,
64+ } ,
65+ // IIFE (for browsers via unpkg.com and script tags)
66+ {
67+ entryPoints : [ 'src/index.ts' ] ,
68+ bundle : true ,
69+ minify : true ,
70+ sourcemap : true ,
71+ target : [ 'es2018' ] ,
72+ format : 'iife' ,
73+ globalName : 'TablePlugin' ,
74+ outfile : 'dist/yasgui-table-plugin.min.js' ,
75+ external : [ ] ,
76+ loader : {
77+ '.js' : 'js' ,
78+ } ,
79+ plugins : [ cssPlugin ] ,
80+ } ,
81+ ] ;
5782
58- // Build ESM format
59- await esbuild . build ( {
60- ...baseConfig ,
61- outfile : 'dist/yasgui-table-plugin.esm.js' ,
62- format : 'esm' ,
63- } ) ;
83+ // TypeScript declaration content
84+ const typeDeclaration = `declare module '@matdata/yasgui-table-plugin';
85+ ` ;
6486
65- // Build minified version in production
66- if ( production ) {
67- await esbuild . build ( {
68- ...baseConfig ,
69- outfile : 'dist/yasgui-table-plugin.min.js' ,
70- format : 'iife' ,
71- globalName : 'YasguiTablePlugin' ,
72- minify : true ,
73- footer : {
74- js : 'if (typeof module !== "undefined" && module.exports) { module.exports = YasguiTablePlugin; }'
75- } ,
76- } ) ;
87+ // Build all formats
88+ Promise . all ( buildConfigs . map ( config => esbuild . build ( config ) ) )
89+ . then ( ( ) => {
90+ // Create TypeScript declaration file
91+ const distDir = path . join ( __dirname , 'dist' ) ;
92+ if ( ! fs . existsSync ( distDir ) ) {
93+ fs . mkdirSync ( distDir , { recursive : true } ) ;
7794 }
7895
7996 // Build CSS bundle
80- const cssContent = readFileSync ( 'styles/index.css' , 'utf8' ) ;
81- const cssResult = await postcss ( [
97+ const cssContent = fs . readFileSync ( 'styles/index.css' , 'utf8' ) ;
98+ return postcss ( [
8299 postcssImport ( ) ,
83100 ...( production ? [ cssnano ( ) ] : [ ] )
84- ] ) . process ( cssContent , { from : 'styles/index.css' } ) ;
85-
86- writeFileSync ( 'dist/yasgui-table-plugin.css' , cssResult . css ) ;
101+ ] ) . process ( cssContent , { from : 'styles/index.css' } )
102+ . then ( ( cssResult ) => {
103+ fs . writeFileSync ( 'dist/yasgui-table-plugin.css' , cssResult . css ) ;
104+ fs . writeFileSync ( path . join ( distDir , 'index.d.ts' ) , typeDeclaration ) ;
87105
88- console . log ( 'Build completed successfully!' ) ;
89- } catch ( error ) {
90- console . error ( 'Build failed:' , error ) ;
106+ console . log ( '✅ Build complete:' ) ;
107+ console . log ( ' - dist/yasgui-table-plugin.esm.js (ES Module for bundlers)' ) ;
108+ console . log ( ' - dist/yasgui-table-plugin.cjs.js (CommonJS for Node.js)' ) ;
109+ console . log ( ' - dist/yasgui-table-plugin.min.js (IIFE for browsers/unpkg)' ) ;
110+ console . log ( ' - dist/yasgui-table-plugin.css (Bundled CSS)' ) ;
111+ console . log ( ' - dist/index.d.ts (TypeScript declarations)' ) ;
112+ } ) ;
113+ } )
114+ . catch ( ( err ) => {
115+ console . error ( '❌ Build failed:' , err ) ;
91116 process . exit ( 1 ) ;
92- }
93- }
94-
95- // Watch mode
96- if ( process . argv . includes ( '--watch' ) ) {
97- const ctx = await esbuild . context ( {
98- ...baseConfig ,
99- outfile : 'dist/yasgui-table-plugin.esm.js' ,
100- format : 'esm' ,
101117 } ) ;
102-
103- await ctx . watch ( ) ;
104- console . log ( 'Watching for changes...' ) ;
105- } else {
106- build ( ) ;
107- }
118+
0 commit comments