-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-module.js
More file actions
205 lines (175 loc) Β· 5.06 KB
/
Copy pathbuild-module.js
File metadata and controls
205 lines (175 loc) Β· 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { spawn } from 'child_process';
import { existsSync, mkdirSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Module package configuration - ESM-only optimized builds
const MODULE_CONFIG = {
name: '9th.js-module',
version: '0.1.0',
source: 'src/index.ts',
output: {
esm: 'dist/module/esm/index.js',
esmMin: 'dist/module/esm/index.min.js'
},
// All standard modules (no experimental features)
modules: [
'core',
'geometry',
'materials',
'rendering',
'animation',
'loaders',
'lights',
'particles',
'physics',
'controls',
'textures',
'cameras'
]
};
class ModuleBuilder {
constructor() {
this.watchMode = process.argv.includes('--watch');
this.cleanOutput();
}
cleanOutput() {
const outputDirs = [
'dist/module',
'dist/module/esm'
];
for (const dir of outputDirs) {
if (existsSync(dir)) {
spawn('rm', ['-rf', dir]);
}
mkdirSync(dir, { recursive: true });
}
}
async buildESM() {
console.log('π¨ Building Module ESM...');
const buildArgs = [
'-c',
'rollup.module.config.js',
'--filter', 'esmModule'
];
if (this.watchMode) {
buildArgs.push('--watch');
}
return this.runRollup(buildArgs);
}
async buildESMMinified() {
console.log('π¨ Building Module ESM Minified...');
const buildArgs = [
'-c',
'rollup.module.config.js',
'--filter', 'esmModuleMinified'
];
if (this.watchMode) {
buildArgs.push('--watch');
}
return this.runRollup(buildArgs);
}
async buildTypes() {
console.log('π¨ Building Module TypeScript types...');
const typeArgs = [
'tsc',
'--emitDeclarationOnly',
'--outDir', 'dist/module/esm',
'--project', 'tsconfig.module.json'
];
return new Promise((resolve, reject) => {
const proc = spawn('npm', typeArgs, { stdio: 'inherit' });
proc.on('close', (code) => {
if (code === 0) {
console.log('β
Module types built successfully');
resolve();
} else {
reject(new Error(`TypeScript compilation failed with code ${code}`));
}
});
});
}
runRollup(args) {
return new Promise((resolve, reject) => {
const proc = spawn('rollup', args, { stdio: 'inherit' });
if (this.watchMode) {
proc.on('spawn', () => console.log('π Module build started in watch mode'));
}
proc.on('close', (code) => {
if (code === 0) {
console.log('β
Module build completed successfully');
resolve();
} else {
reject(new Error(`Build failed with code ${code}`));
}
});
proc.on('error', (err) => {
reject(err);
});
});
}
async buildAll() {
console.log('π Starting Module package build...');
console.log(`π¦ Building ${MODULE_CONFIG.name} v${MODULE_CONFIG.version}`);
console.log(`π― ESM-optimized modules: ${MODULE_CONFIG.modules.length} included`);
try {
await this.buildESM();
await this.buildESMMinified();
await this.buildTypes();
console.log('\nπ Module build completed successfully!');
console.log('π Output directories:');
console.log(' - ESM: dist/module/esm/');
console.log(' - Types: dist/module/esm/');
console.log('\nπ ESM-only benefits:');
console.log(' - Tree-shaking optimized');
console.log(' - Modern bundler compatible');
console.log(' - Reduced bundle size');
console.log(' - Native ES modules');
if (this.watchMode) {
console.log('\nπ Watching for module changes...');
}
} catch (error) {
console.error('β Module build failed:', error.message);
process.exit(1);
}
}
generateBundleInfo() {
const info = {
name: MODULE_CONFIG.name,
version: MODULE_CONFIG.version,
modules: MODULE_CONFIG.modules,
size: {
esm: '~90KB',
esmMinified: '~60KB'
},
target: 'ESM-only 3D graphics library optimized for modern bundlers',
features: [
'ES2020+ module syntax',
'Tree-shaking compatible',
'Optimized for bundlers (Webpack, Rollup, Vite)',
'No CommonJS/UMD overhead',
'Modern JavaScript features',
'TypeScript native support'
],
compatibility: {
bundlers: ['Webpack 5+', 'Rollup 3+', 'Vite 3+', 'esbuild'],
browsers: 'Modern browsers with ES2020+ support',
node: 'Node.js 16+ with ESM support'
}
};
console.log('\nπ Module Bundle Information:');
console.log(JSON.stringify(info, null, 2));
return info;
}
}
// Run the build
const builder = new ModuleBuilder();
builder.buildAll().then(() => {
builder.generateBundleInfo();
}).catch((error) => {
console.error('π₯ Fatal error:', error.message);
process.exit(1);
});
// Export for testing
export default ModuleBuilder;