gofront 1.2.1 → 1.2.2
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/README.md +1 -114
- package/package.json +1 -1
- package/src/codegen/index.js +15 -2
- package/src/codegen/statements.js +4 -1
- package/src/minifier.js +19 -7
package/README.md
CHANGED
|
@@ -387,16 +387,13 @@ gofront <dir> compile all *.go in directory → stdout
|
|
|
387
387
|
gofront <input> -o out.js write output to file (prints elapsed compile time e.g. "15ms")
|
|
388
388
|
gofront <input> --check type-check only
|
|
389
389
|
gofront <input> --watch watch for changes and recompile
|
|
390
|
-
gofront <input> -o out.js --serve watch + serve with live reload
|
|
390
|
+
gofront <input> -o out.js --serve watch + serve with live reload (default port 3000)
|
|
391
391
|
gofront <input> -o out.js --serve --port 8080 use a custom port
|
|
392
|
-
gofront <input> -o out.js --copy-assets compile and synchronize static assets from package.json assetCopy
|
|
393
392
|
gofront <input> --source-map append inline source map (single file or directory; multi-file packages emit per-file mappings)
|
|
394
393
|
gofront <input> --minify minify output (built-in minifier)
|
|
395
394
|
gofront <input> --minify --mangle minify and rename local identifiers
|
|
396
395
|
gofront <file.go> --ast dump AST (debug)
|
|
397
396
|
gofront <file.go> --tokens dump tokens (debug)
|
|
398
|
-
gofront test [dir] [--dom] run unit tests (-v verbose, -run <regex>)
|
|
399
|
-
gofront prep [dir] copy static assets and bundle vendor dependencies
|
|
400
397
|
gofront init [dir] scaffold a new project
|
|
401
398
|
gofront --version / -v print version
|
|
402
399
|
gofront --help / -h print this help
|
|
@@ -404,116 +401,6 @@ gofront --help / -h print this help
|
|
|
404
401
|
|
|
405
402
|
---
|
|
406
403
|
|
|
407
|
-
## Unit testing (`gofront test`)
|
|
408
|
-
|
|
409
|
-
GoFront provides native, Go-idiomatic unit testing out of the box:
|
|
410
|
-
|
|
411
|
-
- Files ending in `*_test.go` are automatically excluded from production builds (`gofront .`, `gofront -o app.js`) and compiled only when running tests.
|
|
412
|
-
- Built-in `testing` standard library package with `*testing.T`.
|
|
413
|
-
- Discover and execute `func TestXxx(t *testing.T)` functions with standard Go-style output.
|
|
414
|
-
|
|
415
|
-
### Writing a test
|
|
416
|
-
|
|
417
|
-
```go
|
|
418
|
-
package store
|
|
419
|
-
|
|
420
|
-
import "testing"
|
|
421
|
-
|
|
422
|
-
func TestAddTodo(t *testing.T) {
|
|
423
|
-
store := NewTodoStore()
|
|
424
|
-
store.Add("Buy milk")
|
|
425
|
-
|
|
426
|
-
if store.Count() != 1 {
|
|
427
|
-
t.Errorf("expected 1 item, got %d", store.Count())
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
```
|
|
431
|
-
|
|
432
|
-
Subtests and skips are fully supported:
|
|
433
|
-
|
|
434
|
-
```go
|
|
435
|
-
func TestFilter(t *testing.T) {
|
|
436
|
-
t.Run("active", func(t *testing.T) {
|
|
437
|
-
// subtest logic
|
|
438
|
-
})
|
|
439
|
-
t.Run("completed", func(t *testing.T) {
|
|
440
|
-
if testing.Short() {
|
|
441
|
-
t.Skip("skipping in short mode")
|
|
442
|
-
}
|
|
443
|
-
})
|
|
444
|
-
}
|
|
445
|
-
```
|
|
446
|
-
|
|
447
|
-
### Running tests
|
|
448
|
-
|
|
449
|
-
```sh
|
|
450
|
-
gofront test # run tests in current directory
|
|
451
|
-
gofront test ./src # run tests in specific directory
|
|
452
|
-
gofront test -v # verbose output (RUN, PASS/FAIL, logs)
|
|
453
|
-
gofront test -run "Todo" # run only tests matching regex pattern
|
|
454
|
-
gofront test --dom # initialize JSDOM for DOM/gom/templ tests
|
|
455
|
-
```
|
|
456
|
-
|
|
457
|
-
`--dom` requires `jsdom`. It is resolved from the project directory first and
|
|
458
|
-
then from GoFront's own installation, so a globally installed `gofront` works
|
|
459
|
-
without a project-local copy.
|
|
460
|
-
|
|
461
|
-
Only `TestXxx` functions declared in `*_test.go` files are discovered, matching Go.
|
|
462
|
-
|
|
463
|
-
---
|
|
464
|
-
|
|
465
|
-
## Static assets & vendor bundling
|
|
466
|
-
|
|
467
|
-
GoFront includes native tooling to manage static assets and package external npm dependencies for the browser without requiring external wrappers or complex build tools.
|
|
468
|
-
|
|
469
|
-
### Asset copying (`assetCopy`)
|
|
470
|
-
|
|
471
|
-
Specify static assets (fonts, stylesheets, images) in `package.json` (or `gofront.json`):
|
|
472
|
-
|
|
473
|
-
```json
|
|
474
|
-
{
|
|
475
|
-
"assetCopy": [
|
|
476
|
-
{ "source": "node_modules/@fontsource/raleway/files", "dest": "app/fonts" },
|
|
477
|
-
{ "source": "node_modules/prismjs/themes", "dest": "app/css/prism-themes" }
|
|
478
|
-
]
|
|
479
|
-
}
|
|
480
|
-
```
|
|
481
|
-
|
|
482
|
-
Run `gofront prep` to copy them, or pass `--copy-assets` to automatically synchronize assets on build.
|
|
483
|
-
|
|
484
|
-
### Vendor bundling (`gofront prep`)
|
|
485
|
-
|
|
486
|
-
Package external npm dependencies for the browser:
|
|
487
|
-
|
|
488
|
-
```sh
|
|
489
|
-
gofront prep
|
|
490
|
-
```
|
|
491
|
-
|
|
492
|
-
GoFront inspects your `dependencies`, dynamically uses `rolldown` or `esbuild` from your `devDependencies`, and bundles them into an ES module (defaulting to `app/vendor.js` or `vendor.js`).
|
|
493
|
-
|
|
494
|
-
Each bundled package is exposed on `window` under its full name, its unscoped name,
|
|
495
|
-
and a sanitised identifier (e.g. `fuse.js` → `window["fuse.js"]`, `window.fuse_js`).
|
|
496
|
-
To expose additional global names, add a `vendor.globals` mapping:
|
|
497
|
-
|
|
498
|
-
```json
|
|
499
|
-
{
|
|
500
|
-
"vendor": {
|
|
501
|
-
"dest": ["app/vendor.js", "public/vendor.js"],
|
|
502
|
-
"minify": true,
|
|
503
|
-
"globals": {
|
|
504
|
-
"fuse.js": ["Fuse"],
|
|
505
|
-
"prismjs": ["Prism"],
|
|
506
|
-
"@emailjs/browser": ["emailjs"]
|
|
507
|
-
}
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
```
|
|
511
|
-
|
|
512
|
-
`dest` accepts a string or an array (the bundle is written once and copied to each
|
|
513
|
-
extra destination). `gofront prep --minify` overrides `minify` from the command line.
|
|
514
|
-
|
|
515
|
-
---
|
|
516
|
-
|
|
517
404
|
## Multi-file packages
|
|
518
405
|
|
|
519
406
|
All `.go` files in a directory share the same namespace and are compiled as one unit.
|
package/package.json
CHANGED
package/src/codegen/index.js
CHANGED
|
@@ -333,10 +333,23 @@ export class CodeGen {
|
|
|
333
333
|
if (fields.length === 0) {
|
|
334
334
|
this.line("constructor() {}");
|
|
335
335
|
} else {
|
|
336
|
-
|
|
336
|
+
// A field named like a type used in a default (e.g. `Project Project`)
|
|
337
|
+
// would shadow that class inside the parameter scope, so alias it.
|
|
338
|
+
const binding = (f) => {
|
|
339
|
+
const re = new RegExp(`\\b${f.name}\\b`);
|
|
340
|
+
return fields.some((o) => re.test(o.zero)) ? `${f.name}$` : f.name;
|
|
341
|
+
};
|
|
342
|
+
const params = fields
|
|
343
|
+
.map((f) => {
|
|
344
|
+
const b = binding(f);
|
|
345
|
+
return b === f.name
|
|
346
|
+
? `${f.name} = ${f.zero}`
|
|
347
|
+
: `${f.name}: ${b} = ${f.zero}`;
|
|
348
|
+
})
|
|
349
|
+
.join(", ");
|
|
337
350
|
this.line(`constructor({ ${params} } = {}) {`);
|
|
338
351
|
this.indented(() => {
|
|
339
|
-
for (const f of fields) this.line(`this.${f.name} = ${f
|
|
352
|
+
for (const f of fields) this.line(`this.${f.name} = ${binding(f)};`);
|
|
340
353
|
});
|
|
341
354
|
this.line("}");
|
|
342
355
|
}
|
|
@@ -448,7 +448,10 @@ export const statementGenMethods = {
|
|
|
448
448
|
},
|
|
449
449
|
|
|
450
450
|
_genRangeIterExpr(iterType, iteree, lhs) {
|
|
451
|
-
if (this._isMapRangeType(iterType))
|
|
451
|
+
if (this._isMapRangeType(iterType)) {
|
|
452
|
+
if (lhs.length === 1) return `Object.keys(${iteree})`;
|
|
453
|
+
return `Object.entries(${iteree})`;
|
|
454
|
+
}
|
|
452
455
|
if (this._isStringRangeType(iterType)) {
|
|
453
456
|
if (lhs.length === 1) return `Array.from(${iteree}).keys()`;
|
|
454
457
|
return `Array.from(${iteree}, (__c, __i) => [__i, __c.codePointAt(0)])`;
|
package/src/minifier.js
CHANGED
|
@@ -545,10 +545,18 @@ function _applyRenaming(tokens, renameMap) {
|
|
|
545
545
|
|
|
546
546
|
function mangle(code) {
|
|
547
547
|
const tokens = tokenize(code);
|
|
548
|
-
const nameGen = shortNameGeneratorFn();
|
|
549
|
-
const renameMap = new Map();
|
|
550
548
|
const { declaredLocals } = _collectLocalDecls(tokens);
|
|
551
549
|
_filterRenameCandidates(declaredLocals);
|
|
550
|
+
|
|
551
|
+
const occupied = new Set(MANGLE_GLOBALS);
|
|
552
|
+
for (const tok of tokens) {
|
|
553
|
+
if (tok.type === "ident" && !declaredLocals.has(tok.value)) {
|
|
554
|
+
occupied.add(tok.value);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
const nameGen = shortNameGeneratorFn(occupied);
|
|
559
|
+
const renameMap = new Map();
|
|
552
560
|
for (const name of declaredLocals) renameMap.set(name, nameGen());
|
|
553
561
|
return _applyRenaming(tokens, renameMap);
|
|
554
562
|
}
|
|
@@ -606,23 +614,27 @@ function collectParams(tokens, openIdx, locals) {
|
|
|
606
614
|
}
|
|
607
615
|
}
|
|
608
616
|
|
|
609
|
-
function* shortNameGenerator() {
|
|
617
|
+
function* shortNameGenerator(occupied = new Set()) {
|
|
610
618
|
const chars = "abcdefghijklmnopqrstuvwxyz";
|
|
611
619
|
let index = 0;
|
|
612
620
|
while (true) {
|
|
621
|
+
let name;
|
|
613
622
|
if (index < 26) {
|
|
614
|
-
|
|
623
|
+
name = chars[index++];
|
|
615
624
|
} else {
|
|
616
625
|
const base = index - 26;
|
|
617
|
-
|
|
626
|
+
name = chars[base % 26] + (Math.floor(base / 26) + 1);
|
|
618
627
|
index++;
|
|
619
628
|
}
|
|
629
|
+
if (!occupied.has(name)) {
|
|
630
|
+
yield name;
|
|
631
|
+
}
|
|
620
632
|
}
|
|
621
633
|
}
|
|
622
634
|
|
|
623
635
|
// Make it callable (not an iterator interface)
|
|
624
|
-
function shortNameGeneratorFn() {
|
|
625
|
-
const gen = shortNameGenerator();
|
|
636
|
+
function shortNameGeneratorFn(occupied = new Set()) {
|
|
637
|
+
const gen = shortNameGenerator(occupied);
|
|
626
638
|
return () => gen.next().value;
|
|
627
639
|
}
|
|
628
640
|
|