11import fs from 'node:fs' ;
22import path from 'node:path' ;
3+ import zlib from 'node:zlib' ;
34import { fileURLToPath } from 'node:url' ;
45
56const __filename = fileURLToPath ( import . meta. url ) ;
@@ -8,7 +9,8 @@ const rootDir = path.resolve(__dirname, '..');
89
910const outputDir = path . join ( rootDir , 'docs/public/wasm' ) ;
1011fs . mkdirSync ( outputDir , { recursive : true } ) ;
11- const outputFile = path . join ( outputDir , 'typephp-runtime.json' ) ;
12+ const outputFileGz = path . join ( outputDir , 'typephp-runtime.json.gz' ) ;
13+ const outputFileJson = path . join ( outputDir , 'typephp-runtime.json' ) ;
1214
1315const searchPaths = [
1416 process . env . TYPEPHP_PATH ,
@@ -26,58 +28,61 @@ for (const candidate of searchPaths) {
2628}
2729
2830if ( ! typephpRepoPath ) {
29- if ( fs . existsSync ( outputFile ) ) {
31+ if ( fs . existsSync ( outputFileGz ) || fs . existsSync ( outputFileJson ) ) {
3032 console . log ( '\x1b[33m[TypePHP Bundler]\x1b[0m Core repository not found, but pre-built runtime bundle exists.' ) ;
31- console . log ( ` • Reusing existing bundle at: ${ outputFile } ` ) ;
3233 process . exit ( 0 ) ;
3334 }
34-
35- console . error ( '\x1b[31m[Bundle Error]\x1b[0m Could not locate the TypePHP core repository.' ) ;
36- console . error ( 'Searched in:' , searchPaths ) ;
37- console . error ( 'Please ensure the "typephp" repository is placed next to "typephp-docs" (e.g. ../typephp)' ) ;
38- console . error ( 'Or set the TYPEPHP_PATH environment variable: TYPEPHP_PATH=C:/path/to/typephp' ) ;
35+ console . error ( '\x1b[31m[Bundle Error]\x1b[0m Could not locate TypePHP core repository.' ) ;
3936 process . exit ( 1 ) ;
4037}
4138
4239console . log ( `\x1b[36m[TypePHP Bundler]\x1b[0m Found TypePHP core at: ${ typephpRepoPath } ` ) ;
4340
44- const vfs = { } ;
41+ const files = { } ;
42+ const dirSet = new Set ( ) ;
4543
4644function addFilesRecursively ( srcDir , vfsPrefix ) {
47- if ( ! fs . existsSync ( srcDir ) ) {
48- console . warn ( `\x1b[33m[Warning]\x1b[0m Directory not found: ${ srcDir } ` ) ;
49- return ;
50- }
45+ if ( ! fs . existsSync ( srcDir ) ) return ;
5146
5247 const entries = fs . readdirSync ( srcDir , { withFileTypes : true } ) ;
5348 for ( const entry of entries ) {
5449 const fullPath = path . join ( srcDir , entry . name ) ;
5550 if ( entry . isDirectory ( ) ) {
51+ dirSet . add ( `${ vfsPrefix } /${ entry . name } ` ) ;
5652 addFilesRecursively ( fullPath , `${ vfsPrefix } /${ entry . name } ` ) ;
5753 } else if ( entry . isFile ( ) && entry . name . endsWith ( '.php' ) ) {
5854 const vfsPath = `${ vfsPrefix } /${ entry . name } ` ;
59- vfs [ vfsPath ] = fs . readFileSync ( fullPath , 'utf8' ) ;
55+ files [ vfsPath ] = fs . readFileSync ( fullPath , 'utf8' ) ;
6056 }
6157 }
6258}
6359
6460console . log ( 'Packaging TypePHP src/...' ) ;
61+ dirSet . add ( '/typephp' ) ;
62+ dirSet . add ( '/typephp/src' ) ;
6563addFilesRecursively ( path . join ( typephpRepoPath , 'src' ) , '/typephp/src' ) ;
6664
6765console . log ( 'Packaging nikic/php-parser...' ) ;
66+ dirSet . add ( '/typephp/vendor' ) ;
67+ dirSet . add ( '/typephp/vendor/nikic' ) ;
68+ dirSet . add ( '/typephp/vendor/nikic/php-parser' ) ;
69+ dirSet . add ( '/typephp/vendor/nikic/php-parser/lib' ) ;
70+ dirSet . add ( '/typephp/vendor/nikic/php-parser/lib/PhpParser' ) ;
6871addFilesRecursively (
6972 path . join ( typephpRepoPath , 'vendor/nikic/php-parser/lib/PhpParser' ) ,
7073 '/typephp/vendor/nikic/php-parser/lib/PhpParser'
7174) ;
7275
7376console . log ( 'Packaging phpstan/phpdoc-parser...' ) ;
77+ dirSet . add ( '/typephp/vendor/phpstan' ) ;
78+ dirSet . add ( '/typephp/vendor/phpstan/phpdoc-parser' ) ;
79+ dirSet . add ( '/typephp/vendor/phpstan/phpdoc-parser/src' ) ;
7480addFilesRecursively (
7581 path . join ( typephpRepoPath , 'vendor/phpstan/phpdoc-parser/src' ) ,
7682 '/typephp/vendor/phpstan/phpdoc-parser/src'
7783) ;
7884
79- console . log ( 'Generating VFS autoloader...' ) ;
80- vfs [ '/typephp/vendor/autoload.php' ] = `<?php
85+ files [ '/typephp/vendor/autoload.php' ] = `<?php
8186declare(strict_types=1);
8287
8388spl_autoload_register(function (string $class): bool {
@@ -102,16 +107,24 @@ spl_autoload_register(function (string $class): bool {
102107});
103108` ;
104109
105- const fileCount = Object . keys ( vfs ) . length ;
106- if ( fileCount < 50 ) {
107- console . error ( `\x1b[31m[Error]\x1b[0m Only ${ fileCount } files collected. Did you run "composer install" in the TypePHP core repository?` ) ;
108- process . exit ( 1 ) ;
109- }
110+ const sortedDirs = Array . from ( dirSet ) . sort ( ( a , b ) => a . split ( '/' ) . length - b . split ( '/' ) . length ) ;
111+
112+ const payload = {
113+ dirs : sortedDirs ,
114+ files : files ,
115+ } ;
116+
117+ const jsonString = JSON . stringify ( payload ) ;
118+ const gzipped = zlib . gzipSync ( jsonString , { level : 9 } ) ;
119+
120+ fs . writeFileSync ( outputFileGz , gzipped ) ;
121+ fs . writeFileSync ( outputFileJson , jsonString ) ;
110122
111- fs . writeFileSync ( outputFile , JSON . stringify ( vfs ) ) ;
112- const sizeMb = ( fs . statSync ( outputFile ) . size / ( 1024 * 1024 ) ) . toFixed ( 2 ) ;
123+ const rawMb = ( Buffer . byteLength ( jsonString ) / ( 1024 * 1024 ) ) . toFixed ( 2 ) ;
124+ const gzKb = ( gzipped . length / 1024 ) . toFixed ( 0 ) ;
113125
114- console . log ( `\x1b[32m✓ Runtime bundle created successfully!\x1b[0m` ) ;
115- console . log ( ` • Destination: docs/public/wasm/typephp-runtime.json` ) ;
116- console . log ( ` • Files bundled: ${ fileCount } ` ) ;
117- console . log ( ` • Payload size: ${ sizeMb } MB` ) ;
126+ console . log ( `\x1b[32m✓ Runtime bundle deployed successfully!\x1b[0m` ) ;
127+ console . log ( ` • Raw JSON size: ${ rawMb } MB` ) ;
128+ console . log ( ` • Gzipped size: \x1b[32m${ gzKb } KB\x1b[0m (~92% reduction)` ) ;
129+ console . log ( ` • Files bundled: ${ Object . keys ( files ) . length } ` ) ;
130+ console . log ( ` • Pre-mapped dirs: ${ sortedDirs . length } ` ) ;
0 commit comments