-
Notifications
You must be signed in to change notification settings - Fork 660
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #463 from trheyi/main
[add] Page Builer Component (30%)
- Loading branch information
Showing
12 changed files
with
598 additions
and
323 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/evanw/esbuild/pkg/api" | ||
"github.com/yaoapp/gou/runtime/transform" | ||
) | ||
|
||
// Compile compile the component | ||
func (component *Component) Compile() (string, error) { | ||
|
||
// Typescript is the default language | ||
// Typescript | ||
if component.Codes.TS.Code != "" { | ||
varName := strings.Replace(component.ID, "-", "_", -1) | ||
ts := strings.Replace(component.Codes.TS.Code, "export default", fmt.Sprintf("window.component__%s =", varName), 1) | ||
if component.Codes.HTML.Code != "" && !strings.Contains(component.Codes.TS.Code, "content:") { | ||
html := strings.ReplaceAll(component.Codes.HTML.Code, "`", "\\`") | ||
ts = strings.Replace(ts, "defaults: {", "defaults: {\n components: `"+html+"`,", 1) | ||
|
||
} | ||
|
||
js, err := transform.TypeScript(ts, api.TransformOptions{ | ||
Target: api.ESNext, | ||
MinifyWhitespace: true, | ||
MinifyIdentifiers: true, | ||
MinifySyntax: true, | ||
}) | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
component.Compiled = js | ||
return js, nil | ||
} | ||
|
||
// Javascript | ||
if component.Codes.JS.Code == "" { | ||
return "", fmt.Errorf("Block %s has no JS code", component.ID) | ||
} | ||
|
||
varName := strings.Replace(component.ID, "-", "_", -1) | ||
js := strings.Replace(component.Codes.JS.Code, "export default", fmt.Sprintf("window.component__%s =", varName), 1) | ||
if component.Codes.HTML.Code != "" && !strings.Contains(component.Codes.JS.Code, "content:") { | ||
html := strings.ReplaceAll(component.Codes.HTML.Code, "`", "\\`") | ||
js = strings.Replace(js, "defaults: {", "defaults: {\n components: `"+html+"`,", 1) | ||
} | ||
|
||
minified, err := transform.MinifyJS(js) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
component.Compiled = minified | ||
return minified, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package local | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTemplateBlocks(t *testing.T) { | ||
tests := prepare(t) | ||
defer clean() | ||
|
||
tmpl, err := tests.Demo.GetTemplate("tech-blue") | ||
if err != nil { | ||
t.Fatalf("GetTemplate error: %v", err) | ||
} | ||
|
||
blocks, err := tmpl.Blocks() | ||
if err != nil { | ||
t.Fatalf("Blocks error: %v", err) | ||
} | ||
|
||
if len(blocks) < 3 { | ||
t.Fatalf("Blocks error: %v", len(blocks)) | ||
} | ||
|
||
assert.Equal(t, "ColumnsTwo", blocks[0].(*Block).ID) | ||
assert.Equal(t, "/ColumnsTwo/ColumnsTwo.html", blocks[0].(*Block).Codes.HTML.File) | ||
assert.Equal(t, "/ColumnsTwo/ColumnsTwo.js", blocks[0].(*Block).Codes.JS.File) | ||
assert.Equal(t, "/ColumnsTwo/ColumnsTwo.ts", blocks[0].(*Block).Codes.TS.File) | ||
|
||
assert.Equal(t, "Hero", blocks[1].(*Block).ID) | ||
assert.Equal(t, "/Hero/Hero.html", blocks[1].(*Block).Codes.HTML.File) | ||
assert.Equal(t, "/Hero/Hero.js", blocks[1].(*Block).Codes.JS.File) | ||
assert.Equal(t, "/Hero/Hero.ts", blocks[1].(*Block).Codes.TS.File) | ||
|
||
assert.Equal(t, "Section", blocks[2].(*Block).ID) | ||
assert.Equal(t, "/Section/Section.html", blocks[2].(*Block).Codes.HTML.File) | ||
assert.Equal(t, "/Section/Section.js", blocks[2].(*Block).Codes.JS.File) | ||
assert.Equal(t, "/Section/Section.ts", blocks[2].(*Block).Codes.TS.File) | ||
} | ||
|
||
func TestTemplateBlockJS(t *testing.T) { | ||
tests := prepare(t) | ||
defer clean() | ||
|
||
tmpl, err := tests.Demo.GetTemplate("tech-blue") | ||
if err != nil { | ||
t.Fatalf("GetTemplate error: %v", err) | ||
} | ||
|
||
block, err := tmpl.Block("ColumnsTwo") | ||
if err != nil { | ||
t.Fatalf("Blocks error: %v", err) | ||
} | ||
|
||
assert.Equal(t, "ColumnsTwo", block.(*Block).ID) | ||
assert.NotEmpty(t, block.(*Block).Codes.HTML.Code) | ||
assert.NotEmpty(t, block.(*Block).Codes.JS.Code) | ||
assert.Contains(t, block.(*Block).Compiled, "window.block__ColumnsTwo") | ||
assert.Contains(t, block.(*Block).Compiled, `<div class="columns-two-left"`) | ||
} | ||
|
||
func TestTemplateBlockTS(t *testing.T) { | ||
tests := prepare(t) | ||
defer clean() | ||
|
||
tmpl, err := tests.Demo.GetTemplate("tech-blue") | ||
if err != nil { | ||
t.Fatalf("GetTemplate error: %v", err) | ||
} | ||
|
||
block, err := tmpl.Block("Hero") | ||
if err != nil { | ||
t.Fatalf("Blocks error: %v", err) | ||
} | ||
|
||
assert.Equal(t, "Hero", block.(*Block).ID) | ||
assert.Empty(t, block.(*Block).Codes.HTML.Code) | ||
assert.NotEmpty(t, block.(*Block).Codes.TS.Code) | ||
assert.Contains(t, block.(*Block).Compiled, "window.block__Hero") | ||
assert.Contains(t, block.(*Block).Compiled, `<div data-gjs-type='Nav'></div>`) | ||
} |
Oops, something went wrong.