Skip to content

Commit e57d192

Browse files
Merge pull request #49 from theNewDynamic/47-refactor-assets
Refactor Styles/Scripts to allow passing settings without config
2 parents 3359644 + c580693 commit e57d192

12 files changed

Lines changed: 235 additions & 184 deletions

config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ markup:
1616
module:
1717
hugoVersion:
1818
extended: true
19+
# Bumping minVersion? Search comments for +minVer+ first.
1920
min: "0.86.1"
2021
mounts:
2122
- source: assets
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
{{/*
2+
huge/common/assets/GetSelectedConfig
3+
This function brings consistency to the way Scripts and Styles retrieves data from config.
4+
It reads a list of selected assets to be processed, and try and match them with declared assets,
5+
before returning the data to be consumed by each features' respective `GetSelectedScripts/GetSelectedStyles`
6+
7+
@author @regisphilibert
8+
9+
@context Slice
10+
- Slice of maps (Declared Assets to try and match from)
11+
- Slice of maps/string (Selected Assets from the top most context)
12+
13+
@access private
14+
15+
@returns Slice of Maps
16+
See scripts/private/GetDeclaredScripts@returns
17+
18+
@uses
19+
- huge/scripts/private/GetDeclaredScripts
20+
21+
*/}}
22+
23+
{{/* First item in the context slice is the assets found in config */}}
24+
{{ $declared_assets := index $ 0 }}
25+
{{/* Second item is the assets passed through the partial /tags context. */}}
26+
{{ $selected_assets := index $ 1 }}
27+
28+
{{ $assets := slice }}
29+
{{ range $asset_ref := $selected_assets }}
30+
{{ $entry := dict }}
31+
{{/* If entry is a map we'll store it and then try and match map from config with the same .name */}}
32+
{{ if reflect.IsMap . }}
33+
{{ $entry = . }}
34+
{{ with .name }}
35+
{{ with where $declared_assets "name" . }}
36+
{{ with index . 0 }}
37+
{{/* If matching config map found, we merge the this one on top of it
38+
This ways user can overwrite config settings from the templates. */}}
39+
{{ $entry = merge . $entry }}
40+
{{ end }}
41+
{{ end }}
42+
{{ end }}
43+
{{ else }}
44+
{{/* If entry is not a map, then it must be a string representing the name of a map stored in config.transition
45+
We must therefor find its match */}}
46+
{{ with $declared_assets }}
47+
{{ with where . "name" $asset_ref }}
48+
{{ with index . 0 }}
49+
{{ $entry = . }}
50+
{{ end }}
51+
{{ else }}
52+
{{ partialCached "huge/console/warn" (printf "An asset named %s is invoked but not declared" $asset_ref) }}
53+
{{ end }}
54+
{{ end }}
55+
{{ end }}
56+
57+
{{/* We only use the entry if it holds a .path */}}
58+
{{ with $entry.path }}
59+
{{ $assets = $assets | append $entry }}
60+
{{ end }}
61+
62+
{{ end }}
63+
64+
{{ return $assets }}

core/scripts/private/GetDeclaredScripts.html

Lines changed: 0 additions & 71 deletions
This file was deleted.
Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
11
{{/*
22
huge/scripts/private/GetSelectedScripts
3-
Returns a list of styles from the registered styles list whose name matches the name
4-
listed as input while preserving the order.
3+
Returns the properly parsed script data from user selected list.
54

65
@author @regisphilibert
76

8-
@context Type Slice of strings
7+
@context Slice of Strings/Maps
98

109
@access private
1110

12-
@returns Slice of Maps
13-
See scripts/private/GetDeclaredScripts@returns
14-
1511
@uses
16-
- huge/scripts/private/GetDeclaredScripts
12+
- huge/config/Get
13+
14+
@returns Slice of Maps
15+
- huge/scripts/private/ParseScript@return
1716

1817
*/}}
18+
{{ $scripts := slice }}
19+
20+
{{ $declared_scripts := slice }}
21+
22+
{{ $config := partialCached "huge/config/Get" "scripts" "scripts" }}
23+
{{ with $config.scripts }}
24+
{{ $declared_scripts = . }}
25+
{{ end }}
26+
{{/* If context of partial proves intent of selected styles from registered pool by being
27+
either a string or a slice of string then we only treat those*/}}
1928
{{ $selected_scripts := slice }}
20-
{{ $declared_scripts := partial "huge/scripts/private/GetDeclaredScripts" "GetDeclaredScripts" }}
21-
{{ range $ }}
22-
{{ with where $declared_scripts "name" . }}
23-
{{ with index . 0 }}
24-
{{ $selected_scripts = $selected_scripts | append . }}
25-
{{ end }}
26-
{{ else }}
27-
{{ partialCached "huge/console/warn" (printf "A script named %s is invoked but not declared" .) }}
29+
{{ if reflect.IsSlice $ }}
30+
{{ $selected_scripts = . }}
31+
{{ else }}
32+
{{ $selected_scripts = slice $ }}
33+
{{ end }}
34+
{{ with $selected_scripts }}
35+
{{ with partial "huge/common/assets/GetSelectedConfig" (slice $declared_scripts $selected_scripts) }}
36+
{{ $scripts = apply . "partial" "huge/scripts/private/ParseScript" "." }}
2837
{{ end }}
2938
{{ end }}
3039

31-
{{ return $selected_scripts }}
40+
{{ return $scripts }}

core/scripts/private/GetTags.html

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,9 @@
1919
{{ $dev := partialCached "huge/config/Get" "dev" "dev"}}
2020
{{ $debug := $dev.debug | default false }}
2121

22-
{{/* Default is printing every registered tags */}}
23-
{{ $scripts := partial "huge/scripts/private/GetDeclaredScripts" "GetDeclaredScripts" }}
24-
25-
{{/* If context of partial proves intent of selected styles from registered pool by being
26-
either a string or a slice of string then we only treat those*/}}
27-
{{ $selected_scripts := slice }}
28-
{{ if reflect.IsSlice $ }}
29-
{{ $selected_scripts = . }}
30-
{{ else }}
31-
{{ if eq (printf "%#T" $) "string" }}
32-
{{ $selected_scripts = slice $ }}
33-
{{ end }}
34-
{{ end }}
35-
{{ with $selected_scripts }}
36-
{{ $scripts = partialCached "huge/scripts/private/GetSelectedScripts" . . }}
37-
{{ end }}
38-
3922
{{/* Ranging on the selected scripts, apply the js transformation */}}
40-
{{ range $script := $scripts }}
41-
{{ with partialCached "huge/scripts/private/transform/_" . .name }}
23+
{{ range $script := partial "huge/scripts/private/GetSelectedScripts" $ }}
24+
{{ with partial "huge/scripts/private/transform/_" . }}
4225
{{/* Default assumes this is a script rather than inline */}}
4326
{{ $tag := dict
4427
"name" "script"
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{{/*
2+
ParseScript
3+
Structure the script consumable data from a given config input
4+
5+
@author @regisphilibert
6+
7+
@context Map
8+
9+
@access private
10+
11+
@returns Map
12+
String .name
13+
Map or String .defines
14+
Bool .fingerprint
15+
String .format
16+
Bool .inline
17+
Slice .inject
18+
Slice .externals
19+
Bool .minify
20+
Map .params
21+
Map .shims
22+
String .target
23+
24+
@uses
25+
- huge/scripts/private/GetDefines
26+
- huge/env/When
27+
@example - Go Template
28+
{{ with partialCached "ParseScripts" context context }}
29+
{{ something = . }}
30+
{{ end }}
31+
*/}}
32+
{{ $data := dict }}
33+
{{ with $script := . }}
34+
{{ $name := $script.name | default $script.path }}
35+
36+
{{/* Huge will not take a null value (emtpy slice) as context for a returning partial, (+minVer+: fixed in 0.90.0)
37+
we therfore default on "none" for $defines, and test the type of context from within the partial */}}
38+
{{ $defines := $script.defines | default "none" }}
39+
{{ $defines = partialCached "huge/scripts/private/GetDefines" $defines $defines }}
40+
{{ $internal := $script.internal | default false }}
41+
{{ $inject := $script.inject | default slice }}
42+
{{ $externals := $script.externals | default slice }}
43+
{{ $format := $script.format | default "iife" }}
44+
{{ $integrity := $script.integrity | default false }}
45+
{{ $minify := $script.minify | default "production" }}
46+
{{ $minify = partialCached "huge/env/When" $minify $minify }}
47+
{{ $fingerprint := $script.fingerprint | default "production" }}
48+
{{ $fingerprint = partialCached "huge/env/When" $fingerprint $fingerprint }}
49+
{{ $params := $script.params | default dict }}
50+
{{ $shims := $script.shims | default slice }}
51+
{{ $target := $script.target | default "esnext" }}
52+
53+
{{ $data = dict
54+
"name" $name
55+
"path" $script.path
56+
"defines" $defines
57+
"format" $format
58+
"integrity" $integrity
59+
"internal" $internal
60+
"inject" $inject
61+
"externals" $externals
62+
"minify" $minify
63+
"fingerprint" $fingerprint
64+
"params" $params
65+
"shims" $shims
66+
"target" $target
67+
}}
68+
{{ end }}
69+
70+
{{ return $data }}

core/styles/private/GetDeclaredStyles.html

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)