Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating scaffolding to create default.css #254

Open
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

ralfiannor
Copy link

Implement #253

@CLAassistant
Copy link

CLAassistant commented Aug 20, 2022

CLA assistant check
All committers have signed the CLA.

@matthewmueller
Copy link
Contributor

matthewmueller commented Aug 20, 2022

Thanks for the PR @ralfiannor and for kicking this off! I don't want to remove the default.css without providing a solution to the goal. I've wrote my thoughts up in #253. Let me know if you want to drive one of those solutions forward or if you have any other ideas!

@matthewmueller matthewmueller marked this pull request as draft August 20, 2022 18:13
@ralfiannor ralfiannor changed the title delete default css Updating scaffolding to create default.css Aug 27, 2022
@ralfiannor ralfiannor marked this pull request as ready for review August 27, 2022 21:47
Copy link
Contributor

@matthewmueller matthewmueller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! I'd just like us to clean a few things up and add a few more tests (maybe in framework/public/public_test.go) and then I think we're good to go!

framework/view/ssr/svelte.ts Outdated Show resolved Hide resolved
framework/view/ssr/svelte.js Outdated Show resolved Hide resolved
internal/cli/create/create_test.go Show resolved Hide resolved
internal/embedded/embedded.go Outdated Show resolved Hide resolved
Fix staticcheck issues and add staticcheck to ci (livebud#263)
Copy link
Contributor

@matthewmueller matthewmueller left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just reviewed. This is getting closer @ralfiannor, thank you!

framework/controller/controller_test.go Show resolved Hide resolved
// EmptyCss reset the default css data
func EmptyCss() []byte {
return []byte("/* No Default CSS Loaded */")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be better if the view didn't include the <link /> if a public/default.css isn't present since that would avoid a roundtrip for this CSS sheet.

Then we can also remove this function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -49,6 +49,7 @@ func (c *CLI) Run(ctx context.Context, args ...string) error {
cmd := create.New(cmd, c.in)
cli := cli.Command("create", "create a new app")
cli.Arg("dir").String(&cmd.Dir)
cli.Flag("css", "add a css").String(&cmd.Css).Default("normalize")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove this flag for now, I'm not ready to expose it just yet, especially since it's not really possible to select an alternative.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

res, err = app.Get("/default.css")
is.NoErr(err)
is.Equal(200, res.Status())
is.Equal(res.Body().Bytes(), defaultCss2)
Copy link
Contributor

@matthewmueller matthewmueller Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit hard to follow these changes because there are new conditions added to existing tests.

Let's try to only touch existing tests that need to be updated for the new behavior, then add new tests for the new behavior.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted. No need to update on public.go just update on ssr.go

@@ -327,7 +327,7 @@ var defaultLayout = {
<html>
<head>
<meta charset="utf-8"/>
<style>${defaultCSS}</style>
<link rel="stylesheet" href="default.css" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind trying to feed the configuration through so you can remove this if public/default.css doesn't exist? Let me know if it's difficult and I'll give it a go in this PR.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try. I will add the checking file existing in ssr.go but will call our logic each page loaded. Is it okay ?

Copy link
Contributor

@matthewmueller matthewmueller Sep 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be done at compile time actually. Do you know how to do this?

@matthewmueller
Copy link
Contributor

matthewmueller commented Sep 2, 2022

I just realized we should also do the same thing for public/favicon.ico: scaffold a default one, but then give users the option to override it or delete it.

@ralfiannor
Copy link
Author

I just realized we should also do the same thing for public/favicon.ico: scaffold a default one, but then give users the option to override it or delete it.

Can we do it on another PR?

@matthewmueller
Copy link
Contributor

matthewmueller commented Sep 23, 2022

Thanks for continuing to push this forward @ralfiannor!

Do you happen to know the condition in which Go is adding/removing Transfer-Encoding: chunked?

Let's just remove the Transfer-Encoding: chunked in the view tests and then I think this PR is good to go!

@ralfiannor
Copy link
Author

ralfiannor commented Sep 25, 2022

Do you happen to know the condition in which Go is adding/removing Transfer-Encoding: chunked?

Yes, it happen when we added the CSS into <style></style> and the chunked also added

@matthewmueller
Copy link
Contributor

matthewmueller commented Sep 25, 2022

Thanks @ralfiannor! I just pulled it down and did some manual testing. Two issues popped up:

  1. Welcome page no longer shows up when creating a new project
  2. Renaming or removing the default.css requires you to restart the bud process

Adjusting the global svelteRuntime is a bit hacky. If you're up for cleaning this up, a better solution would be to pass this information through the svelte.gotext template and then reference that information in the svelte.ts file. I'm okay with cleaning this up later if you're not sure how to do this.

Here's a diff with the 2 fixes above:

diff --git a/framework/app/app_test.go b/framework/app/app_test.go
index b5ba644..500a117 100644
--- a/framework/app/app_test.go
+++ b/framework/app/app_test.go
@@ -30,3 +30,26 @@ func TestWelcome(t *testing.T) {
 	is.NoErr(td.Exists("bud/app"))
 	is.NoErr(app.Close())
 }
+
+func TestWelcomeWithPublic(t *testing.T) {
+	is := is.New(t)
+	ctx, cancel := context.WithCancel(context.Background())
+	defer cancel()
+	dir := t.TempDir()
+	td := testdir.New(dir)
+	td.Files["public/default.css"] = `* { box-sizing: border-box; }`
+	is.NoErr(td.Write(ctx))
+	cli := testcli.New(dir)
+	is.NoErr(td.NotExists("bud/app"))
+	app, err := cli.Start(ctx, "run")
+	is.NoErr(err)
+	res, err := app.Get("/")
+	is.NoErr(err)
+	is.Equal(res.Status(), 200)
+	is.In(res.Body().String(), "Hey Bud")
+	is.In(res.Body().String(), "Hey Bud") // should work multiple times
+	is.Equal(app.Stdout(), "")
+	is.Equal(app.Stderr(), "")
+	is.NoErr(td.Exists("bud/app"))
+	is.NoErr(app.Close())
+}
diff --git a/framework/view/ssr/ssr.go b/framework/view/ssr/ssr.go
index 7116bcd..8bab21c 100644
--- a/framework/view/ssr/ssr.go
+++ b/framework/view/ssr/ssr.go
@@ -50,9 +50,10 @@ type Compiler struct {
 
 func (c *Compiler) Compile(ctx context.Context, fsys budfs.FS) ([]byte, error) {
 	dir := c.module.Directory()
-
 	if existCss := vfs.Exist(fsys, "public/default.css"); nil == existCss {
 		svelteRuntime = strings.Replace(svelteRuntime, `<!-- default css -->`, `<link rel="stylesheet" href="default.css">`, 1)
+	} else {
+		svelteRuntime = strings.Replace(svelteRuntime, `<link rel="stylesheet" href="default.css">`, `<!-- default css -->`, 1)
 	}
 
 	result := esbuild.Build(esbuild.BuildOptions{
diff --git a/framework/web/loader.go b/framework/web/loader.go
index 5f0d149..e4695b8 100644
--- a/framework/web/loader.go
+++ b/framework/web/loader.go
@@ -52,7 +52,10 @@ func (l *loader) Load() (state *State, err error) {
 	l.imports.AddNamed("webrt", "github.com/livebud/bud/framework/web/webrt")
 	l.imports.AddNamed("router", "github.com/livebud/bud/package/router")
 	// Show the welcome page if we don't have controllers, views or public files
-	if len(exist) == 0 {
+	//
+	// TODO: welcome should be part of a default controller, then we can support
+	// a simple static file server without generating the welcome page.
+	if len(exist) == 0 || (len(exist) == 1 && exist["bud/internal/app/public/public.go"]) {
 		l.imports.AddNamed("welcome", "github.com/livebud/bud/framework/web/welcome")
 		state.ShowWelcome = true
 		state.Imports = l.imports.List()

@matthewmueller
Copy link
Contributor

Any chance of wrapping this up @ralfiannor ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants