@sprlab/wccompiler 0.11.4 → 0.11.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/bin/wcc.js +5 -0
- package/lib/compiler.js +16 -0
- package/lib/config.js +6 -1
- package/lib/parser-extractors.js +8 -0
- package/package.json +1 -1
package/bin/wcc.js
CHANGED
|
@@ -35,6 +35,7 @@ async function build(config, cwd) {
|
|
|
35
35
|
|
|
36
36
|
const { code, usesSharedRuntime } = await compile(file, {
|
|
37
37
|
standalone: config.standalone,
|
|
38
|
+
minify: config.minify,
|
|
38
39
|
runtimeImportPath,
|
|
39
40
|
});
|
|
40
41
|
|
|
@@ -209,6 +210,9 @@ async function main() {
|
|
|
209
210
|
const cwd = process.cwd();
|
|
210
211
|
const config = await loadConfig(cwd);
|
|
211
212
|
|
|
213
|
+
// CLI flags override config
|
|
214
|
+
if (process.argv.includes('--minify')) config.minify = true;
|
|
215
|
+
|
|
212
216
|
if (command === 'build') {
|
|
213
217
|
const errors = await build(config, cwd);
|
|
214
218
|
if (errors > 0) process.exit(1);
|
|
@@ -236,6 +240,7 @@ async function main() {
|
|
|
236
240
|
|
|
237
241
|
const { code, usesSharedRuntime } = await compile(filePath, {
|
|
238
242
|
standalone: config.standalone,
|
|
243
|
+
minify: config.minify,
|
|
239
244
|
runtimeImportPath,
|
|
240
245
|
});
|
|
241
246
|
|
package/lib/compiler.js
CHANGED
|
@@ -485,5 +485,21 @@ export function resolveStandalone(componentValue, globalValue) {
|
|
|
485
485
|
*/
|
|
486
486
|
export async function compile(filePath, config) {
|
|
487
487
|
const result = await compileSFC(filePath, config);
|
|
488
|
+
|
|
489
|
+
if (config?.minify) {
|
|
490
|
+
const { transform } = await import('esbuild');
|
|
491
|
+
try {
|
|
492
|
+
const minified = await transform(result.code, {
|
|
493
|
+
minify: true,
|
|
494
|
+
loader: 'js',
|
|
495
|
+
target: 'esnext',
|
|
496
|
+
});
|
|
497
|
+
result.code = minified.code;
|
|
498
|
+
} catch {
|
|
499
|
+
// If minification fails (e.g., edge-case syntax), return unminified code
|
|
500
|
+
// This is a graceful fallback — the code still works at runtime
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
488
504
|
return result;
|
|
489
505
|
}
|
package/lib/config.js
CHANGED
|
@@ -19,7 +19,7 @@ import { pathToFileURL } from 'node:url';
|
|
|
19
19
|
* @returns {Promise<WccConfig>}
|
|
20
20
|
*/
|
|
21
21
|
export async function loadConfig(projectRoot) {
|
|
22
|
-
const defaults = { port: 4100, input: 'src', output: 'dist', standalone: false };
|
|
22
|
+
const defaults = { port: 4100, input: 'src', output: 'dist', standalone: false, minify: false };
|
|
23
23
|
const configPath = resolve(projectRoot, 'wcc.config.js');
|
|
24
24
|
|
|
25
25
|
if (!existsSync(configPath)) return defaults;
|
|
@@ -56,6 +56,11 @@ export async function loadConfig(projectRoot) {
|
|
|
56
56
|
error.code = 'INVALID_CONFIG';
|
|
57
57
|
throw error;
|
|
58
58
|
}
|
|
59
|
+
if (typeof config.minify !== 'boolean') {
|
|
60
|
+
const error = new Error(`Error en wcc.config.js: minify debe ser un booleano`);
|
|
61
|
+
error.code = 'INVALID_CONFIG';
|
|
62
|
+
throw error;
|
|
63
|
+
}
|
|
59
64
|
|
|
60
65
|
return config;
|
|
61
66
|
}
|
package/lib/parser-extractors.js
CHANGED
|
@@ -890,6 +890,14 @@ export function extractFunctions(source) {
|
|
|
890
890
|
if (j === i) {
|
|
891
891
|
// First line: capture everything after the opening brace
|
|
892
892
|
const afterBrace = l.substring(l.indexOf('{') + 1);
|
|
893
|
+
// Single-line function: depth already closed on first line
|
|
894
|
+
if (depth <= 0) {
|
|
895
|
+
const lastBraceIdx = afterBrace.lastIndexOf('}');
|
|
896
|
+
const inner = lastBraceIdx >= 0 ? afterBrace.substring(0, lastBraceIdx) : afterBrace;
|
|
897
|
+
if (inner.trim()) bodyLines.push(inner);
|
|
898
|
+
i = j;
|
|
899
|
+
break;
|
|
900
|
+
}
|
|
893
901
|
if (afterBrace.trim()) bodyLines.push(afterBrace);
|
|
894
902
|
} else if (depth <= 0) {
|
|
895
903
|
// Last line: capture everything before the closing brace
|
package/package.json
CHANGED