hightjs 0.5.3 → 0.5.5
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/dist/adapters/express.d.ts +7 -0
- package/dist/adapters/express.js +63 -0
- package/dist/adapters/factory.d.ts +23 -0
- package/dist/adapters/factory.js +122 -0
- package/dist/adapters/fastify.d.ts +25 -0
- package/dist/adapters/fastify.js +61 -0
- package/dist/adapters/native.d.ts +8 -0
- package/dist/adapters/native.js +198 -0
- package/dist/api/console.d.ts +94 -0
- package/dist/api/console.js +294 -0
- package/dist/api/http.d.ts +180 -0
- package/dist/api/http.js +469 -0
- package/dist/bin/hightjs.d.ts +2 -0
- package/dist/bin/hightjs.js +214 -0
- package/dist/builder.d.ts +32 -0
- package/dist/builder.js +581 -0
- package/dist/client/DefaultNotFound.d.ts +1 -0
- package/dist/client/DefaultNotFound.js +79 -0
- package/dist/client/client.d.ts +3 -0
- package/dist/client/client.js +24 -0
- package/dist/client/clientRouter.d.ts +58 -0
- package/dist/client/clientRouter.js +132 -0
- package/dist/client/entry.client.d.ts +1 -0
- package/dist/client/entry.client.js +455 -0
- package/dist/components/Link.d.ts +7 -0
- package/dist/components/Link.js +13 -0
- package/dist/global/global.d.ts +117 -0
- package/dist/global/global.js +17 -0
- package/dist/helpers.d.ts +20 -0
- package/dist/helpers.js +583 -0
- package/dist/hotReload.d.ts +32 -0
- package/dist/hotReload.js +545 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +494 -0
- package/dist/loaders.d.ts +1 -0
- package/dist/loaders.js +46 -0
- package/dist/renderer.d.ts +14 -0
- package/dist/renderer.js +380 -0
- package/dist/router.d.ts +101 -0
- package/dist/router.js +659 -0
- package/dist/types/framework.d.ts +37 -0
- package/dist/types/framework.js +2 -0
- package/dist/types.d.ts +192 -0
- package/dist/types.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Builds a single entry point into a single output file.
|
|
3
|
+
* @param {string} entryPoint - The path to the entry file.
|
|
4
|
+
* @param {string} outfile - The full path to the output file.
|
|
5
|
+
* @param {boolean} isProduction - Se está em modo produção ou não.
|
|
6
|
+
* @returns {Promise<void>}
|
|
7
|
+
*/
|
|
8
|
+
export function build(entryPoint: string, outfile: string, isProduction?: boolean): Promise<void>;
|
|
9
|
+
/**
|
|
10
|
+
* Watches an entry point and its dependencies, rebuilding to a single output file.
|
|
11
|
+
* @param {string} entryPoint - The path to the entry file.
|
|
12
|
+
* @param {string} outfile - The full path to the output file.
|
|
13
|
+
* @param {Object} hotReloadManager - Manager de hot reload (opcional).
|
|
14
|
+
* @returns {Promise<void>}
|
|
15
|
+
*/
|
|
16
|
+
export function watch(entryPoint: string, outfile: string, hotReloadManager?: Object): Promise<void>;
|
|
17
|
+
/**
|
|
18
|
+
* Builds with code splitting into multiple chunks based on module types.
|
|
19
|
+
* @param {string} entryPoint - The path to the entry file.
|
|
20
|
+
* @param {string} outdir - The directory for output files.
|
|
21
|
+
* @param {boolean} isProduction - Se está em modo produção ou não.
|
|
22
|
+
* @returns {Promise<void>}
|
|
23
|
+
*/
|
|
24
|
+
export function buildWithChunks(entryPoint: string, outdir: string, isProduction?: boolean): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Watches with code splitting enabled
|
|
27
|
+
* @param {string} entryPoint - The path to the entry file.
|
|
28
|
+
* @param {string} outdir - The directory for output files.
|
|
29
|
+
* @param {Object} hotReloadManager - Manager de hot reload (opcional).
|
|
30
|
+
* @returns {Promise<void>}
|
|
31
|
+
*/
|
|
32
|
+
export function watchWithChunks(entryPoint: string, outdir: string, hotReloadManager?: Object): Promise<void>;
|
package/dist/builder.js
ADDED
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* This file is part of the HightJS Project.
|
|
4
|
+
* Copyright (c) 2025 itsmuzin
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/
|
|
18
|
+
const esbuild = require('esbuild');
|
|
19
|
+
const path = require('path');
|
|
20
|
+
const Console = require("./api/console").default;
|
|
21
|
+
const fs = require('fs');
|
|
22
|
+
const { readdir, stat } = require("node:fs/promises");
|
|
23
|
+
const { rm } = require("fs-extra");
|
|
24
|
+
// Lista de módulos nativos do Node.js para marcar como externos (apenas os built-ins do Node)
|
|
25
|
+
const nodeBuiltIns = [
|
|
26
|
+
'assert', 'buffer', 'child_process', 'cluster', 'crypto', 'dgram', 'dns',
|
|
27
|
+
'domain', 'events', 'fs', 'http', 'https', 'net', 'os', 'path', 'punycode',
|
|
28
|
+
'querystring', 'readline', 'stream', 'string_decoder', 'tls', 'tty', 'url',
|
|
29
|
+
'util', 'v8', 'vm', 'zlib', 'module', 'worker_threads', 'perf_hooks'
|
|
30
|
+
];
|
|
31
|
+
/**
|
|
32
|
+
* Plugin para processar CSS com PostCSS e Tailwind
|
|
33
|
+
*/
|
|
34
|
+
const postcssPlugin = {
|
|
35
|
+
name: 'postcss-plugin',
|
|
36
|
+
setup(build) {
|
|
37
|
+
build.onLoad({ filter: /\.css$/ }, async (args) => {
|
|
38
|
+
const fs = require('fs');
|
|
39
|
+
const path = require('path');
|
|
40
|
+
try {
|
|
41
|
+
// Lê o CSS original
|
|
42
|
+
let css = await fs.promises.readFile(args.path, 'utf8');
|
|
43
|
+
// Verifica se tem PostCSS config no projeto
|
|
44
|
+
const projectDir = process.cwd();
|
|
45
|
+
const postcssConfigPath = path.join(projectDir, 'postcss.config.js');
|
|
46
|
+
const postcssConfigMjsPath = path.join(projectDir, 'postcss.config.mjs');
|
|
47
|
+
let processedCss = css;
|
|
48
|
+
if (fs.existsSync(postcssConfigPath) || fs.existsSync(postcssConfigMjsPath)) {
|
|
49
|
+
try {
|
|
50
|
+
// Importa PostCSS do projeto (não do SDK)
|
|
51
|
+
const postcssPath = path.join(projectDir, 'node_modules', 'postcss');
|
|
52
|
+
const postcss = require(postcssPath);
|
|
53
|
+
// Carrega a config do PostCSS
|
|
54
|
+
const configPath = fs.existsSync(postcssConfigPath) ? postcssConfigPath : postcssConfigMjsPath;
|
|
55
|
+
delete require.cache[require.resolve(configPath)];
|
|
56
|
+
const config = require(configPath);
|
|
57
|
+
const postcssConfig = config.default || config;
|
|
58
|
+
// Resolve plugins do projeto
|
|
59
|
+
const plugins = [];
|
|
60
|
+
if (postcssConfig.plugins) {
|
|
61
|
+
if (Array.isArray(postcssConfig.plugins)) {
|
|
62
|
+
// Formato array: ["@tailwindcss/postcss"]
|
|
63
|
+
plugins.push(...postcssConfig.plugins.map(plugin => {
|
|
64
|
+
if (typeof plugin === 'string') {
|
|
65
|
+
try {
|
|
66
|
+
// Tenta resolver do node_modules do projeto primeiro
|
|
67
|
+
return require.resolve(plugin, { paths: [projectDir] });
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
// Fallback para require direto
|
|
71
|
+
return require(plugin);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return plugin;
|
|
75
|
+
}));
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
// Formato objeto: { tailwindcss: {}, autoprefixer: {} }
|
|
79
|
+
for (const [pluginName, pluginOptions] of Object.entries(postcssConfig.plugins)) {
|
|
80
|
+
try {
|
|
81
|
+
// Resolve o plugin do projeto
|
|
82
|
+
const resolvedPath = require.resolve(pluginName, { paths: [projectDir] });
|
|
83
|
+
const pluginModule = require(resolvedPath);
|
|
84
|
+
plugins.push(pluginModule(pluginOptions || {}));
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
Console.warn(`Unable to load plugin ${pluginName}:`, error.message);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
// Processa o CSS com PostCSS
|
|
93
|
+
const result = await postcss(plugins)
|
|
94
|
+
.process(css, {
|
|
95
|
+
from: args.path,
|
|
96
|
+
to: args.path.replace(/\.css$/, '.processed.css')
|
|
97
|
+
});
|
|
98
|
+
processedCss = result.css;
|
|
99
|
+
}
|
|
100
|
+
catch (postcssError) {
|
|
101
|
+
Console.warn(`Error processing CSS with PostCSS:`, postcssError.message);
|
|
102
|
+
Console.warn(`Using raw CSS without processing.`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
contents: `
|
|
107
|
+
const css = ${JSON.stringify(processedCss)};
|
|
108
|
+
if (typeof document !== 'undefined') {
|
|
109
|
+
const style = document.createElement('style');
|
|
110
|
+
style.textContent = css;
|
|
111
|
+
document.head.appendChild(style);
|
|
112
|
+
}
|
|
113
|
+
export default css;
|
|
114
|
+
`,
|
|
115
|
+
loader: 'js'
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
catch (error) {
|
|
119
|
+
Console.error(`Erro ao processar CSS ${args.path}:`, error);
|
|
120
|
+
return {
|
|
121
|
+
contents: `export default "";`,
|
|
122
|
+
loader: 'js'
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
/**
|
|
129
|
+
* Plugin para resolver dependências npm no frontend
|
|
130
|
+
*/
|
|
131
|
+
const npmDependenciesPlugin = {
|
|
132
|
+
name: 'npm-dependencies',
|
|
133
|
+
setup(build) {
|
|
134
|
+
// Permite que dependências npm sejam bundladas (não marcadas como external)
|
|
135
|
+
build.onResolve({ filter: /^[^./]/ }, (args) => {
|
|
136
|
+
// Se for um módulo built-in do Node.js, marca como external
|
|
137
|
+
if (nodeBuiltIns.includes(args.path)) {
|
|
138
|
+
return { path: args.path, external: true };
|
|
139
|
+
}
|
|
140
|
+
// Para dependências npm (axios, lodash, react, react-dom, etc), deixa o esbuild resolver normalmente
|
|
141
|
+
// Isso permite que sejam bundladas no frontend
|
|
142
|
+
return null;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Plugin para garantir que React seja corretamente resolvido
|
|
148
|
+
*/
|
|
149
|
+
const reactResolvePlugin = {
|
|
150
|
+
name: 'react-resolve',
|
|
151
|
+
setup(build) {
|
|
152
|
+
// Força o uso de uma única instância do React do projeto
|
|
153
|
+
build.onResolve({ filter: /^react$/ }, (args) => {
|
|
154
|
+
const reactPath = require.resolve('react', { paths: [process.cwd()] });
|
|
155
|
+
return {
|
|
156
|
+
path: reactPath
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
build.onResolve({ filter: /^react-dom$/ }, (args) => {
|
|
160
|
+
const reactDomPath = require.resolve('react-dom', { paths: [process.cwd()] });
|
|
161
|
+
return {
|
|
162
|
+
path: reactDomPath
|
|
163
|
+
};
|
|
164
|
+
});
|
|
165
|
+
build.onResolve({ filter: /^react\/jsx-runtime$/ }, (args) => {
|
|
166
|
+
const jsxRuntimePath = require.resolve('react/jsx-runtime', { paths: [process.cwd()] });
|
|
167
|
+
return {
|
|
168
|
+
path: jsxRuntimePath
|
|
169
|
+
};
|
|
170
|
+
});
|
|
171
|
+
// Também resolve react-dom/client para React 18+
|
|
172
|
+
build.onResolve({ filter: /^react-dom\/client$/ }, (args) => {
|
|
173
|
+
const clientPath = require.resolve('react-dom/client', { paths: [process.cwd()] });
|
|
174
|
+
return {
|
|
175
|
+
path: clientPath
|
|
176
|
+
};
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
/**
|
|
181
|
+
* Plugin para adicionar suporte a HMR (Hot Module Replacement)
|
|
182
|
+
*/
|
|
183
|
+
const hmrPlugin = {
|
|
184
|
+
name: 'hmr-plugin',
|
|
185
|
+
setup(build) {
|
|
186
|
+
// Adiciona runtime de HMR para arquivos TSX/JSX
|
|
187
|
+
build.onLoad({ filter: /\.(tsx|jsx)$/ }, async (args) => {
|
|
188
|
+
// Ignora arquivos de node_modules
|
|
189
|
+
if (args.path.includes('node_modules')) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
const fs = require('fs');
|
|
193
|
+
const contents = await fs.promises.readFile(args.path, 'utf8');
|
|
194
|
+
// Adiciona código de HMR apenas em componentes de rota
|
|
195
|
+
if (args.path.includes('/routes/') || args.path.includes('\\routes\\')) {
|
|
196
|
+
const hmrCode = `
|
|
197
|
+
// HMR Runtime
|
|
198
|
+
if (typeof window !== 'undefined' && window.__HWEB_HMR__) {
|
|
199
|
+
const moduleId = ${JSON.stringify(args.path)};
|
|
200
|
+
if (!window.__HWEB_HMR_MODULES__) {
|
|
201
|
+
window.__HWEB_HMR_MODULES__ = new Map();
|
|
202
|
+
}
|
|
203
|
+
window.__HWEB_HMR_MODULES__.set(moduleId, module.exports);
|
|
204
|
+
}
|
|
205
|
+
`;
|
|
206
|
+
return {
|
|
207
|
+
contents: contents + '\n' + hmrCode,
|
|
208
|
+
loader: 'tsx'
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
return null;
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Plugin para suportar importação de arquivos Markdown (.md)
|
|
217
|
+
*/
|
|
218
|
+
const markdownPlugin = {
|
|
219
|
+
name: 'markdown-plugin',
|
|
220
|
+
setup(build) {
|
|
221
|
+
build.onLoad({ filter: /\.md$/ }, async (args) => {
|
|
222
|
+
const fs = require('fs');
|
|
223
|
+
const content = await fs.promises.readFile(args.path, 'utf8');
|
|
224
|
+
return {
|
|
225
|
+
contents: `export default ${JSON.stringify(content)};`,
|
|
226
|
+
loader: 'js'
|
|
227
|
+
};
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
/**
|
|
232
|
+
* Plugin para suportar importação de arquivos de imagem e outros assets
|
|
233
|
+
*/
|
|
234
|
+
const assetsPlugin = {
|
|
235
|
+
name: 'assets-plugin',
|
|
236
|
+
setup(build) {
|
|
237
|
+
// Suporte para imagens (PNG, JPG, JPEG, GIF, SVG, WEBP, etc)
|
|
238
|
+
build.onLoad({ filter: /\.(png|jpe?g|gif|webp|avif|ico|bmp|tiff?)$/i }, async (args) => {
|
|
239
|
+
const fs = require('fs');
|
|
240
|
+
const buffer = await fs.promises.readFile(args.path);
|
|
241
|
+
const base64 = buffer.toString('base64');
|
|
242
|
+
const ext = path.extname(args.path).slice(1).toLowerCase();
|
|
243
|
+
// Mapeamento de extensões para MIME types
|
|
244
|
+
const mimeTypes = {
|
|
245
|
+
'png': 'image/png',
|
|
246
|
+
'jpg': 'image/jpeg',
|
|
247
|
+
'jpeg': 'image/jpeg',
|
|
248
|
+
'gif': 'image/gif',
|
|
249
|
+
'webp': 'image/webp',
|
|
250
|
+
'avif': 'image/avif',
|
|
251
|
+
'ico': 'image/x-icon',
|
|
252
|
+
'bmp': 'image/bmp',
|
|
253
|
+
'tif': 'image/tiff',
|
|
254
|
+
'tiff': 'image/tiff'
|
|
255
|
+
};
|
|
256
|
+
const mimeType = mimeTypes[ext] || 'application/octet-stream';
|
|
257
|
+
return {
|
|
258
|
+
contents: `export default "data:${mimeType};base64,${base64}";`,
|
|
259
|
+
loader: 'js'
|
|
260
|
+
};
|
|
261
|
+
});
|
|
262
|
+
// Suporte especial para SVG (pode ser usado como string ou data URL)
|
|
263
|
+
build.onLoad({ filter: /\.svg$/i }, async (args) => {
|
|
264
|
+
const fs = require('fs');
|
|
265
|
+
const content = await fs.promises.readFile(args.path, 'utf8');
|
|
266
|
+
const base64 = Buffer.from(content).toString('base64');
|
|
267
|
+
return {
|
|
268
|
+
contents: `
|
|
269
|
+
export default "data:image/svg+xml;base64,${base64}";
|
|
270
|
+
export const svgContent = ${JSON.stringify(content)};
|
|
271
|
+
`,
|
|
272
|
+
loader: 'js'
|
|
273
|
+
};
|
|
274
|
+
});
|
|
275
|
+
// Suporte para arquivos JSON
|
|
276
|
+
build.onLoad({ filter: /\.json$/i }, async (args) => {
|
|
277
|
+
const fs = require('fs');
|
|
278
|
+
const content = await fs.promises.readFile(args.path, 'utf8');
|
|
279
|
+
return {
|
|
280
|
+
contents: `export default ${content};`,
|
|
281
|
+
loader: 'js'
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
// Suporte para arquivos de texto (.txt)
|
|
285
|
+
build.onLoad({ filter: /\.txt$/i }, async (args) => {
|
|
286
|
+
const fs = require('fs');
|
|
287
|
+
const content = await fs.promises.readFile(args.path, 'utf8');
|
|
288
|
+
return {
|
|
289
|
+
contents: `export default ${JSON.stringify(content)};`,
|
|
290
|
+
loader: 'js'
|
|
291
|
+
};
|
|
292
|
+
});
|
|
293
|
+
// Suporte para arquivos de fonte (WOFF, WOFF2, TTF, OTF, EOT)
|
|
294
|
+
build.onLoad({ filter: /\.(woff2?|ttf|otf|eot)$/i }, async (args) => {
|
|
295
|
+
const fs = require('fs');
|
|
296
|
+
const buffer = await fs.promises.readFile(args.path);
|
|
297
|
+
const base64 = buffer.toString('base64');
|
|
298
|
+
const ext = path.extname(args.path).slice(1).toLowerCase();
|
|
299
|
+
const mimeTypes = {
|
|
300
|
+
'woff': 'font/woff',
|
|
301
|
+
'woff2': 'font/woff2',
|
|
302
|
+
'ttf': 'font/ttf',
|
|
303
|
+
'otf': 'font/otf',
|
|
304
|
+
'eot': 'application/vnd.ms-fontobject'
|
|
305
|
+
};
|
|
306
|
+
const mimeType = mimeTypes[ext] || 'application/octet-stream';
|
|
307
|
+
return {
|
|
308
|
+
contents: `export default "data:${mimeType};base64,${base64}";`,
|
|
309
|
+
loader: 'js'
|
|
310
|
+
};
|
|
311
|
+
});
|
|
312
|
+
// Suporte para arquivos de áudio (MP3, WAV, OGG, etc)
|
|
313
|
+
build.onLoad({ filter: /\.(mp3|wav|ogg|m4a|aac|flac)$/i }, async (args) => {
|
|
314
|
+
const fs = require('fs');
|
|
315
|
+
const buffer = await fs.promises.readFile(args.path);
|
|
316
|
+
const base64 = buffer.toString('base64');
|
|
317
|
+
const ext = path.extname(args.path).slice(1).toLowerCase();
|
|
318
|
+
const mimeTypes = {
|
|
319
|
+
'mp3': 'audio/mpeg',
|
|
320
|
+
'wav': 'audio/wav',
|
|
321
|
+
'ogg': 'audio/ogg',
|
|
322
|
+
'm4a': 'audio/mp4',
|
|
323
|
+
'aac': 'audio/aac',
|
|
324
|
+
'flac': 'audio/flac'
|
|
325
|
+
};
|
|
326
|
+
const mimeType = mimeTypes[ext] || 'audio/mpeg';
|
|
327
|
+
return {
|
|
328
|
+
contents: `export default "data:${mimeType};base64,${base64}";`,
|
|
329
|
+
loader: 'js'
|
|
330
|
+
};
|
|
331
|
+
});
|
|
332
|
+
// Suporte para arquivos de vídeo (MP4, WEBM, OGV)
|
|
333
|
+
build.onLoad({ filter: /\.(mp4|webm|ogv)$/i }, async (args) => {
|
|
334
|
+
const fs = require('fs');
|
|
335
|
+
const buffer = await fs.promises.readFile(args.path);
|
|
336
|
+
const base64 = buffer.toString('base64');
|
|
337
|
+
const ext = path.extname(args.path).slice(1).toLowerCase();
|
|
338
|
+
const mimeTypes = {
|
|
339
|
+
'mp4': 'video/mp4',
|
|
340
|
+
'webm': 'video/webm',
|
|
341
|
+
'ogv': 'video/ogg'
|
|
342
|
+
};
|
|
343
|
+
const mimeType = mimeTypes[ext] || 'video/mp4';
|
|
344
|
+
return {
|
|
345
|
+
contents: `export default "data:${mimeType};base64,${base64}";`,
|
|
346
|
+
loader: 'js'
|
|
347
|
+
};
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
/**
|
|
352
|
+
* Builds with code splitting into multiple chunks based on module types.
|
|
353
|
+
* @param {string} entryPoint - The path to the entry file.
|
|
354
|
+
* @param {string} outdir - The directory for output files.
|
|
355
|
+
* @param {boolean} isProduction - Se está em modo produção ou não.
|
|
356
|
+
* @returns {Promise<void>}
|
|
357
|
+
*/
|
|
358
|
+
async function buildWithChunks(entryPoint, outdir, isProduction = false) {
|
|
359
|
+
// limpar diretorio, menos a pasta temp
|
|
360
|
+
await cleanDirectoryExcept(outdir, 'temp');
|
|
361
|
+
try {
|
|
362
|
+
await esbuild.build({
|
|
363
|
+
entryPoints: [entryPoint],
|
|
364
|
+
bundle: true,
|
|
365
|
+
minify: isProduction,
|
|
366
|
+
sourcemap: !isProduction,
|
|
367
|
+
platform: 'browser',
|
|
368
|
+
outdir: outdir,
|
|
369
|
+
loader: { '.js': 'js', '.ts': 'tsx' },
|
|
370
|
+
external: nodeBuiltIns,
|
|
371
|
+
plugins: [postcssPlugin, npmDependenciesPlugin, reactResolvePlugin, markdownPlugin, assetsPlugin],
|
|
372
|
+
format: 'esm', // ESM suporta melhor o code splitting
|
|
373
|
+
jsx: 'automatic',
|
|
374
|
+
define: {
|
|
375
|
+
'process.env.NODE_ENV': isProduction ? '"production"' : '"development"'
|
|
376
|
+
},
|
|
377
|
+
conditions: ['development'],
|
|
378
|
+
mainFields: ['browser', 'module', 'main'],
|
|
379
|
+
resolveExtensions: ['.tsx', '.ts', '.js'],
|
|
380
|
+
splitting: true,
|
|
381
|
+
chunkNames: 'chunks/[name]-[hash]',
|
|
382
|
+
// Força o nome do entry para main(.js) ou main-[hash].js em prod
|
|
383
|
+
entryNames: isProduction ? 'main-[hash]' : 'main',
|
|
384
|
+
keepNames: true,
|
|
385
|
+
treeShaking: true,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
catch (error) {
|
|
389
|
+
console.error('An error occurred while building:', error);
|
|
390
|
+
process.exit(1);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Watches with code splitting enabled
|
|
395
|
+
* @param {string} entryPoint - The path to the entry file.
|
|
396
|
+
* @param {string} outdir - The directory for output files.
|
|
397
|
+
* @param {Object} hotReloadManager - Manager de hot reload (opcional).
|
|
398
|
+
* @returns {Promise<void>}
|
|
399
|
+
*/
|
|
400
|
+
async function watchWithChunks(entryPoint, outdir, hotReloadManager = null) {
|
|
401
|
+
// limpar diretorio
|
|
402
|
+
await cleanDirectoryExcept(outdir, 'temp');
|
|
403
|
+
try {
|
|
404
|
+
// Plugin para notificar quando o build termina
|
|
405
|
+
const buildCompletePlugin = {
|
|
406
|
+
name: 'build-complete',
|
|
407
|
+
setup(build) {
|
|
408
|
+
let isFirstBuild = true;
|
|
409
|
+
build.onEnd((result) => {
|
|
410
|
+
if (hotReloadManager) {
|
|
411
|
+
if (isFirstBuild) {
|
|
412
|
+
isFirstBuild = false;
|
|
413
|
+
hotReloadManager.onBuildComplete(true);
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
// Notifica o hot reload manager que o build foi concluído
|
|
417
|
+
hotReloadManager.onBuildComplete(result.errors.length === 0);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
const context = await esbuild.context({
|
|
424
|
+
entryPoints: [entryPoint],
|
|
425
|
+
bundle: true,
|
|
426
|
+
minify: false,
|
|
427
|
+
sourcemap: true,
|
|
428
|
+
platform: 'browser',
|
|
429
|
+
outdir: outdir,
|
|
430
|
+
loader: { '.js': 'js', '.ts': 'tsx' },
|
|
431
|
+
external: nodeBuiltIns,
|
|
432
|
+
plugins: [postcssPlugin, npmDependenciesPlugin, reactResolvePlugin, hmrPlugin, buildCompletePlugin, markdownPlugin, assetsPlugin],
|
|
433
|
+
format: 'esm',
|
|
434
|
+
jsx: 'automatic',
|
|
435
|
+
define: {
|
|
436
|
+
'process.env.NODE_ENV': '"development"'
|
|
437
|
+
},
|
|
438
|
+
conditions: ['development'],
|
|
439
|
+
mainFields: ['browser', 'module', 'main'],
|
|
440
|
+
resolveExtensions: ['.tsx', '.ts', '.js'],
|
|
441
|
+
splitting: true,
|
|
442
|
+
chunkNames: 'chunks/[name]-[hash]',
|
|
443
|
+
entryNames: 'main',
|
|
444
|
+
keepNames: true,
|
|
445
|
+
treeShaking: true,
|
|
446
|
+
});
|
|
447
|
+
await context.watch();
|
|
448
|
+
}
|
|
449
|
+
catch (error) {
|
|
450
|
+
console.error(error);
|
|
451
|
+
Console.error('Error starting watch mode with chunks:', error);
|
|
452
|
+
throw error;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Builds a single entry point into a single output file.
|
|
457
|
+
* @param {string} entryPoint - The path to the entry file.
|
|
458
|
+
* @param {string} outfile - The full path to the output file.
|
|
459
|
+
* @param {boolean} isProduction - Se está em modo produção ou não.
|
|
460
|
+
* @returns {Promise<void>}
|
|
461
|
+
*/
|
|
462
|
+
async function build(entryPoint, outfile, isProduction = false) {
|
|
463
|
+
// limpar diretorio do outfile
|
|
464
|
+
const outdir = path.dirname(outfile);
|
|
465
|
+
await cleanDirectoryExcept(outdir, 'temp');
|
|
466
|
+
try {
|
|
467
|
+
await esbuild.build({
|
|
468
|
+
entryPoints: [entryPoint],
|
|
469
|
+
bundle: true,
|
|
470
|
+
minify: isProduction,
|
|
471
|
+
sourcemap: !isProduction, // Só gera sourcemap em dev
|
|
472
|
+
platform: 'browser',
|
|
473
|
+
outfile: outfile,
|
|
474
|
+
loader: { '.js': 'js', '.ts': 'tsx' },
|
|
475
|
+
external: nodeBuiltIns,
|
|
476
|
+
plugins: [postcssPlugin, npmDependenciesPlugin, reactResolvePlugin, markdownPlugin, assetsPlugin],
|
|
477
|
+
format: 'iife',
|
|
478
|
+
globalName: 'HwebApp',
|
|
479
|
+
jsx: 'automatic',
|
|
480
|
+
define: {
|
|
481
|
+
'process.env.NODE_ENV': isProduction ? '"production"' : '"development"'
|
|
482
|
+
},
|
|
483
|
+
// Configurações específicas para React 19
|
|
484
|
+
conditions: ['development'],
|
|
485
|
+
mainFields: ['browser', 'module', 'main'],
|
|
486
|
+
resolveExtensions: ['.tsx', '.ts', '.js'],
|
|
487
|
+
// Garante que não há duplicação de dependências
|
|
488
|
+
splitting: false,
|
|
489
|
+
// Preserva nomes de funções e comportamento
|
|
490
|
+
keepNames: true,
|
|
491
|
+
// Remove otimizações que podem causar problemas com hooks
|
|
492
|
+
treeShaking: true,
|
|
493
|
+
drop: [], // Não remove nada automaticamente
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
catch (error) {
|
|
497
|
+
console.error('An error occurred during build:', error);
|
|
498
|
+
process.exit(1);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* Watches an entry point and its dependencies, rebuilding to a single output file.
|
|
503
|
+
* @param {string} entryPoint - The path to the entry file.
|
|
504
|
+
* @param {string} outfile - The full path to the output file.
|
|
505
|
+
* @param {Object} hotReloadManager - Manager de hot reload (opcional).
|
|
506
|
+
* @returns {Promise<void>}
|
|
507
|
+
*/
|
|
508
|
+
async function watch(entryPoint, outfile, hotReloadManager = null) {
|
|
509
|
+
try {
|
|
510
|
+
// Plugin para notificar quando o build termina
|
|
511
|
+
const buildCompletePlugin = {
|
|
512
|
+
name: 'build-complete',
|
|
513
|
+
setup(build) {
|
|
514
|
+
let isFirstBuild = true;
|
|
515
|
+
build.onEnd((result) => {
|
|
516
|
+
if (hotReloadManager) {
|
|
517
|
+
if (isFirstBuild) {
|
|
518
|
+
isFirstBuild = false;
|
|
519
|
+
hotReloadManager.onBuildComplete(true);
|
|
520
|
+
}
|
|
521
|
+
else {
|
|
522
|
+
// Notifica o hot reload manager que o build foi concluído
|
|
523
|
+
hotReloadManager.onBuildComplete(result.errors.length === 0);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
};
|
|
529
|
+
const context = await esbuild.context({
|
|
530
|
+
entryPoints: [entryPoint],
|
|
531
|
+
bundle: true,
|
|
532
|
+
minify: false,
|
|
533
|
+
sourcemap: true,
|
|
534
|
+
platform: 'browser',
|
|
535
|
+
outfile: outfile,
|
|
536
|
+
loader: { '.js': 'js', '.ts': 'tsx' },
|
|
537
|
+
external: nodeBuiltIns,
|
|
538
|
+
format: 'iife',
|
|
539
|
+
globalName: 'HwebApp',
|
|
540
|
+
jsx: 'automatic',
|
|
541
|
+
define: {
|
|
542
|
+
'process.env.NODE_ENV': '"development"'
|
|
543
|
+
},
|
|
544
|
+
// Configurações específicas para React 19 (mesmo que no build)
|
|
545
|
+
conditions: ['development'],
|
|
546
|
+
mainFields: ['browser', 'module', 'main'],
|
|
547
|
+
resolveExtensions: ['.tsx', '.ts', '.js'],
|
|
548
|
+
// Garante que não há duplicação de dependências
|
|
549
|
+
splitting: false,
|
|
550
|
+
// Preserva nomes de funções e comportamento
|
|
551
|
+
keepNames: true,
|
|
552
|
+
// Remove otimizações que podem causar problemas com hooks
|
|
553
|
+
treeShaking: true,
|
|
554
|
+
});
|
|
555
|
+
// Configura o watcher do esbuild
|
|
556
|
+
await context.watch();
|
|
557
|
+
}
|
|
558
|
+
catch (error) {
|
|
559
|
+
Console.error('Error starting watch mode:', error);
|
|
560
|
+
throw error;
|
|
561
|
+
}
|
|
562
|
+
}
|
|
563
|
+
/**
|
|
564
|
+
* Remove todo o conteúdo de um diretório,
|
|
565
|
+
* exceto a pasta (ou pastas) especificada(s) em excludeFolder.
|
|
566
|
+
*
|
|
567
|
+
* @param {string} dirPath - Caminho do diretório a limpar
|
|
568
|
+
* @param {string|string[]} excludeFolder - Nome ou nomes das pastas a manter
|
|
569
|
+
*/
|
|
570
|
+
async function cleanDirectoryExcept(dirPath, excludeFolder) {
|
|
571
|
+
const excludes = Array.isArray(excludeFolder) ? excludeFolder : [excludeFolder];
|
|
572
|
+
const items = await readdir(dirPath);
|
|
573
|
+
for (const item of items) {
|
|
574
|
+
if (excludes.includes(item))
|
|
575
|
+
continue; // pula as pastas excluídas
|
|
576
|
+
const itemPath = path.join(dirPath, item);
|
|
577
|
+
const info = await stat(itemPath);
|
|
578
|
+
await rm(itemPath, { recursive: info.isDirectory(), force: true });
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
module.exports = { build, watch, buildWithChunks, watchWithChunks };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function App(): import("react/jsx-runtime").JSX.Element;
|