bertui 0.1.5 → 0.1.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -20
- package/bin/bertui.js +7 -7
- package/index.js +35 -35
- package/package.json +5 -4
- package/src/build/css-builder.js +83 -83
- package/src/build.js +122 -122
- package/src/cli.js +65 -65
- package/src/client/compiler.js +227 -90
- package/src/config/defaultConfig.js +15 -15
- package/src/config/loadConfig.js +32 -32
- package/src/dev.js +22 -22
- package/src/logger/logger.js +17 -17
- package/src/logger/notes.md +19 -19
- package/src/router/Router.js +129 -0
- package/src/server/dev-server.js +279 -198
- package/src/styles/bertui.css +209 -209
- package/src/router/router.js +0 -216
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2025 BunElysiaReact
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 BunElysiaReact
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
21
|
SOFTWARE.
|
package/bin/bertui.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
// bin/bertui.js
|
|
3
|
-
|
|
4
|
-
// Import the CLI program from index
|
|
5
|
-
import { program } from '../index.js';
|
|
6
|
-
|
|
7
|
-
// Run it
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// bin/bertui.js
|
|
3
|
+
|
|
4
|
+
// Import the CLI program from index
|
|
5
|
+
import { program } from '../index.js';
|
|
6
|
+
|
|
7
|
+
// Run it
|
|
8
8
|
program();
|
package/index.js
CHANGED
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
// index.js
|
|
2
|
-
import logger from "./src/logger/logger.js";
|
|
3
|
-
import { defaultConfig } from "./src/config/defaultConfig.js";
|
|
4
|
-
import { loadConfig } from "./src/config/loadConfig.js";
|
|
5
|
-
import { startDev } from "./src/dev.js";
|
|
6
|
-
import { buildProduction } from "./src/build.js";
|
|
7
|
-
import { compileProject } from "./src/client/compiler.js";
|
|
8
|
-
import { buildCSS, copyCSS } from "./src/build/css-builder.js";
|
|
9
|
-
import { program } from "./src/cli.js";
|
|
10
|
-
|
|
11
|
-
// Named exports
|
|
12
|
-
export {
|
|
13
|
-
logger,
|
|
14
|
-
defaultConfig,
|
|
15
|
-
loadConfig,
|
|
16
|
-
startDev,
|
|
17
|
-
buildProduction,
|
|
18
|
-
compileProject,
|
|
19
|
-
buildCSS,
|
|
20
|
-
copyCSS,
|
|
21
|
-
program
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
// Default export
|
|
25
|
-
export default {
|
|
26
|
-
logger,
|
|
27
|
-
defaultConfig,
|
|
28
|
-
loadConfig,
|
|
29
|
-
startDev,
|
|
30
|
-
buildProduction,
|
|
31
|
-
compileProject,
|
|
32
|
-
buildCSS,
|
|
33
|
-
copyCSS,
|
|
34
|
-
program,
|
|
35
|
-
version: "0.1.
|
|
1
|
+
// index.js
|
|
2
|
+
import logger from "./src/logger/logger.js";
|
|
3
|
+
import { defaultConfig } from "./src/config/defaultConfig.js";
|
|
4
|
+
import { loadConfig } from "./src/config/loadConfig.js";
|
|
5
|
+
import { startDev } from "./src/dev.js";
|
|
6
|
+
import { buildProduction } from "./src/build.js";
|
|
7
|
+
import { compileProject } from "./src/client/compiler.js";
|
|
8
|
+
import { buildCSS, copyCSS } from "./src/build/css-builder.js";
|
|
9
|
+
import { program } from "./src/cli.js";
|
|
10
|
+
|
|
11
|
+
// Named exports
|
|
12
|
+
export {
|
|
13
|
+
logger,
|
|
14
|
+
defaultConfig,
|
|
15
|
+
loadConfig,
|
|
16
|
+
startDev,
|
|
17
|
+
buildProduction,
|
|
18
|
+
compileProject,
|
|
19
|
+
buildCSS,
|
|
20
|
+
copyCSS,
|
|
21
|
+
program
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Default export
|
|
25
|
+
export default {
|
|
26
|
+
logger,
|
|
27
|
+
defaultConfig,
|
|
28
|
+
loadConfig,
|
|
29
|
+
startDev,
|
|
30
|
+
buildProduction,
|
|
31
|
+
compileProject,
|
|
32
|
+
buildCSS,
|
|
33
|
+
copyCSS,
|
|
34
|
+
program,
|
|
35
|
+
version: "0.1.7"
|
|
36
36
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bertui",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "Lightning-fast React dev server powered by Bun and Elysia",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -8,9 +8,10 @@
|
|
|
8
8
|
"bertui": "./bin/bertui.js"
|
|
9
9
|
},
|
|
10
10
|
"exports": {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
".": "./index.js",
|
|
12
|
+
"./styles": "./src/styles/bertui.css",
|
|
13
|
+
"./logger": "./src/logger/logger.js",
|
|
14
|
+
"./router": "./src/router/Router.jsx"
|
|
14
15
|
},
|
|
15
16
|
"files": [
|
|
16
17
|
"bin",
|
package/src/build/css-builder.js
CHANGED
|
@@ -1,84 +1,84 @@
|
|
|
1
|
-
// src/build/css-builder.js
|
|
2
|
-
import { join } from 'path';
|
|
3
|
-
import { existsSync, mkdirSync } from 'fs';
|
|
4
|
-
import postcss from 'postcss';
|
|
5
|
-
import autoprefixer from 'autoprefixer';
|
|
6
|
-
import cssnano from 'cssnano';
|
|
7
|
-
import logger from '../logger/logger.js';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Build and minify CSS for production
|
|
11
|
-
* @param {string} srcPath - Source CSS file path
|
|
12
|
-
* @param {string} destPath - Destination CSS file path
|
|
13
|
-
*/
|
|
14
|
-
export async function buildCSS(srcPath, destPath) {
|
|
15
|
-
try {
|
|
16
|
-
logger.info('Processing CSS...');
|
|
17
|
-
|
|
18
|
-
// Ensure destination directory exists
|
|
19
|
-
const destDir = join(destPath, '..');
|
|
20
|
-
if (!existsSync(destDir)) {
|
|
21
|
-
mkdirSync(destDir, { recursive: true });
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Read source CSS
|
|
25
|
-
const css = await Bun.file(srcPath).text();
|
|
26
|
-
|
|
27
|
-
// Process with PostCSS
|
|
28
|
-
const result = await postcss([
|
|
29
|
-
autoprefixer(),
|
|
30
|
-
cssnano({
|
|
31
|
-
preset: ['default', {
|
|
32
|
-
discardComments: { removeAll: true },
|
|
33
|
-
normalizeWhitespace: true,
|
|
34
|
-
colormin: true,
|
|
35
|
-
minifyFontValues: true,
|
|
36
|
-
minifySelectors: true,
|
|
37
|
-
}]
|
|
38
|
-
})
|
|
39
|
-
]).process(css, { from: srcPath, to: destPath });
|
|
40
|
-
|
|
41
|
-
// Write minified CSS
|
|
42
|
-
await Bun.write(destPath, result.css);
|
|
43
|
-
|
|
44
|
-
// Calculate size reduction
|
|
45
|
-
const originalSize = (Buffer.byteLength(css) / 1024).toFixed(2);
|
|
46
|
-
const minifiedSize = (Buffer.byteLength(result.css) / 1024).toFixed(2);
|
|
47
|
-
const reduction = ((1 - Buffer.byteLength(result.css) / Buffer.byteLength(css)) * 100).toFixed(1);
|
|
48
|
-
|
|
49
|
-
logger.success(`CSS minified: ${originalSize}KB → ${minifiedSize}KB (-${reduction}%)`);
|
|
50
|
-
|
|
51
|
-
if (result.warnings().length > 0) {
|
|
52
|
-
result.warnings().forEach(warn => {
|
|
53
|
-
logger.warn(warn.toString());
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
return { success: true, size: minifiedSize };
|
|
58
|
-
} catch (error) {
|
|
59
|
-
logger.error(`CSS build failed: ${error.message}`);
|
|
60
|
-
throw error;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Copy CSS without minification (for dev)
|
|
66
|
-
* @param {string} srcPath - Source CSS file path
|
|
67
|
-
* @param {string} destPath - Destination CSS file path
|
|
68
|
-
*/
|
|
69
|
-
export async function copyCSS(srcPath, destPath) {
|
|
70
|
-
try {
|
|
71
|
-
const destDir = join(destPath, '..');
|
|
72
|
-
if (!existsSync(destDir)) {
|
|
73
|
-
mkdirSync(destDir, { recursive: true });
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
await Bun.write(destPath, Bun.file(srcPath));
|
|
77
|
-
logger.info('CSS copied for development');
|
|
78
|
-
|
|
79
|
-
return { success: true };
|
|
80
|
-
} catch (error) {
|
|
81
|
-
logger.error(`CSS copy failed: ${error.message}`);
|
|
82
|
-
throw error;
|
|
83
|
-
}
|
|
1
|
+
// src/build/css-builder.js
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { existsSync, mkdirSync } from 'fs';
|
|
4
|
+
import postcss from 'postcss';
|
|
5
|
+
import autoprefixer from 'autoprefixer';
|
|
6
|
+
import cssnano from 'cssnano';
|
|
7
|
+
import logger from '../logger/logger.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Build and minify CSS for production
|
|
11
|
+
* @param {string} srcPath - Source CSS file path
|
|
12
|
+
* @param {string} destPath - Destination CSS file path
|
|
13
|
+
*/
|
|
14
|
+
export async function buildCSS(srcPath, destPath) {
|
|
15
|
+
try {
|
|
16
|
+
logger.info('Processing CSS...');
|
|
17
|
+
|
|
18
|
+
// Ensure destination directory exists
|
|
19
|
+
const destDir = join(destPath, '..');
|
|
20
|
+
if (!existsSync(destDir)) {
|
|
21
|
+
mkdirSync(destDir, { recursive: true });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Read source CSS
|
|
25
|
+
const css = await Bun.file(srcPath).text();
|
|
26
|
+
|
|
27
|
+
// Process with PostCSS
|
|
28
|
+
const result = await postcss([
|
|
29
|
+
autoprefixer(),
|
|
30
|
+
cssnano({
|
|
31
|
+
preset: ['default', {
|
|
32
|
+
discardComments: { removeAll: true },
|
|
33
|
+
normalizeWhitespace: true,
|
|
34
|
+
colormin: true,
|
|
35
|
+
minifyFontValues: true,
|
|
36
|
+
minifySelectors: true,
|
|
37
|
+
}]
|
|
38
|
+
})
|
|
39
|
+
]).process(css, { from: srcPath, to: destPath });
|
|
40
|
+
|
|
41
|
+
// Write minified CSS
|
|
42
|
+
await Bun.write(destPath, result.css);
|
|
43
|
+
|
|
44
|
+
// Calculate size reduction
|
|
45
|
+
const originalSize = (Buffer.byteLength(css) / 1024).toFixed(2);
|
|
46
|
+
const minifiedSize = (Buffer.byteLength(result.css) / 1024).toFixed(2);
|
|
47
|
+
const reduction = ((1 - Buffer.byteLength(result.css) / Buffer.byteLength(css)) * 100).toFixed(1);
|
|
48
|
+
|
|
49
|
+
logger.success(`CSS minified: ${originalSize}KB → ${minifiedSize}KB (-${reduction}%)`);
|
|
50
|
+
|
|
51
|
+
if (result.warnings().length > 0) {
|
|
52
|
+
result.warnings().forEach(warn => {
|
|
53
|
+
logger.warn(warn.toString());
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return { success: true, size: minifiedSize };
|
|
58
|
+
} catch (error) {
|
|
59
|
+
logger.error(`CSS build failed: ${error.message}`);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Copy CSS without minification (for dev)
|
|
66
|
+
* @param {string} srcPath - Source CSS file path
|
|
67
|
+
* @param {string} destPath - Destination CSS file path
|
|
68
|
+
*/
|
|
69
|
+
export async function copyCSS(srcPath, destPath) {
|
|
70
|
+
try {
|
|
71
|
+
const destDir = join(destPath, '..');
|
|
72
|
+
if (!existsSync(destDir)) {
|
|
73
|
+
mkdirSync(destDir, { recursive: true });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
await Bun.write(destPath, Bun.file(srcPath));
|
|
77
|
+
logger.info('CSS copied for development');
|
|
78
|
+
|
|
79
|
+
return { success: true };
|
|
80
|
+
} catch (error) {
|
|
81
|
+
logger.error(`CSS copy failed: ${error.message}`);
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
84
|
}
|
package/src/build.js
CHANGED
|
@@ -1,123 +1,123 @@
|
|
|
1
|
-
// src/build.js
|
|
2
|
-
import { join } from 'path';
|
|
3
|
-
import { existsSync, mkdirSync, rmSync, cpSync } from 'fs';
|
|
4
|
-
import logger from './logger/logger.js';
|
|
5
|
-
import { buildCSS } from './build/css-builder.js';
|
|
6
|
-
|
|
7
|
-
export async function buildProduction(options = {}) {
|
|
8
|
-
const root = options.root || process.cwd();
|
|
9
|
-
const outDir = join(root, 'dist');
|
|
10
|
-
|
|
11
|
-
logger.bigLog('BUILDING FOR PRODUCTION', { color: 'green' });
|
|
12
|
-
|
|
13
|
-
// Clean dist folder
|
|
14
|
-
if (existsSync(outDir)) {
|
|
15
|
-
rmSync(outDir, { recursive: true });
|
|
16
|
-
logger.info('Cleaned dist/');
|
|
17
|
-
}
|
|
18
|
-
mkdirSync(outDir, { recursive: true });
|
|
19
|
-
|
|
20
|
-
const startTime = Date.now();
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
// Step 1: Build CSS from BertUI library
|
|
24
|
-
logger.info('Step 1: Building CSS...');
|
|
25
|
-
const bertuiCssSource = join(import.meta.dir, 'styles/bertui.css');
|
|
26
|
-
const bertuiCssDest = join(outDir, 'styles/bertui.min.css');
|
|
27
|
-
await buildCSS(bertuiCssSource, bertuiCssDest);
|
|
28
|
-
|
|
29
|
-
// Step 2: Copy public assets if they exist
|
|
30
|
-
const publicDir = join(root, 'public');
|
|
31
|
-
if (existsSync(publicDir)) {
|
|
32
|
-
logger.info('Step 2: Copying public assets...');
|
|
33
|
-
cpSync(publicDir, outDir, { recursive: true });
|
|
34
|
-
logger.success('Public assets copied');
|
|
35
|
-
} else {
|
|
36
|
-
logger.info('Step 2: No public directory found, skipping...');
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
// Step 3: Build JavaScript with Bun's bundler
|
|
40
|
-
logger.info('Step 3: Bundling JavaScript...');
|
|
41
|
-
const mainEntry = join(root, 'src/main.jsx');
|
|
42
|
-
|
|
43
|
-
if (!existsSync(mainEntry)) {
|
|
44
|
-
logger.error('Entry point not found: src/main.jsx');
|
|
45
|
-
process.exit(1);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const result = await Bun.build({
|
|
49
|
-
entrypoints: [mainEntry],
|
|
50
|
-
outdir: join(outDir, 'assets'),
|
|
51
|
-
target: 'browser',
|
|
52
|
-
minify: true,
|
|
53
|
-
splitting: true,
|
|
54
|
-
sourcemap: 'external',
|
|
55
|
-
naming: {
|
|
56
|
-
entry: '[name]-[hash].js',
|
|
57
|
-
chunk: 'chunks/[name]-[hash].js',
|
|
58
|
-
asset: '[name]-[hash].[ext]'
|
|
59
|
-
},
|
|
60
|
-
external: [] // Don't externalize anything for browser builds
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
if (!result.success) {
|
|
64
|
-
logger.error('JavaScript build failed!');
|
|
65
|
-
result.logs.forEach(log => logger.error(log.message));
|
|
66
|
-
process.exit(1);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
logger.success('JavaScript bundled');
|
|
70
|
-
|
|
71
|
-
// Step 4: Generate index.html
|
|
72
|
-
logger.info('Step 4: Generating index.html...');
|
|
73
|
-
await generateProductionHTML(root, outDir, result);
|
|
74
|
-
|
|
75
|
-
const duration = Date.now() - startTime;
|
|
76
|
-
logger.success(`✨ Build complete in ${duration}ms`);
|
|
77
|
-
logger.info(`📦 Output: ${outDir}`);
|
|
78
|
-
|
|
79
|
-
// Display build stats
|
|
80
|
-
logger.table(result.outputs.map(o => ({
|
|
81
|
-
file: o.path.replace(outDir, ''),
|
|
82
|
-
size: `${(o.size / 1024).toFixed(2)} KB`
|
|
83
|
-
})));
|
|
84
|
-
|
|
85
|
-
} catch (error) {
|
|
86
|
-
logger.error(`Build failed: ${error.message}`);
|
|
87
|
-
if (error.stack) {
|
|
88
|
-
logger.error(error.stack);
|
|
89
|
-
}
|
|
90
|
-
process.exit(1);
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
async function generateProductionHTML(root, outDir, buildResult) {
|
|
95
|
-
// Find the main bundle
|
|
96
|
-
const mainBundle = buildResult.outputs.find(o =>
|
|
97
|
-
o.path.includes('main') && o.kind === 'entry-point'
|
|
98
|
-
);
|
|
99
|
-
|
|
100
|
-
if (!mainBundle) {
|
|
101
|
-
throw new Error('Could not find main bundle in build output');
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
const bundlePath = mainBundle.path.replace(outDir, '').replace(/^\//, '');
|
|
105
|
-
|
|
106
|
-
const html = `<!DOCTYPE html>
|
|
107
|
-
<html lang="en">
|
|
108
|
-
<head>
|
|
109
|
-
<meta charset="UTF-8">
|
|
110
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
111
|
-
<meta name="description" content="Built with BertUI - Lightning fast React development">
|
|
112
|
-
<title>BertUI App</title>
|
|
113
|
-
<link rel="stylesheet" href="/styles/bertui.min.css">
|
|
114
|
-
</head>
|
|
115
|
-
<body>
|
|
116
|
-
<div id="root"></div>
|
|
117
|
-
<script type="module" src="/${bundlePath}"></script>
|
|
118
|
-
</body>
|
|
119
|
-
</html>`;
|
|
120
|
-
|
|
121
|
-
await Bun.write(join(outDir, 'index.html'), html);
|
|
122
|
-
logger.success('Generated index.html');
|
|
1
|
+
// src/build.js
|
|
2
|
+
import { join } from 'path';
|
|
3
|
+
import { existsSync, mkdirSync, rmSync, cpSync } from 'fs';
|
|
4
|
+
import logger from './logger/logger.js';
|
|
5
|
+
import { buildCSS } from './build/css-builder.js';
|
|
6
|
+
|
|
7
|
+
export async function buildProduction(options = {}) {
|
|
8
|
+
const root = options.root || process.cwd();
|
|
9
|
+
const outDir = join(root, 'dist');
|
|
10
|
+
|
|
11
|
+
logger.bigLog('BUILDING FOR PRODUCTION', { color: 'green' });
|
|
12
|
+
|
|
13
|
+
// Clean dist folder
|
|
14
|
+
if (existsSync(outDir)) {
|
|
15
|
+
rmSync(outDir, { recursive: true });
|
|
16
|
+
logger.info('Cleaned dist/');
|
|
17
|
+
}
|
|
18
|
+
mkdirSync(outDir, { recursive: true });
|
|
19
|
+
|
|
20
|
+
const startTime = Date.now();
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
// Step 1: Build CSS from BertUI library
|
|
24
|
+
logger.info('Step 1: Building CSS...');
|
|
25
|
+
const bertuiCssSource = join(import.meta.dir, 'styles/bertui.css');
|
|
26
|
+
const bertuiCssDest = join(outDir, 'styles/bertui.min.css');
|
|
27
|
+
await buildCSS(bertuiCssSource, bertuiCssDest);
|
|
28
|
+
|
|
29
|
+
// Step 2: Copy public assets if they exist
|
|
30
|
+
const publicDir = join(root, 'public');
|
|
31
|
+
if (existsSync(publicDir)) {
|
|
32
|
+
logger.info('Step 2: Copying public assets...');
|
|
33
|
+
cpSync(publicDir, outDir, { recursive: true });
|
|
34
|
+
logger.success('Public assets copied');
|
|
35
|
+
} else {
|
|
36
|
+
logger.info('Step 2: No public directory found, skipping...');
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// Step 3: Build JavaScript with Bun's bundler
|
|
40
|
+
logger.info('Step 3: Bundling JavaScript...');
|
|
41
|
+
const mainEntry = join(root, 'src/main.jsx');
|
|
42
|
+
|
|
43
|
+
if (!existsSync(mainEntry)) {
|
|
44
|
+
logger.error('Entry point not found: src/main.jsx');
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const result = await Bun.build({
|
|
49
|
+
entrypoints: [mainEntry],
|
|
50
|
+
outdir: join(outDir, 'assets'),
|
|
51
|
+
target: 'browser',
|
|
52
|
+
minify: true,
|
|
53
|
+
splitting: true,
|
|
54
|
+
sourcemap: 'external',
|
|
55
|
+
naming: {
|
|
56
|
+
entry: '[name]-[hash].js',
|
|
57
|
+
chunk: 'chunks/[name]-[hash].js',
|
|
58
|
+
asset: '[name]-[hash].[ext]'
|
|
59
|
+
},
|
|
60
|
+
external: [] // Don't externalize anything for browser builds
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
if (!result.success) {
|
|
64
|
+
logger.error('JavaScript build failed!');
|
|
65
|
+
result.logs.forEach(log => logger.error(log.message));
|
|
66
|
+
process.exit(1);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
logger.success('JavaScript bundled');
|
|
70
|
+
|
|
71
|
+
// Step 4: Generate index.html
|
|
72
|
+
logger.info('Step 4: Generating index.html...');
|
|
73
|
+
await generateProductionHTML(root, outDir, result);
|
|
74
|
+
|
|
75
|
+
const duration = Date.now() - startTime;
|
|
76
|
+
logger.success(`✨ Build complete in ${duration}ms`);
|
|
77
|
+
logger.info(`📦 Output: ${outDir}`);
|
|
78
|
+
|
|
79
|
+
// Display build stats
|
|
80
|
+
logger.table(result.outputs.map(o => ({
|
|
81
|
+
file: o.path.replace(outDir, ''),
|
|
82
|
+
size: `${(o.size / 1024).toFixed(2)} KB`
|
|
83
|
+
})));
|
|
84
|
+
|
|
85
|
+
} catch (error) {
|
|
86
|
+
logger.error(`Build failed: ${error.message}`);
|
|
87
|
+
if (error.stack) {
|
|
88
|
+
logger.error(error.stack);
|
|
89
|
+
}
|
|
90
|
+
process.exit(1);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async function generateProductionHTML(root, outDir, buildResult) {
|
|
95
|
+
// Find the main bundle
|
|
96
|
+
const mainBundle = buildResult.outputs.find(o =>
|
|
97
|
+
o.path.includes('main') && o.kind === 'entry-point'
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
if (!mainBundle) {
|
|
101
|
+
throw new Error('Could not find main bundle in build output');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const bundlePath = mainBundle.path.replace(outDir, '').replace(/^\//, '');
|
|
105
|
+
|
|
106
|
+
const html = `<!DOCTYPE html>
|
|
107
|
+
<html lang="en">
|
|
108
|
+
<head>
|
|
109
|
+
<meta charset="UTF-8">
|
|
110
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
111
|
+
<meta name="description" content="Built with BertUI - Lightning fast React development">
|
|
112
|
+
<title>BertUI App</title>
|
|
113
|
+
<link rel="stylesheet" href="/styles/bertui.min.css">
|
|
114
|
+
</head>
|
|
115
|
+
<body>
|
|
116
|
+
<div id="root"></div>
|
|
117
|
+
<script type="module" src="/${bundlePath}"></script>
|
|
118
|
+
</body>
|
|
119
|
+
</html>`;
|
|
120
|
+
|
|
121
|
+
await Bun.write(join(outDir, 'index.html'), html);
|
|
122
|
+
logger.success('Generated index.html');
|
|
123
123
|
}
|