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

[WIP] Allow external scoping #294

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion cmd/astro-wasm/astro-wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func makeTransformOptions(options js.Value, hash string) transform.TransformOpti
projectRoot = "."
}

externalScoping := false
if jsBool(options.Get("externalScoping")) {
externalScoping = true
}

staticExtraction := false
if jsBool(options.Get("experimentalStaticExtraction")) {
staticExtraction = true
Expand All @@ -96,6 +101,7 @@ func makeTransformOptions(options js.Value, hash string) transform.TransformOpti
Site: site,
ProjectRoot: projectRoot,
PreprocessStyle: preprocessStyle,
ExternalScoping: externalScoping,
StaticExtraction: staticExtraction,
}
}
Expand Down Expand Up @@ -129,7 +135,7 @@ func preprocessStyle(i int, style *astro.Node, transformOptions transform.Transf
return
}
attrs := wasm_utils.GetAttrs(style)
data, _ := wasm_utils.Await(transformOptions.PreprocessStyle.(js.Value).Invoke(style.FirstChild.Data, attrs))
data, _ := wasm_utils.Await(transformOptions.PreprocessStyle.(js.Value).Invoke(style.FirstChild.Data, attrs, transformOptions))
Copy link
Contributor

Choose a reason for hiding this comment

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

is this needed? I think that ProcessStyle has access to all of these options on the JS side anyways. Is there something that it needs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think so. This was to have the object containing the hash value (e.g. astro-hash1d2c3b4a).

Copy link
Contributor

Choose a reason for hiding this comment

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

We talked about this, make sense.

// note: Rollup (and by extension our Astro Vite plugin) allows for "undefined" and "null" responses if a transform wishes to skip this occurrence
if data[0].Equal(js.Undefined()) || data[0].Equal(js.Null()) {
return
Expand Down
3 changes: 2 additions & 1 deletion internal/transform/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ type TransformOptions struct {
Site string
ProjectRoot string
PreprocessStyle interface{}
ExternalScoping bool
StaticExtraction bool
}

func Transform(doc *astro.Node, opts TransformOptions) *astro.Node {
shouldScope := len(doc.Styles) > 0 && ScopeStyle(doc.Styles, opts)
shouldScope := opts.ExternalScoping || len(doc.Styles) > 0 && ScopeStyle(doc.Styles, opts)
Copy link
Contributor

Choose a reason for hiding this comment

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

The shouldScope variable is used to determine if the scoped class name should be added to elements. We don't want this to always be true, because some components do not need the elements to have a scoped class name.

Components that do not have any <style>s or only have <style global> do not need the scoped class name. So in this case I think it makes sense for ScopeStyle to still run, but take opts.ExternalScoping into account for whether not not the CSS should be scoped.

walk(doc, func(n *astro.Node) {
ExtractScript(doc, n)
AddComponentProps(doc, n)
Expand Down