Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Sep 20, 2024
1 parent f69421a commit 2b2dbc2
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 27 deletions.
6 changes: 6 additions & 0 deletions resources/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
_ resource.Cloner = (*genericResource)(nil)
_ resource.ResourcesLanguageMerger = (*resource.Resources)(nil)
_ resource.Identifier = (*genericResource)(nil)
_ resource.PathProvider = (*genericResource)(nil)
_ identity.IdentityGroupProvider = (*genericResource)(nil)
_ identity.DependencyManagerProvider = (*genericResource)(nil)
_ identity.Identity = (*genericResource)(nil)
Expand Down Expand Up @@ -463,6 +464,11 @@ func (l *genericResource) Key() string {
return key
}

// TODO1 test and document this. Consider adding it to the Resource interface.
func (l *genericResource) Path() string {
return l.paths.TargetPath()
}

func (l *genericResource) MediaType() media.Type {
return l.sd.MediaType
}
Expand Down
7 changes: 7 additions & 0 deletions resources/resource/resourcetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ type MediaTypeProvider interface {
MediaType() media.Type
}

type PathProvider interface {
// Path is the relative path to this resource.
// In most cases this will be the same as the RelPermalink(),
// but it will not trigger any lazy publishing.
Path() string
}

type ResourceLinksProvider interface {
// Permalink represents the absolute link to this resource.
Permalink() string
Expand Down
1 change: 1 addition & 0 deletions resources/resource_transformers/js/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func (c *Client) build(opts Options, transformCtx *resources.ResourceTransformat
if err != nil {
return err
}

}

return nil
Expand Down
30 changes: 19 additions & 11 deletions resources/resource_transformers/js/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,24 +261,32 @@ func createBuildPlugins(c *Client, depsManager identity.Manager, opts Options) (
}

dodebug := strings.Contains(impPath, "css")
// TODO1 todelido.
if dodebug {
// impPath = strings.TrimPrefix(impPath, "/")
}
importer := args.Importer
// TODO1 todelido.
if false && dodebug {
impPath = path.Join("js/hugoheadlessui/components", impPath)
} else {

isStdin := args.Importer == stdinImporter
isStdin := importer == stdinImporter
var relDir string
if !isStdin {
rel, found := fs.MakePathRelative(args.Importer, true)
if strings.HasPrefix(importer, "@hugo-virtual") {
// TODO1 constants.
relDir = filepath.Dir(strings.TrimPrefix(importer, "@hugo-virtual"))
} else {
rel, found := fs.MakePathRelative(importer, true)

if !found {
// Not in any of the /assets folders.
// This is an import from a node_modules, let
// ESBuild resolve this.
return api.OnResolveResult{}, nil
}

if !found {
// Not in any of the /assets folders.
// This is an import from a node_modules, let
// ESBuild resolve this.
return api.OnResolveResult{}, nil
relDir = filepath.Dir(rel)
}

relDir = filepath.Dir(rel)
} else {
relDir = opts.SourceDir
}
Expand Down
6 changes: 6 additions & 0 deletions resources/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var (
_ identity.IdentityGroupProvider = (*resourceAdapterInner)(nil)
_ resource.Source = (*resourceAdapter)(nil)
_ resource.Identifier = (*resourceAdapter)(nil)
_ resource.PathProvider = (*resourceAdapter)(nil)
_ resource.ResourceNameTitleProvider = (*resourceAdapter)(nil)
_ resource.WithResourceMetaProvider = (*resourceAdapter)(nil)
_ identity.DependencyManagerProvider = (*resourceAdapter)(nil)
Expand Down Expand Up @@ -277,6 +278,11 @@ func (r *resourceAdapter) Key() string {
return r.target.(resource.Identifier).Key()
}

func (r *resourceAdapter) Path() string {
r.init(false, false)
return r.target.(resource.PathProvider).Path()
}

func (r *resourceAdapter) MediaType() media.Type {
r.init(false, false)
return r.target.MediaType()
Expand Down
42 changes: 27 additions & 15 deletions tpl/js/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (

type Batcher interface {
UseScript(id string) BatcherScript
UseScriptMany(id string) BatcherScriptMany
UseScriptGroup(id string) BatcherScriptMany
Build() (*Package, error)
}

Expand Down Expand Up @@ -89,7 +89,11 @@ func (b *batcher) UseScript(id string) BatcherScript {

one, found := b.scriptOnes[id]
if !found {
one = &scriptOne{id: id, client: b.client}
one = &scriptOne{
id: id,
instances: make(map[string]scriptInstance),
client: b.client,
}
b.scriptOnes[id] = one
}

Expand All @@ -114,7 +118,7 @@ func (b *batcher) UseScript(id string) BatcherScript {
}
}

func (b *batcher) UseScriptMany(id string) BatcherScriptMany {
func (b *batcher) UseScriptGroup(id string) BatcherScriptMany {
b.mu.Lock()

many, found := b.scriptManys[id]
Expand Down Expand Up @@ -157,18 +161,14 @@ var (
)

func (b *scriptOne) AddInstance(id string, opts any) string {
panic("not implemented")
/*if b.r == nil {
if b.r == nil {
panic("resource not set")
}
if id == "" {
panic("id not set")
}

paramsm := cast.ToStringMap(params)
b.instances[id] = &batchInstance{params: paramsm}
*/ // TODO1

b.instances[id] = decodeScriptInstance(opts)
return ""
}

Expand Down Expand Up @@ -205,7 +205,11 @@ func (b *scriptMany) SetCallback(r resource.Resource) string {
func (b *scriptMany) UseScript(id string) BatcherScript {
item, found := b.items[id]
if !found {
item = &scriptManyItem{id: id, instances: make(map[string]scriptInstance), client: b.client}
item = &scriptManyItem{
id: id,
instances: make(map[string]scriptInstance),
client: b.client,
}
b.items[id] = item
}

Expand Down Expand Up @@ -299,6 +303,7 @@ type scriptOne struct {
id string

resourceGetSet
instances map[string]scriptInstance

client *Namespace
}
Expand Down Expand Up @@ -340,6 +345,10 @@ type resourceGetSet struct {
r resource.Resource
}

func (r *resourceGetSet) Dir() string {
return path.Dir(r.r.(resource.PathProvider).Path())
}

func (r *resourceGetSet) GetResource() resource.Resource {
if resource.StaleVersion(r.r) > 0 {
// Allow the client to set a new resource.
Expand Down Expand Up @@ -491,7 +500,7 @@ func (b *batcher) build() (*Package, error) {
if v.r == nil {
return nil, fmt.Errorf("resource not set for %q", k)
}
keyPath := path.Join(keyPath, k)
keyPath := keyPath + "_" + k
resourcePath := paths.AddLeadingSlash(keyPath + v.r.MediaType().FirstSuffix.FullSuffix)
addEntryPoint(k, resourcePath, v.r)

Expand All @@ -500,7 +509,8 @@ func (b *batcher) build() (*Package, error) {

if len(b.scriptManys) > 0 {
for k, v := range b.scriptManys {
keyPath := path.Join(keyPath, k)
keyPath := keyPath + "_" + k

bopts := batchBuildOpts{
Callback: v.callback,
}
Expand All @@ -521,11 +531,13 @@ func (b *batcher) build() (*Package, error) {
// TODO1 others.
return nil, fmt.Errorf("resource not set for %q", kk)
}
keyPath := path.Join(keyPath, kk)
keyPath := keyPath + "_" + kk
const namespace = "@hugo-virtual"
impPath := path.Join(namespace, vv.Dir(), keyPath+vv.r.MediaType().FirstSuffix.FullSuffix)
bt := batchTemplateExecutionsContext{
ID: kk,
r: vv.r,
ImportPath: keyPath + vv.r.MediaType().FirstSuffix.FullSuffix,
ImportPath: impPath,
}
impMap[bt.ImportPath] = vv.r
for kkk, vvv := range vv.instances {
Expand Down Expand Up @@ -605,7 +617,7 @@ func (b bundleResource) Title() string {
}

func (b bundleResource) RelPermalink() string {
return "/js/bundles" + string(b)
return "/js/bundles/mybundle" + string(b)
}

func (b bundleResource) Permalink() string {
Expand Down
9 changes: 8 additions & 1 deletion tpl/js/batch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,15 @@ export default function Callback(modules) {
}
}
}
-- assets/js/button.css --
button {
background-color: red;
}
-- assets/js/react1.jsx --
import * as React from "react";
import './button.css'
window.React1 = React;
Expand Down Expand Up @@ -101,7 +108,7 @@ Home.
{{ end }}
{{ .AddInstance "foo" (dict "title" "Main2 Instance") }}
{{ end }}
{{ with $bundle.UseScriptMany "reactbatch" }}
{{ with $bundle.UseScriptGroup "reactbatch" }}
{{ if not .GetCallback }}
{{ .SetCallback (resources.Get "js/reactcallback.js") }}
{{ end }}
Expand Down

0 comments on commit 2b2dbc2

Please sign in to comment.