You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: guides/Getting-Started.md
+93-6Lines changed: 93 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -6,13 +6,17 @@ We'll start with the installation of the compiler and Spago build tool, and then
6
6
7
7
#### Installing the Compiler
8
8
9
-
You'll need [Node.js and npm](https://docs.npmjs.com/getting-started/installing-node)and to be [able to install global packages](https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-1-change-the-permission-to-npm-s-default-directory) to proceed.
9
+
You'll need to install [Node.js and npm](https://docs.npmjs.com/getting-started/installing-node). We recommend installing [Node.js and npm via a node version manager](https://docs.npmjs.com/getting-started/installing-node)to avoid issues with installing packages globally. If you choose to install it manually, you might experience the [`EACCES` error when installing packages globally](https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-1-change-the-permission-to-npm-s-default-directory).
10
10
11
-
The Purescript compiler (`purs`) can be installed with npm:
11
+
Install the Purescript compiler (`purs`) with npm:
12
12
13
13
npm install -g purescript
14
14
15
-
(It can also be installed from [Hackage](http://hackage.haskell.org/package/purescript), or by downloading the latest [binary bundle](https://github.com/purescript/purescript/releases) for your OS. If you do so, make sure the `purs` executable is on your `$PATH`.)
15
+
Try running the PureScript compiler on the command line to verify that the PureScript compiler executables are available on your `$PATH`:
16
+
17
+
purs
18
+
19
+
It can also be installed from [Hackage](http://hackage.haskell.org/package/purescript), or by downloading the latest [binary bundle](https://github.com/purescript/purescript/releases) for your OS. If you do so, make sure the `purs` executable is on your `$PATH`.
16
20
17
21
#### Setting up the Development Environment
18
22
@@ -24,6 +28,8 @@ If you don't have Spago installed, install it now:
24
28
25
29
Create a new project in an empty directory using `spago init`:
26
30
31
+
mkdir my-project
32
+
cd my-project
27
33
spago init
28
34
29
35
Your directory should now contain the following files:
@@ -50,10 +56,12 @@ If everything was built successfully, and the tests ran without problems, then t
50
56
51
57
#### Installing Dependencies
52
58
53
-
Dependencies can be installed using Spago. We will be using the `purescript-lists` library shortly, so install it now:
59
+
Dependencies can be installed using Spago. We will be using the `lists` library shortly, so install it now:
54
60
55
61
spago install lists
56
62
63
+
The `lists` library sources should now be available in the `.spago/lists/{version}/` subdirectory, and will be included when you compile your project.
64
+
57
65
#### Working in PSCI
58
66
59
67
PSCi is the interactive mode of PureScript. It is useful for working with pure computations, and for testing ideas.
@@ -172,6 +180,11 @@ multiples = filter (\n -> mod n 3 == 0 || mod n 5 == 0) ns
172
180
answer = sum multiples
173
181
```
174
182
183
+
This sample illustrates a few key ideas regarding modules:
184
+
185
+
- Every file begins with a module header. A module name consists of one or more capitalized words separated by dots. In this case, only a single word is used, but `My.First.Module` would be an equally valid module name.
186
+
- Modules are imported using their full names, including dots to separate the parts of the module name. Here, we import the `Prelude` module, which provides `mod`, `==`, and many other common functions. We also import `Data.List` which provides the explicitly-listed `range` and `filter` functions. We can either import all functions in a module implicitly, as is done with `Prelude`, or list them explicitly. Guidelines are to only have one module with implicit imports.
187
+
175
188
It is possible to load this file directly into the REPL and to continue working:
176
189
177
190
spago repl
@@ -191,7 +204,7 @@ The compiler will display several warnings about missing type declarations. In g
191
204
192
205
#### Writing a Test Suite
193
206
194
-
To test our code, we'll use the `purescript-assert` library:
207
+
To test our code, we'll use the `assert` library:
195
208
196
209
spago install assert
197
210
@@ -231,10 +244,84 @@ main = do
231
244
232
245
The `spago run` command can be used to compile and run the `Main` module:
233
246
234
-
> spago run
247
+
$ spago run
235
248
[info] Build succeeded.
236
249
The answer is 233168
237
250
251
+
252
+
#### Compiling for the Browser
253
+
254
+
Spago can be used to turn our PureScript code into JavaScript suitable for use in the web browser by using the `spago bundle-app` command:
255
+
256
+
$ spago bundle-app
257
+
...
258
+
Build succeeded.
259
+
Bundle succeeded and output file to index.js
260
+
261
+
All the code in the `src` directory and any project dependencies have been compiled to JavaScript. The resulting code is bundled as `index.js` and has also had any unused code removed, a process known as dead code elimination. This `index.js` file can now be included in an HTML document. If you try this, you should see the words "Hello, World!" printed to your browser's console.
262
+
263
+
If you open `index.js`, you should see a few compiled modules which look like this:
264
+
265
+
```javascript
266
+
// Generated by purs bundle 0.13.6
267
+
varPS= {};
268
+
269
+
// ...
270
+
271
+
(function($PS) {
272
+
"use strict";
273
+
$PS["Euler"] =$PS["Euler"] || {};
274
+
varexports=$PS["Euler"];
275
+
var Data_EuclideanRing =$PS["Data.EuclideanRing"];
var answer =Data_Foldable.sum(Data_List_Types.foldableList)(Data_Semiring.semiringInt)(multiples);
285
+
exports["answer"] = answer;
286
+
})(PS);
287
+
288
+
(function($PS) {
289
+
// Generated by purs version 0.13.6
290
+
"use strict";
291
+
$PS["Main"] =$PS["Main"] || {};
292
+
varexports=$PS["Main"];
293
+
var Data_Show =$PS["Data.Show"];
294
+
var Effect_Console =$PS["Effect.Console"];
295
+
var Euler =$PS["Euler"];
296
+
var main =Effect_Console.log("The answer is "+Data_Show.show(Data_Show.showInt)(Euler.answer));
297
+
exports["main"] = main;
298
+
})(PS);
299
+
300
+
PS["Main"].main();
301
+
```
302
+
303
+
This illustrates a few points about the way the PureScript compiler generates JavaScript code:
304
+
305
+
- Every module gets turned into an object, created by a wrapper function, which contains the module's exported members.
306
+
- PureScript tries to preserve the names of variables wherever possible.
307
+
- Function applications in PureScript get turned into function applications in JavaScript.
308
+
- The main method is run after all modules have been defined and is generated as a simple method call with no arguments.
309
+
- PureScript code does not rely on any runtime libraries. All of the code that is generated by the compiler originated in a PureScript module somewhere which your code depended on.
310
+
311
+
These points are important since they mean that PureScript generates simple, understandable code. The code generation process, in general, is quite a shallow transformation. It takes relatively little understanding of the language to predict what JavaScript code will be generated for a particular input.
312
+
313
+
#### Compiling CommonJS Modules
314
+
315
+
Spago can also be used to generate CommonJS modules from PureScript code. This can be useful when using NodeJS, or just when developing a larger project which uses CommonJS modules to break code into smaller components.
316
+
317
+
To build CommonJS modules, use the `spago build` command:
318
+
319
+
$ spago build
320
+
...
321
+
Build succeeded.
322
+
323
+
The generated modules will be placed in the `output` directory by default. Each PureScript module will be compiled to its own CommonJS module, in its own subdirectory.
324
+
238
325
#### What Next?
239
326
240
327
If you're new to typed functional programming, your next stop should be [PureScript by Example](https://book.purescript.org/), which will walk you through learning PureScript by solving practical problems.
0 commit comments