Skip to content

Commit

Permalink
Identity manager
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Oct 21, 2024
1 parent 871bad8 commit 3c397bb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion resources/resource_transformers/js/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (c *Client) BuildBundle(opts Options) (api.BuildResult, error) {
func (c *Client) build(opts Options, transformCtx *resources.ResourceTransformationCtx) (api.BuildResult, error) {
dependencyManager := opts.DependencyManager
if transformCtx != nil {
dependencyManager = transformCtx.DependencyManager // TODO1
dependencyManager = transformCtx.DependencyManager
}
if dependencyManager == nil {
dependencyManager = identity.NopManager
Expand Down
5 changes: 2 additions & 3 deletions resources/resource_transformers/js/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,12 @@ type InternalOptions struct {

DependencyManager identity.Manager

// TODO1
Write bool // Set to false to write to memory.
AllowOverwrite bool
Splitting bool
TsConfig string
EntryPoints []string
ImportOnResolveFunc func(string, api.OnResolveArgs) string
ImportOnResolveFunc func(identity.Manager, string, api.OnResolveArgs) string
ImportOnLoadFunc func(api.OnLoadArgs) string
ImportParamsOnLoadFunc func(args api.OnLoadArgs) json.RawMessage
ErrorMessageResolveFunc func(api.Message) *ErrorMessageResolved
Expand Down Expand Up @@ -270,7 +269,7 @@ func createBuildPlugins(c *Client, depsManager identity.Manager, opts Options) (
}

if opts.ImportOnResolveFunc != nil {
if s := opts.ImportOnResolveFunc(impPath, args); s != "" {
if s := opts.ImportOnResolveFunc(depsManager, impPath, args); s != "" {
return api.OnResolveResult{Path: s, Namespace: NsHugoImportResolveFunc}, nil
}
}
Expand Down
30 changes: 24 additions & 6 deletions tpl/js/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,12 +386,15 @@ func (s scriptGroups) Sorted() []*scriptGroup {
return a
}

var _ identity.DependencyManagerProvider = (*batcher)(nil)

type batcher struct {
mu sync.Mutex
id string
scriptGroups scriptGroups

client *Namespace
client *Namespace
dependencyManager identity.Manager

configOptions *options

Expand All @@ -403,6 +406,10 @@ type batcher struct {
config js.ExternalOptions
}

func (b *batcher) GetDependencyManager() identity.Manager {
return b.dependencyManager
}

func (b *batcher) compile() error {
var err error
b.config, err = js.DecodeExternalOptions(b.configOptions.commit().opts)
Expand Down Expand Up @@ -652,6 +659,15 @@ func (p *Package) IsProbablyDependency(other identity.Identity) bool {

// TODO1 why is this called twice on change?

if b {
fmt.Println("M1", other)
return b
}

if p.origin.dependencyManager != nil {
b = depsFinder.Contains(other, p.origin.dependencyManager, -1) > 0
}

return b
}

Expand Down Expand Up @@ -870,11 +886,12 @@ func (b *batcher) doBuild() (*Package, error) {
jsOpts := js.Options{
ExternalOptions: externalOptions,
InternalOptions: js.InternalOptions{
OutDir: outDir,
Write: true,
AllowOverwrite: true,
Splitting: true,
ImportOnResolveFunc: func(imp string, args api.OnResolveArgs) string {
DependencyManager: b.dependencyManager,
OutDir: outDir,
Write: true,
AllowOverwrite: true,
Splitting: true,
ImportOnResolveFunc: func(depsManager identity.Manager, imp string, args api.OnResolveArgs) string {
if _, found := state.importResource.Get(imp); found {
return imp
}
Expand All @@ -889,6 +906,7 @@ func (b *batcher) doBuild() (*Package, error) {
if importContext.resourceGetter != nil {
resolved := importContext.resourceGetter.Get(imp)
if resolved != nil {
depsManager.AddIdentity(identity.FirstIdentity(resolved))
imp := js.PrefixHugoVirtual + resolved.(resource.PathProvider).Path()
state.importResource.Set(imp, resolved)
state.importerImportContext.Set(imp, importContext)
Expand Down
5 changes: 0 additions & 5 deletions tpl/js/batch_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ disableKinds = ["taxonomy", "term"]
disableLiveReload = true
baseURL = "https://example.com"
# TOOD1
[build]
[[build.cachebusters]]
source = '.*'
target = '.*'
-- package.json --
{
Expand Down
9 changes: 5 additions & 4 deletions tpl/js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ func (ns *Namespace) Batch(id string, store *maps.Scratch) (Batcher, error) {
key := path.Join(nsBundle, id)
b := store.GetOrCreate(key, func() any {
return &batcher{
id: id,
scriptGroups: make(map[string]*scriptGroup),
client: ns,
configOptions: newOptions(),
id: id,
scriptGroups: make(map[string]*scriptGroup),
client: ns,
dependencyManager: ns.d.Conf.NewIdentityManager("jsbatch"),
configOptions: newOptions(),
}
})
return b.(Batcher), nil
Expand Down

0 comments on commit 3c397bb

Please sign in to comment.