Skip to content

Conversation

@leaanthony
Copy link
Member

@leaanthony leaanthony commented Oct 23, 2022

This PR aims to make a Wails project more configurable as requested in #1877.
Changes:

  • A new frontend:dir entry in wails.json. This can be an absolute path or a relative path from the wails.json file. The last segment of this path is considered the name of the frontend directory. Not specifying this will result in the surrent default behaviour.
  • If go.mod is not found in the same directory as wails.json, the cli will look up the parent directories to find it.

Note: The CLI commands build and dev must still be run in the same directory as wails.json.

Example project in v2/examples/customlayout

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Oct 23, 2022

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: f087641
Status: ✅  Deploy successful!
Preview URL: https://341cf4c7.wails.pages.dev
Branch Preview URL: https://feature-1877-modularize-wail.wails.pages.dev

View logs

Copy link
Collaborator

@stffabi stffabi left a comment

Choose a reason for hiding this comment

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

Great enhancement 🚀 , left some minor comments.

leaanthony and others added 2 commits October 24, 2022 08:44
Co-authored-by: stffabi <stffabi@users.noreply.github.com>
@leaanthony
Copy link
Member Author

I feel a CLI rewrite coming on 😜

@stffabi
Copy link
Collaborator

stffabi commented Oct 24, 2022

Just added a commit which should fix the buildassets comments.

@mholt
Copy link
Contributor

mholt commented Oct 24, 2022

Hey, this works for what I need! 🥳

I only tested it briefly, thanks for the great instructions. But so far it's quite nice.

@leaanthony
Copy link
Member Author

Thanks for pushing for this. It really has been on the backburner for quite some time so good to get it done finally 🙏

@mholt
Copy link
Contributor

mholt commented Oct 24, 2022

Thank you for working on it. Wow, this was not a small change!

One thing I just noticed, there does remain a build/bin folder after the application exits. It cleans up the binary that is there while it runs, but still leaves the empty folders. (It's just a minor nit.)

Should that bin go in a temp dir somewhere?

(Then again, come to think of it, MacOS may prohibit the execution of files in the temp path... I seem to recall that being so frustrating it was one of the reasons I switched to Linux on my desktop.)

@leaanthony
Copy link
Member Author

The folders are just the standard build/bin folders. I mean we could remove them but that's a standard place for builds to go. Or are you saying the Dev build is appearing in a different place and THAT needs removing?

@leaanthony
Copy link
Member Author

@mholt @stffabi - you think it's worth finishing this off with a bin:dir too or are we good with ${build:dir}/bin?

@stffabi
Copy link
Collaborator

stffabi commented Oct 26, 2022

I think we are good with ${build:dir}/bin for the time being.

@mholt
Copy link
Contributor

mholt commented Oct 26, 2022

@leaanthony The dev build from wails dev is appearing in the folder from which I run wails dev (in my case, cmd/timelinize). It has the binary in it and then after I exit the program it goes away but the build/bin folders remain, empty.

I think they're supposed to be in the root of the project, right? (the build dir is at the root in the template project, at least)

@leaanthony
Copy link
Member Author

Thanks for explaining. Yeah, should use same build dir 👍

@mholt
Copy link
Contributor

mholt commented Oct 26, 2022

One thing I just thought of that I am curious your thoughts on, is that if I do change my frontend:dir (let's say, I'm testing something real quick) I also need to update my go:embed tag on my embed.FS value. If these are out of sync things get weird real quick.

It could be nice if Wails could infer the frontend dir from the go:embed on the fs.FS set on options.App.Assets (soon to be options.App.AssetServer.Assets), but since Assets takes an fs.FS and not an embed.FS, I guess there's no guarantee it's a real folder in the first place.

So in summary, I don't know an easy way to infer the frontend dir, and this is not a big deal to me, and definitely not a blocker for this PR or the next release. But I wanted to document this nuance here for the record: if the frontend:dir is changed, don't forget to update go:embed and vice-versa. (I imagine most users will have them set to similar/same paths.)

Thanks again for this super handy improvement!

@leaanthony
Copy link
Member Author

leaanthony commented Oct 26, 2022

It's a valid thought Matt. I guess I'd not expect this to change that often so I think the work involved in inferring this has little payoff. There are a few things I wish embed did better and reflection is one of them. Also default to zero values if the dir does not exist. I considered making a proposal for it:

//go:embed dir/*
var fs embed.FS = nil

Anyway, off topic.

@stffabi
Copy link
Collaborator

stffabi commented Oct 26, 2022

It's pretty hard to infer that, since it is currently needed pretty early in the build process. So one would either need to do static analysis or build the binary in some special mode and execute it to get the inferred directory if the assets are an embed.FS. The problem is also how to find the root directory when inferring.

IIRC the frontend dir is also not exactly the same as the assets dir since the assets dir would point to the output dir of the frontend, e.g. frontend/dist.

I wonder if we could change that to some code generation method. We could add the assetdir again to wails.json which is the only source of truth for it. The build command could then create a 'assets.go' where 'main.go' is and with the correct go embed content.
'
//go:embed all:frontend/dist
var assets embed.FS
'

Would only work if the frontend dir is a child of the project dir. If it's in another dir one could add the embed there and add an import in the assts.go in the main package and referring to the assets in the other package.

Just a quick thought without thinking too much about the details and consequences...

@mholt
Copy link
Contributor

mholt commented Oct 26, 2022

@leaanthony Yep, totally understand. No problem.

@stffabi Ah yeah, you're right... I forget that frontend and frontend/dist ("Assets") are different for most people, as I have a vanilla JS frontend that doesn't use build steps. So to me they are the same.

Anyway, carry on. 😄

@leaanthony
Copy link
Member Author

@leaanthony The dev build from wails dev is appearing in the folder from which I run wails dev (in my case, cmd/timelinize). It has the binary in it and then after I exit the program it goes away but the build/bin folders remain, empty.

I think they're supposed to be in the root of the project, right? (the build dir is at the root in the template project, at least)

@mholt - I am unable to reproduce this! The dev exe is appearing in the same place as the production binary for me with the -dev suffix 😖

@mholt
Copy link
Contributor

mholt commented Oct 29, 2022

@leaanthony Ok, no worries then! I probably misconfigured something in my setup. I wouldn't worry about it right now or let it be a blocker.

This LGTM to merge for now!

@leaanthony
Copy link
Member Author

No worries @mholt. Just let me know if you can reproduce with a clean project and we'll fix it!

@olup
Copy link

olup commented Oct 30, 2022

Just wanted to add a useless comment sharing how exciting this is, I can't wait to finally refactor my app to include all sub modules alongside the wails code !

@leaanthony leaanthony marked this pull request as ready for review November 3, 2022 08:16
Copy link
Collaborator

@stffabi stffabi left a comment

Choose a reason for hiding this comment

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

LGTM 🚀

@leaanthony leaanthony merged commit 18b2d31 into master Nov 3, 2022
@leaanthony leaanthony deleted the feature/1877-modularize-wails branch November 3, 2022 10:21
@W1M0R
Copy link

W1M0R commented Sep 14, 2023

Just leaving this note here for others that might have the same issue.

The ../ path does not seem be supported by go:embed. So your frontend dist (not src) will still have to live next to or below the wails.json file:

// THE FOLLOWING WON'T WORK

//go:embed all:../frontend/dist
var assets embed.FS

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.

6 participants