elm-pages 3.0.0-beta.3 → 3.0.0-beta.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/README.md +10 -1
  2. package/codegen/{elm-pages-codegen.js → elm-pages-codegen.cjs} +2864 -2589
  3. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmi +0 -0
  4. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateData.elmo +0 -0
  5. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/Pages-Review-DeadCodeEliminateDataTest.elmo +0 -0
  6. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  7. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  8. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  9. package/generator/dead-code-review/elm-stuff/tests-0.19.1/elm.json +1 -1
  10. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1327 -122
  11. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/Runner.elm.js +15295 -13271
  12. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  13. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +4 -4
  14. package/generator/dead-code-review/elm.json +8 -6
  15. package/generator/dead-code-review/src/Pages/Review/DeadCodeEliminateData.elm +59 -10
  16. package/generator/dead-code-review/tests/Pages/Review/DeadCodeEliminateDataTest.elm +52 -36
  17. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  18. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  19. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  20. package/generator/review/elm-stuff/tests-0.19.1/elm.json +1 -1
  21. package/generator/review/elm-stuff/tests-0.19.1/js/Reporter.elm.js +1327 -122
  22. package/generator/review/elm-stuff/tests-0.19.1/js/Runner.elm.js +14621 -12637
  23. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  24. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +4 -4
  25. package/generator/review/elm.json +8 -8
  26. package/generator/src/RouteBuilder.elm +113 -107
  27. package/generator/src/SharedTemplate.elm +3 -2
  28. package/generator/src/SiteConfig.elm +3 -2
  29. package/generator/src/basepath-middleware.js +3 -3
  30. package/generator/src/build.js +123 -87
  31. package/generator/src/cli.js +256 -77
  32. package/generator/src/codegen.js +29 -27
  33. package/generator/src/compatibility-key.js +3 -0
  34. package/generator/src/compile-elm.js +25 -25
  35. package/generator/src/config.js +39 -0
  36. package/generator/src/copy-dir.js +2 -2
  37. package/generator/src/dev-server.js +150 -133
  38. package/generator/src/dir-helpers.js +9 -26
  39. package/generator/src/elm-codegen.js +5 -4
  40. package/generator/src/elm-file-constants.js +2 -3
  41. package/generator/src/error-formatter.js +12 -11
  42. package/generator/src/file-helpers.js +3 -4
  43. package/generator/src/generate-template-module-connector.js +23 -22
  44. package/generator/src/init.js +9 -8
  45. package/generator/src/pre-render-html.js +39 -28
  46. package/generator/src/render-test.js +109 -0
  47. package/generator/src/render-worker.js +25 -28
  48. package/generator/src/render.js +322 -142
  49. package/generator/src/request-cache.js +252 -163
  50. package/generator/src/rewrite-client-elm-json.js +5 -5
  51. package/generator/src/rewrite-elm-json.js +7 -7
  52. package/generator/src/route-codegen-helpers.js +16 -31
  53. package/generator/src/seo-renderer.js +12 -7
  54. package/generator/src/vite-utils.js +77 -0
  55. package/generator/static-code/hmr.js +79 -13
  56. package/generator/template/app/Api.elm +6 -5
  57. package/generator/template/app/Effect.elm +123 -0
  58. package/generator/template/app/ErrorPage.elm +37 -6
  59. package/generator/template/app/Route/Index.elm +17 -10
  60. package/generator/template/app/Shared.elm +24 -47
  61. package/generator/template/app/Site.elm +19 -6
  62. package/generator/template/app/View.elm +1 -8
  63. package/generator/template/elm-tooling.json +0 -3
  64. package/generator/template/elm.json +34 -25
  65. package/generator/template/package.json +10 -4
  66. package/package.json +23 -22
  67. package/src/ApiRoute.elm +199 -61
  68. package/src/BackendTask/Custom.elm +325 -0
  69. package/src/BackendTask/Env.elm +90 -0
  70. package/src/{DataSource → BackendTask}/File.elm +128 -43
  71. package/src/{DataSource → BackendTask}/Glob.elm +136 -125
  72. package/src/BackendTask/Http.elm +673 -0
  73. package/src/{DataSource → BackendTask}/Internal/Glob.elm +1 -1
  74. package/src/BackendTask/Internal/Request.elm +28 -0
  75. package/src/BackendTask/Random.elm +79 -0
  76. package/src/BackendTask/Time.elm +47 -0
  77. package/src/BackendTask.elm +537 -0
  78. package/src/FatalError.elm +89 -0
  79. package/src/Form/Field.elm +21 -9
  80. package/src/Form/FieldView.elm +94 -14
  81. package/src/Form.elm +275 -400
  82. package/src/Head.elm +237 -7
  83. package/src/HtmlPrinter.elm +7 -3
  84. package/src/Internal/ApiRoute.elm +7 -5
  85. package/src/PageServerResponse.elm +6 -1
  86. package/src/Pages/FormState.elm +6 -5
  87. package/src/Pages/GeneratorProgramConfig.elm +15 -0
  88. package/src/Pages/Internal/FatalError.elm +5 -0
  89. package/src/Pages/Internal/Form.elm +21 -1
  90. package/src/Pages/{Msg.elm → Internal/Msg.elm} +26 -16
  91. package/src/Pages/Internal/Platform/Cli.elm +507 -763
  92. package/src/Pages/Internal/Platform/CompatibilityKey.elm +6 -0
  93. package/src/Pages/Internal/Platform/Effect.elm +1 -2
  94. package/src/Pages/Internal/Platform/GeneratorApplication.elm +373 -0
  95. package/src/Pages/Internal/Platform/StaticResponses.elm +73 -270
  96. package/src/Pages/Internal/Platform/ToJsPayload.elm +4 -7
  97. package/src/Pages/Internal/Platform.elm +215 -102
  98. package/src/Pages/Internal/Script.elm +17 -0
  99. package/src/Pages/Internal/StaticHttpBody.elm +35 -1
  100. package/src/Pages/Manifest.elm +29 -4
  101. package/src/Pages/PageUrl.elm +23 -9
  102. package/src/Pages/ProgramConfig.elm +14 -10
  103. package/src/Pages/Script.elm +109 -0
  104. package/src/Pages/SiteConfig.elm +3 -2
  105. package/src/Pages/StaticHttp/Request.elm +2 -2
  106. package/src/Pages/StaticHttpRequest.elm +23 -98
  107. package/src/PagesMsg.elm +92 -0
  108. package/src/Path.elm +16 -19
  109. package/src/QueryParams.elm +21 -172
  110. package/src/RequestsAndPending.elm +8 -19
  111. package/src/Result/Extra.elm +26 -0
  112. package/src/Scaffold/Form.elm +484 -0
  113. package/src/Scaffold/Route.elm +1376 -0
  114. package/src/Server/Request.elm +43 -37
  115. package/src/Server/Session.elm +34 -34
  116. package/src/Server/SetCookie.elm +1 -1
  117. package/src/Test/Html/Internal/ElmHtml/ToString.elm +8 -9
  118. package/src/DataSource/Env.elm +0 -38
  119. package/src/DataSource/Http.elm +0 -446
  120. package/src/DataSource/Internal/Request.elm +0 -20
  121. package/src/DataSource/Port.elm +0 -90
  122. package/src/DataSource.elm +0 -538
  123. package/src/Pages/Generate.elm +0 -800
@@ -0,0 +1,89 @@
1
+ module FatalError exposing (FatalError, fromString, recoverable)
2
+
3
+ {-| The Elm language doesn't have the concept of exceptions or special control flow for errors. It just has
4
+ Custom Types, and by convention types like `Result` and the `Err` variant are used to represent possible failure states
5
+ and combine together different error states.
6
+
7
+ `elm-pages` doesn't change that, Elm still doesn't have special exception control flow at the language level. It does have
8
+ a type, which is just a regular old Elm type, called `FatalError`. Why? Because this plain old Elm type does have one
9
+ special characteristic - the `elm-pages` framework knows how to turn it into an error message. This becomes interesting
10
+ because an `elm-pages` app has several places that accept a value of type `BackendTask FatalError.FatalError value`.
11
+ This design lets the `elm-pages` framework do some of the work for you.
12
+
13
+ For example, if you wanted to handle possible errors to present them to the user
14
+
15
+ type alias Data =
16
+ String
17
+
18
+ data : RouteParams -> BackendTask FatalError Data
19
+ data routeParams =
20
+ BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages"
21
+ (Decode.field "description" Decode.string)
22
+ |> BackendTask.onError
23
+ (\error ->
24
+ case FatalError.unwrap error of
25
+ BackendTask.Http.BadStatus metadata string ->
26
+ if metadata.statusCode == 401 || metadata.statusCode == 403 || metadata.statusCode == 404 then
27
+ BackendTask.succeed "Either this repo doesn't exist or you don't have access to it."
28
+
29
+ else
30
+ -- we're only handling these expected error cases. In the case of an HTTP timeout,
31
+ -- we'll let the error propagate as a FatalError
32
+ BackendTask.fail error |> BackendTask.allowFatal
33
+
34
+ _ ->
35
+ BackendTask.fail error |> BackendTask.allowFatal
36
+ )
37
+
38
+ This can be a lot of work for all possible errors, though. If you don't expect this kind of error (it's an _exceptional_ case),
39
+ you can let the framework handle it if the error ever does unexpectedly occur.
40
+
41
+ data : RouteParams -> BackendTask FatalError Data
42
+ data routeParams =
43
+ BackendTask.Http.getJson "https://api.github.com/repos/dillonkearns/elm-pages"
44
+ (Decode.field "description" Decode.string)
45
+ |> BackendTask.allowFatal
46
+
47
+ This is especially useful for pages generated at build-time (`RouteBuilder.preRender`) where you want the build
48
+ to fail if anything unexpected happens. With pre-rendered routes, you know that these error cases won't
49
+ be seen by users, so it's often a great idea to just let the framework handle these unexpected errors so a developer can
50
+ debug them and see what went wrong. In the example above, maybe we are only pre-rendering pages for a set of known
51
+ GitHub Repositories, so a Not Found or Unauthorized HTTP error would be unexpected and should stop the build so we can fix the
52
+ issue.
53
+
54
+ In the case of server-rendered Routes (`RouteBuilder.serverRender`), `elm-pages` will show your 500 error page
55
+ when these errors occur.
56
+
57
+ @docs FatalError, fromString, recoverable
58
+
59
+ -}
60
+
61
+ import Pages.Internal.FatalError
62
+
63
+
64
+ {-| -}
65
+ type alias FatalError =
66
+ Pages.Internal.FatalError.FatalError
67
+
68
+
69
+ {-| -}
70
+ build : { title : String, body : String } -> FatalError
71
+ build info =
72
+ Pages.Internal.FatalError.FatalError info
73
+
74
+
75
+ {-| -}
76
+ fromString : String -> FatalError
77
+ fromString string =
78
+ build
79
+ { title = "Custom Error"
80
+ , body = string
81
+ }
82
+
83
+
84
+ {-| -}
85
+ recoverable : { title : String, body : String } -> error -> { fatal : FatalError, recoverable : error }
86
+ recoverable info value =
87
+ { fatal = build info
88
+ , recoverable = value
89
+ }
@@ -3,7 +3,7 @@ module Form.Field exposing
3
3
  , select, range, OutsideRange(..)
4
4
  , date, time, TimeOfDay
5
5
  , Field(..), FieldInfo, exactValue
6
- , required, withClientValidation, withInitialValue, map
6
+ , required, withClientValidation, withInitialValue, withOptionalInitialValue, map
7
7
  , email, password, search, telephone, url, textarea
8
8
  , withMax, withMin, withStep, withMinLength, withMaxLength
9
9
  , No, Yes
@@ -34,7 +34,7 @@ module Form.Field exposing
34
34
 
35
35
  ## Field Configuration
36
36
 
37
- @docs required, withClientValidation, withInitialValue, map
37
+ @docs required, withClientValidation, withInitialValue, withOptionalInitialValue, map
38
38
 
39
39
 
40
40
  ## Text Field Display Options
@@ -67,7 +67,7 @@ type Field error parsed data kind constraints
67
67
 
68
68
  {-| -}
69
69
  type alias FieldInfo error parsed data =
70
- { initialValue : Maybe (data -> String)
70
+ { initialValue : Maybe (data -> Maybe String)
71
71
  , decode : Maybe String -> ( Maybe parsed, List error )
72
72
  , properties : List ( String, Encode.Value )
73
73
  }
@@ -327,7 +327,7 @@ exactValue :
327
327
  }
328
328
  exactValue initialValue error =
329
329
  Field
330
- { initialValue = Just (\_ -> initialValue)
330
+ { initialValue = Just (\_ -> Just initialValue)
331
331
  , decode =
332
332
  \rawValue ->
333
333
  if rawValue == Just initialValue then
@@ -490,10 +490,11 @@ url (Field field _) =
490
490
 
491
491
  {-| -}
492
492
  textarea :
493
- Field error parsed data Input { constraints | plainText : () }
493
+ { rows : Maybe Int, cols : Maybe Int }
494
+ -> Field error parsed data Input { constraints | plainText : () }
494
495
  -> Field error parsed data Input constraints
495
- textarea (Field field _) =
496
- Field field (FieldView.Input FieldView.Textarea)
496
+ textarea options (Field field _) =
497
+ Field field (FieldView.Input (FieldView.Textarea options))
497
498
 
498
499
 
499
500
  {-| -}
@@ -535,7 +536,7 @@ range info field =
535
536
  |> required info.missing
536
537
  |> withMin info.min (info.invalid BelowRange)
537
538
  |> withMax info.max (info.invalid AboveRange)
538
- |> (\(Field innerField _) -> Field { innerField | initialValue = Just (info.initial >> Form.Value.toString) } (FieldView.Input FieldView.Range))
539
+ |> (\(Field innerField _) -> Field { innerField | initialValue = Just (info.initial >> Form.Value.toString >> Just) } (FieldView.Input FieldView.Range))
539
540
 
540
541
 
541
542
  {-| -}
@@ -576,7 +577,18 @@ withInitialValue toInitialValue (Field field kind) =
576
577
  Field
577
578
  { field
578
579
  | initialValue =
579
- Just (toInitialValue >> Form.Value.toString)
580
+ Just (toInitialValue >> Form.Value.toString >> Just)
581
+ }
582
+ kind
583
+
584
+
585
+ {-| -}
586
+ withOptionalInitialValue : (data -> Maybe (Form.Value.Value valueType)) -> Field error value data kind { constraints | initial : valueType } -> Field error value data kind constraints
587
+ withOptionalInitialValue toInitialValue (Field field kind) =
588
+ Field
589
+ { field
590
+ | initialValue =
591
+ Just (toInitialValue >> Maybe.map Form.Value.toString)
580
592
  }
581
593
  kind
582
594
 
@@ -1,16 +1,16 @@
1
1
  module Form.FieldView exposing
2
- ( Input(..), InputType(..), Options(..), input, inputTypeToString, radio, toHtmlProperties, Hidden(..), select
3
- , radioStyled, inputStyled
2
+ ( Input(..), InputType(..), Options(..), input, inputTypeToString, radio, toHtmlProperties, Hidden(..), select, valueButton
3
+ , radioStyled, inputStyled, valueButtonStyled
4
4
  )
5
5
 
6
6
  {-|
7
7
 
8
- @docs Input, InputType, Options, input, inputTypeToString, radio, toHtmlProperties, Hidden, select
8
+ @docs Input, InputType, Options, input, inputTypeToString, radio, toHtmlProperties, Hidden, select, valueButton
9
9
 
10
10
 
11
11
  ## Html.Styled Helpers
12
12
 
13
- @docs radioStyled, inputStyled
13
+ @docs radioStyled, inputStyled, valueButtonStyled
14
14
 
15
15
  -}
16
16
 
@@ -40,7 +40,7 @@ type InputType
40
40
  | Password
41
41
  | Email
42
42
  | Url
43
- | Textarea
43
+ | Textarea { rows : Maybe Int, cols : Maybe Int }
44
44
 
45
45
 
46
46
  {-| -}
@@ -50,7 +50,7 @@ inputTypeToString inputType =
50
50
  Text ->
51
51
  "text"
52
52
 
53
- Textarea ->
53
+ Textarea _ ->
54
54
  "text"
55
55
 
56
56
  Number ->
@@ -103,6 +103,74 @@ type Options a
103
103
  = Options (String -> Maybe a) (List String)
104
104
 
105
105
 
106
+ {-| Gives you a submit button that will submit the form with a specific value for the given Field.
107
+ -}
108
+ valueButton :
109
+ String
110
+ -> List (Html.Attribute msg)
111
+ -> List (Html msg)
112
+ -> Form.Validation.Field error parsed kind
113
+ -> Html msg
114
+ valueButton exactValue attrs children (Validation viewField fieldName _) =
115
+ let
116
+ justViewField : ViewField kind
117
+ justViewField =
118
+ expectViewField viewField
119
+
120
+ rawField : { name : String, value : Maybe String, kind : ( kind, List ( String, Encode.Value ) ) }
121
+ rawField =
122
+ { name = fieldName |> Maybe.withDefault ""
123
+ , value = Just exactValue --justViewField.value
124
+ , kind = justViewField.kind
125
+ }
126
+ in
127
+ case rawField.kind of
128
+ ( _, properties ) ->
129
+ Html.button
130
+ (attrs
131
+ ++ toHtmlProperties properties
132
+ ++ [ Attr.value (rawField.value |> Maybe.withDefault "")
133
+ , Attr.name rawField.name
134
+ ]
135
+ )
136
+ children
137
+
138
+
139
+ {-| Gives you a submit button that will submit the form with a specific value for the given Field.
140
+ -}
141
+ valueButtonStyled :
142
+ String
143
+ -> List (Html.Styled.Attribute msg)
144
+ -> List (Html.Styled.Html msg)
145
+ -> Form.Validation.Field error parsed kind
146
+ -> Html.Styled.Html msg
147
+ valueButtonStyled exactValue attrs children (Validation viewField fieldName _) =
148
+ let
149
+ justViewField : ViewField kind
150
+ justViewField =
151
+ expectViewField viewField
152
+
153
+ rawField : { name : String, value : Maybe String, kind : ( kind, List ( String, Encode.Value ) ) }
154
+ rawField =
155
+ { name = fieldName |> Maybe.withDefault ""
156
+ , value = Just exactValue
157
+ , kind = justViewField.kind
158
+ }
159
+ in
160
+ case rawField.kind of
161
+ ( _, properties ) ->
162
+ Html.Styled.button
163
+ (attrs
164
+ ++ (toHtmlProperties properties |> List.map StyledAttr.fromUnstyled)
165
+ ++ ([ Attr.value (rawField.value |> Maybe.withDefault "")
166
+ , Attr.name rawField.name
167
+ ]
168
+ |> List.map StyledAttr.fromUnstyled
169
+ )
170
+ )
171
+ children
172
+
173
+
106
174
  {-| -}
107
175
  input :
108
176
  List (Html.Attribute msg)
@@ -122,15 +190,21 @@ input attrs (Validation viewField fieldName _) =
122
190
  }
123
191
  in
124
192
  case rawField.kind of
125
- ( Input Textarea, properties ) ->
193
+ ( Input (Textarea { rows, cols }), properties ) ->
126
194
  Html.textarea
127
195
  (attrs
128
196
  ++ toHtmlProperties properties
129
- ++ [ Attr.value (rawField.value |> Maybe.withDefault "")
130
- , Attr.name rawField.name
197
+ ++ ([ rows |> Maybe.map Attr.rows
198
+ , cols |> Maybe.map Attr.cols
199
+ ]
200
+ |> List.filterMap identity
201
+ )
202
+ ++ [ Attr.name rawField.name
131
203
  ]
132
204
  )
133
- []
205
+ [ -- textarea does not support the `value` attribute, but instead uses inner text for its form value
206
+ Html.text (rawField.value |> Maybe.withDefault "")
207
+ ]
134
208
 
135
209
  ( Input inputType, properties ) ->
136
210
  Html.input
@@ -170,17 +244,23 @@ inputStyled attrs (Validation viewField fieldName _) =
170
244
  }
171
245
  in
172
246
  case rawField.kind of
173
- ( Input Textarea, properties ) ->
247
+ ( Input (Textarea { rows, cols }), properties ) ->
174
248
  Html.Styled.textarea
175
249
  (attrs
176
250
  ++ (toHtmlProperties properties |> List.map StyledAttr.fromUnstyled)
177
- ++ ([ Attr.value (rawField.value |> Maybe.withDefault "")
178
- , Attr.name rawField.name
251
+ ++ ([ rows |> Maybe.map StyledAttr.rows
252
+ , cols |> Maybe.map StyledAttr.cols
253
+ ]
254
+ |> List.filterMap identity
255
+ )
256
+ ++ ([ Attr.name rawField.name
179
257
  ]
180
258
  |> List.map StyledAttr.fromUnstyled
181
259
  )
182
260
  )
183
- []
261
+ [ -- textarea does not support the `value` attribute, but instead uses inner text for its form value
262
+ Html.Styled.text (rawField.value |> Maybe.withDefault "")
263
+ ]
184
264
 
185
265
  ( Input inputType, properties ) ->
186
266
  Html.Styled.input