elm-pages 3.0.0-beta.12 → 3.0.0-beta.14

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 (77) hide show
  1. package/README.md +1 -1
  2. package/codegen/elm-pages-codegen.js +1496 -1126
  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/Runner.elm.js +151 -39
  11. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  12. package/generator/dead-code-review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  13. package/generator/dead-code-review/elm.json +3 -2
  14. package/generator/dead-code-review/src/Pages/Review/DeadCodeEliminateData.elm +58 -10
  15. package/generator/dead-code-review/tests/Pages/Review/DeadCodeEliminateDataTest.elm +45 -29
  16. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/d.dat +0 -0
  17. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/i.dat +0 -0
  18. package/generator/review/elm-stuff/tests-0.19.1/elm-stuff/0.19.1/o.dat +0 -0
  19. package/generator/review/elm-stuff/tests-0.19.1/elm.json +1 -1
  20. package/generator/review/elm-stuff/tests-0.19.1/js/Runner.elm.js +45 -4
  21. package/generator/review/elm-stuff/tests-0.19.1/js/node_runner.js +1 -1
  22. package/generator/review/elm-stuff/tests-0.19.1/js/node_supervisor.js +1 -1
  23. package/generator/review/elm.json +3 -3
  24. package/generator/src/RouteBuilder.elm +66 -52
  25. package/generator/src/SharedTemplate.elm +3 -2
  26. package/generator/src/SiteConfig.elm +3 -2
  27. package/generator/src/build.js +6 -6
  28. package/generator/src/cli.js +12 -7
  29. package/generator/src/compatibility-key.js +1 -1
  30. package/generator/src/dev-server.js +7 -7
  31. package/generator/src/render-test.js +109 -0
  32. package/generator/src/render.js +77 -51
  33. package/generator/src/request-cache.js +149 -158
  34. package/generator/template/app/Api.elm +2 -2
  35. package/generator/template/app/Route/Index.elm +3 -3
  36. package/generator/template/app/Shared.elm +3 -3
  37. package/generator/template/app/Site.elm +3 -3
  38. package/package.json +11 -11
  39. package/src/ApiRoute.elm +63 -57
  40. package/src/BackendTask/Env.elm +87 -0
  41. package/src/{DataSource → BackendTask}/File.elm +89 -43
  42. package/src/{DataSource → BackendTask}/Glob.elm +134 -125
  43. package/src/BackendTask/Http.elm +637 -0
  44. package/src/{DataSource → BackendTask}/Internal/Glob.elm +1 -1
  45. package/src/BackendTask/Internal/Request.elm +28 -0
  46. package/src/BackendTask/Port.elm +202 -0
  47. package/src/{DataSource.elm → BackendTask.elm} +223 -207
  48. package/src/Exception.elm +37 -0
  49. package/src/Form.elm +20 -20
  50. package/src/Head.elm +7 -7
  51. package/src/Internal/ApiRoute.elm +7 -5
  52. package/src/PageServerResponse.elm +6 -1
  53. package/src/Pages/Generate.elm +35 -63
  54. package/src/Pages/Internal/Platform/Cli.elm +422 -731
  55. package/src/Pages/Internal/Platform/Cli.elm.bak +22 -22
  56. package/src/Pages/Internal/Platform/CompatibilityKey.elm +1 -1
  57. package/src/Pages/Internal/Platform/Effect.elm +0 -1
  58. package/src/Pages/Internal/Platform/GeneratorApplication.elm +53 -113
  59. package/src/Pages/Internal/Platform/StaticResponses.elm +72 -256
  60. package/src/Pages/Internal/Platform/ToJsPayload.elm +4 -4
  61. package/src/Pages/Internal/Platform.elm +25 -31
  62. package/src/Pages/Internal/Script.elm +17 -0
  63. package/src/Pages/Internal/StaticHttpBody.elm +35 -1
  64. package/src/Pages/Manifest.elm +5 -4
  65. package/src/Pages/ProgramConfig.elm +8 -7
  66. package/src/Pages/Script.elm +34 -25
  67. package/src/Pages/SiteConfig.elm +3 -2
  68. package/src/Pages/StaticHttp/Request.elm +2 -2
  69. package/src/Pages/StaticHttpRequest.elm +37 -90
  70. package/src/RequestsAndPending.elm +8 -19
  71. package/src/Server/Request.elm +14 -14
  72. package/src/Server/Session.elm +34 -34
  73. package/src/Server/SetCookie.elm +1 -1
  74. package/src/DataSource/Env.elm +0 -62
  75. package/src/DataSource/Http.elm +0 -446
  76. package/src/DataSource/Internal/Request.elm +0 -20
  77. package/src/DataSource/Port.elm +0 -90
@@ -0,0 +1,202 @@
1
+ module BackendTask.Port exposing
2
+ ( get
3
+ , Error(..)
4
+ )
5
+
6
+ {-|
7
+
8
+ @docs get
9
+
10
+ @docs Error
11
+
12
+ -}
13
+
14
+ import BackendTask
15
+ import BackendTask.Http
16
+ import BackendTask.Internal.Request
17
+ import Exception exposing (Catchable)
18
+ import Json.Decode as Decode exposing (Decoder)
19
+ import Json.Encode as Encode
20
+ import TerminalText
21
+
22
+
23
+ {-| In a vanilla Elm application, ports let you either send or receive JSON data between your Elm application and the JavaScript context in the user's browser at runtime.
24
+
25
+ With `BackendTask.Port`, you send and receive JSON to JavaScript running in NodeJS during build-time. This means that you can call shell scripts, or run NPM packages that are installed, or anything else you could do with NodeJS.
26
+
27
+ A `BackendTask.Port` will call an async JavaScript function with the given name. The function receives the input JSON value, and the Decoder is used to decode the return value of the async function.
28
+
29
+ Here is the Elm code and corresponding JavaScript definition for getting an environment variable (or a build error if it isn't found).
30
+
31
+ import BackendTask exposing (BackendTask)
32
+ import BackendTask.Port
33
+ import Json.Encode
34
+ import OptimizedDecoder as Decode
35
+
36
+ data : BackendTask String
37
+ data =
38
+ BackendTask.Port.get "environmentVariable"
39
+ (Json.Encode.string "EDITOR")
40
+ Decode.string
41
+
42
+ -- will resolve to "VIM" if you run `EDITOR=vim elm-pages dev`
43
+
44
+ ```javascript
45
+ const kleur = require("kleur");
46
+
47
+
48
+ module.exports =
49
+ /**
50
+ * @param { unknown } fromElm
51
+ * @returns { Promise<unknown> }
52
+ */
53
+ {
54
+ environmentVariable: async function (name) {
55
+ const result = process.env[name];
56
+ if (result) {
57
+ return result;
58
+ } else {
59
+ throw `No environment variable called ${kleur
60
+ .yellow()
61
+ .underline(name)}\n\nAvailable:\n\n${Object.keys(process.env).join(
62
+ "\n"
63
+ )}`;
64
+ }
65
+ },
66
+ }
67
+ ```
68
+
69
+
70
+ ## Error Handling
71
+
72
+ `port-data-source.js`
73
+
74
+ Any time you throw an exception from a BackendTask.Port definition, it will result in a build error in your `elm-pages build` or dev server. In the example above, if the environment variable
75
+ is not found it will result in a build failure. Notice that the NPM package `kleur` is being used in this example to add color to the output for that build error. You can use any tool you
76
+ prefer to add ANSI color codes within the error string in an exception and it will show up with color output in the build output and dev server.
77
+
78
+
79
+ ## Performance
80
+
81
+ As with any JavaScript or NodeJS code, avoid doing blocking IO operations. For example, avoid using `fs.readFileSync`, because blocking IO can slow down your elm-pages builds and dev server.
82
+
83
+ -}
84
+ get : String -> Encode.Value -> Decoder b -> BackendTask.BackendTask (Catchable Error) b
85
+ get portName input decoder =
86
+ BackendTask.Internal.Request.request
87
+ { name = "port"
88
+ , body =
89
+ Encode.object
90
+ [ ( "input", input )
91
+ , ( "portName", Encode.string portName )
92
+ ]
93
+ |> BackendTask.Http.jsonBody
94
+ , expect =
95
+ Decode.oneOf
96
+ [ Decode.field "elm-pages-internal-error" Decode.string
97
+ |> Decode.andThen
98
+ (\errorKind ->
99
+ if errorKind == "PortNotDefined" then
100
+ Exception.Catchable (PortNotDefined { name = portName })
101
+ { title = "Port Error"
102
+ , body =
103
+ [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I expected to find a port named `"
104
+ , TerminalText.yellow portName
105
+ , TerminalText.text "` but I couldn't find it. Is the function exported in your port-data-source file?"
106
+ ]
107
+ |> TerminalText.toString
108
+ }
109
+ |> Decode.succeed
110
+
111
+ else if errorKind == "ExportIsNotFunction" then
112
+ Decode.field "error" Decode.string
113
+ |> Decode.maybe
114
+ |> Decode.map (Maybe.withDefault "")
115
+ |> Decode.map
116
+ (\incorrectPortType ->
117
+ Exception.Catchable ExportIsNotFunction
118
+ { title = "Port Error"
119
+ , body =
120
+ [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I found an export called `"
121
+ , TerminalText.yellow portName
122
+ , TerminalText.text "` but I expected its type to be function, but instead its type was: "
123
+ , TerminalText.red incorrectPortType
124
+ ]
125
+ |> TerminalText.toString
126
+ }
127
+ )
128
+
129
+ else if errorKind == "MissingPortsFile" then
130
+ Exception.Catchable MissingPortsFile
131
+ { title = "Port Error"
132
+ , body =
133
+ [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I couldn't find your port-data-source file. Be sure to create a 'port-data-source.ts' or 'port-data-source.js' file."
134
+ ]
135
+ |> TerminalText.toString
136
+ }
137
+ |> Decode.succeed
138
+
139
+ else if errorKind == "ErrorInPortsFile" then
140
+ Decode.field "error" Decode.string
141
+ |> Decode.maybe
142
+ |> Decode.map (Maybe.withDefault "")
143
+ |> Decode.map
144
+ (\errorMessage ->
145
+ Exception.Catchable
146
+ ErrorInPortsFile
147
+ { title = "Port Error"
148
+ , body =
149
+ [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I couldn't import the port definitions file, because of this exception:\n\n"
150
+ , TerminalText.red errorMessage
151
+ , TerminalText.text "\n\nAre there syntax errors or exceptions thrown during import?"
152
+ ]
153
+ |> TerminalText.toString
154
+ }
155
+ )
156
+
157
+ else if errorKind == "PortCallError" then
158
+ Decode.field "error" Decode.value
159
+ |> Decode.maybe
160
+ |> Decode.map (Maybe.withDefault Encode.null)
161
+ |> Decode.map
162
+ (\portCallError ->
163
+ Exception.Catchable
164
+ (PortCallError portCallError)
165
+ { title = "Port Error"
166
+ , body =
167
+ [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I couldn't import the port definitions file, because of this exception:\n\n"
168
+ , TerminalText.red (Encode.encode 2 portCallError)
169
+ , TerminalText.text "\n\nAre there syntax errors or exceptions thrown during import?"
170
+ ]
171
+ |> TerminalText.toString
172
+ }
173
+ )
174
+
175
+ else
176
+ Exception.Catchable ErrorInPortsFile
177
+ { title = "Port Error"
178
+ , body =
179
+ [ TerminalText.text "Something went wrong in a call to BackendTask.Port.get. I expected to find a port named `"
180
+ , TerminalText.yellow portName
181
+ , TerminalText.text "`."
182
+ ]
183
+ |> TerminalText.toString
184
+ }
185
+ |> Decode.succeed
186
+ )
187
+ |> Decode.map Err
188
+ , decoder |> Decode.map Ok
189
+ ]
190
+ |> BackendTask.Http.expectJson
191
+ }
192
+ |> BackendTask.andThen BackendTask.fromResult
193
+
194
+
195
+ {-| -}
196
+ type Error
197
+ = Error
198
+ | ErrorInPortsFile
199
+ | MissingPortsFile
200
+ | PortNotDefined { name : String }
201
+ | PortCallError Decode.Value
202
+ | ExportIsNotFunction