11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.

Home Page:https://www.11ty.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Async options for BrowserSync config

zachleat opened this issue · comments

Some discussion with @Holben888 prompted the Slinkity/Vite need to do some async work to generate middleware options for browsersync.

https://github.com/slinkity/slinkity/blob/main/src/plugin/index.js#L101-L125

Note server = await viteSSR.createServer()

Because async config init is a very hefty task (no async in class constructors), we thought perhaps a more specialized case might be warranted here, because the browsersync init happens so late in the order of operations.

Note sure what that looks like yet.

Eleventy->watch is already async

async watch() {

Eleventy->serve is not yet async

serve(port) {
but could be changed. Called via cmd.js

eleventy/cmd.js

Line 113 in 5020390

elev.serve(argv.port);

Did some initial investigation here! It seem we can't safely resolve async configs from the user config side of things, since that opens a huge "async everything" can of worms. However, it is safe to resolve async configs inside async-ready functions like elev.serve.

We'll likely need to touch 2 functions:

getOptions(port) {

Adding an await before the Object.assign statement

serve(port) {

Adding an await before the this.getOptions call

There's just 1 snag left to fix: merging browsersync configs. This is currently handled from the user-facing setBrowserSyncConfig, so it could only work when options is synchronous 😬

eleventy/src/UserConfig.js

Lines 676 to 682 in 1ea3a90

setBrowserSyncConfig(options = {}, mergeOptions = true) {
if (mergeOptions) {
this.browserSyncConfig = merge(this.browserSyncConfig, options);
} else {
this.browserSyncConfig = options;
}
}

We'll likely need to move merge into the getOptions call mentioned earlier. We'll also need to change UserConfig.browserSyncConfig to an array of configs so we can delay merging until later (when it's safe to perform async operations).

Will check back on this, but it seems doable!

Should be good to close with new 11ty 2.0 server "setup" option!