Skip to content

Commit 15dea75

Browse files
kebdead-claudia
keb
authored andcommitted
update installation page
1 parent f6cc4e9 commit 15dea75

File tree

1 file changed

+52
-205
lines changed

1 file changed

+52
-205
lines changed

docs/installation.md

Lines changed: 52 additions & 205 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ If you're new to JavaScript or just want a very simple setup to get your feet we
1616
<script src="https://unpkg.com/mithril/mithril.js"></script>
1717
```
1818

19-
If you would like to try out mithril without setting up a local environment, you can easily use an online playground at [flems.io/mithril](https://flems.io/mithril).
19+
If you would like to try out Mithril.js without setting up a local environment, you can easily use an online playground at [flems.io/mithril](https://flems.io/mithril).
2020

2121
---
2222

2323
### npm
2424

2525
```bash
26-
$ npm install mithril --save
26+
$ npm install mithril
2727
```
2828

2929
TypeScript type definitions are available from DefinitelyTyped. They can be installed with:
@@ -42,233 +42,80 @@ $ npm install -D MithrilJS/mithril.d.ts#next
4242

4343
---
4444

45-
### Quick start with Webpack
45+
### Create a project locally
4646

47-
1. Initialize the directory as an npm package
48-
```bash
49-
$ npm init --yes
50-
```
47+
You can use one of several existing Mithril.js starter templates such as
48+
* [mithril-vite-starter](https://github.com/ArthurClemens/mithril-vite-starter)
49+
* [mithril-esbuild-starter](https://github.com/kevinfiol/mithril-esbuild-starter)
50+
* [mithril-rollup-starter](https://github.com/kevinfiol/mithril-rollup-starter)
5151

52-
2. install required tools
52+
For example, if you'd like to get started with `mithril-esbuild-starter`, run the following commands:
5353
```bash
54-
$ npm install mithril --save
55-
$ npm install webpack webpack-cli --save-dev
56-
```
57-
58-
3. Add a "start" entry to the scripts section in `package.json`.
59-
```json
60-
{
61-
"...": "...",
62-
"scripts": {
63-
"start": "webpack ./src/index.js --output-path ./bin --watch"
64-
}
65-
}
66-
```
67-
68-
4. Create `src/index.js` file.
69-
```javascript
70-
import m from "mithril";
71-
m.render(document.body, "hello world");
72-
```
73-
74-
5. create `index.html`
75-
```html
76-
<!DOCTYPE html>
77-
<body>
78-
<script src="bin/main.js"></script>
79-
</body>
80-
```
81-
82-
6. run bundler
83-
```bash
84-
$ npm start
85-
```
86-
87-
7. open `index.html` in a browser
54+
# Clone the the template to a directory of your choice
55+
npx degit kevinfiol/mithril-esbuild-starter hello-world
8856

89-
Optionally, you can include Mithril.js as a global variable using Webpack's provide plugin, to avoid including `import m from "mithril"` across a large number of files:
90-
```js
91-
plugins: [
92-
new webpack.ProvidePlugin({m: "mithril"}),
93-
// ...
94-
]
95-
```
96-
Then, you could remove the import line from step 4 (don't forget to restart Webpack if you ran it with `--watch`), and it will work just the same.
97-
98-
#### Step by step
99-
100-
For production-level projects, the recommended way of installing Mithril.js is to use npm.
57+
# Navigate to newly scaffolded project
58+
cd ./hello-world/
10159

102-
npm is the default package manager that is bundled with Node.js. It is widely used as the package manager for both client-side and server-side libraries in the JavaScript ecosystem. Download and install [Node](https://nodejs.org); npm is bundled with that and installed alongside it.
60+
# Install dependencies
61+
npm install
10362

104-
To use Mithril.js via npm, go to your project folder, and run `npm init --yes` from the command line. This will create a file called `package.json`.
105-
106-
```bash
107-
npm init --yes
108-
# creates a file called package.json
63+
# Build the app and watch for changes
64+
npm run dev
10965
```
11066

111-
Then, to install Mithril.js, run:
67+
### Quick start with [esbuild](https://esbuild.github.io/)
11268

69+
1. Initialize the directory as an npm package.
11370
```bash
114-
npm install mithril --save
115-
```
116-
117-
This will create a folder called `node_modules`, and a `mithril` folder inside of it. It will also add an entry under `dependencies` in the `package.json` file
118-
119-
You are now ready to start using Mithril. The recommended way to structure code is to modularize it via CommonJS modules:
120-
121-
```javascript
122-
// index.js
123-
var m = require("mithril")
124-
125-
m.render(document.body, "hello world")
71+
$ npm init --yes
12672
```
12773

128-
Modularization is the practice of separating the code into files. Doing so makes it easier to find code, understand what code relies on what code, and test.
129-
130-
CommonJS is a de-facto standard for modularizing JavaScript code, and it's used by Node.js, as well as tools like [Browserify](https://browserify.org/) and [Webpack](https://webpack.js.org/). It's a robust, battle-tested precursor to ES6 modules. Although the syntax for ES6 modules is specified in Ecmascript 6, the actual module loading mechanism is not. If you wish to use ES6 modules despite the non-standardized status of module loading, you can use tools like [Rollup](https://rollupjs.org/) or [Babel](https://babeljs.io/).
131-
132-
Most browser today do not natively support modularization systems (CommonJS or ES6), so modularized code must be bundled into a single JavaScript file before running in a client-side application.
133-
134-
A popular way for creating a bundle is to setup an npm script for [Webpack](https://webpack.js.org/). To install Webpack, run this from the command line:
135-
74+
2. Install required tools.
13675
```bash
137-
npm install webpack webpack-cli --save-dev
76+
$ npm install mithril
77+
$ npm install esbuild --save-dev
13878
```
13979

140-
Open the `package.json` that you created earlier, and add an entry to the `scripts` section:
141-
142-
```json
143-
{
144-
"name": "my-project",
145-
"scripts": {
146-
"start": "webpack src/index.js --output bin/app.js -d --watch"
80+
3. Add a "start" entry to the scripts section in `package.json`.
81+
```json
82+
{
83+
"...": "...",
84+
"scripts": {
85+
"start": "esbuild index.js --bundle --outfile=bin/main.js --watch"
86+
}
14787
}
148-
}
149-
```
150-
151-
Remember this is a JSON file, so object key names such as `"scripts"` and `"start"` must be inside of double quotes.
152-
153-
The `-d` flag tells webpack to use development mode, which produces source maps for a better debugging experience.
88+
```
15489

155-
The `--watch` flag tells webpack to watch the file system and automatically recreate `app.js` if file changes are detected.
90+
Optionally, if you'd like to use JSX, you can use the `--jsx-factory` and `--jsx-fragment` flags with esbuild.
15691

157-
Now you can run the script via `npm start` in your command line window. This looks up the `webpack` command in the npm path, reads `index.js` and creates a file called `app.js` which includes both Mithril.js and the `hello world` code above. If you want to run the `webpack` command directly from the command line, you need to either add `node_modules/.bin` to your PATH, or install webpack globally via `npm install webpack -g`. It's, however, recommended that you always install webpack locally and use npm scripts, to ensure builds are reproducible in different computers.
158-
159-
```
160-
npm start
161-
```
162-
163-
Now that you have created a bundle, you can then reference the `bin/app.js` file from an HTML file:
164-
165-
```html
166-
<html>
167-
<head>
168-
<title>Hello world</title>
169-
</head>
170-
<body>
171-
<script src="bin/app.js"></script>
172-
</body>
173-
</html>
174-
```
175-
176-
As you've seen above, importing a module in CommonJS is done via the `require` function. You can reference npm modules by their library names (e.g. `require("mithril")` or `require("jquery")`), and you can reference your own modules via relative paths minus the file extension (e.g. if you have a file called `mycomponent.js` in the same folder as the file you're importing to, you can import it by calling `require("./mycomponent")`).
177-
178-
To export a module, assign what you want to export to the special `module.exports` object:
179-
180-
```javascript
181-
// mycomponent.js
182-
module.exports = {
183-
view: function() {return "hello from a module"}
184-
}
185-
```
186-
187-
In the `index.js`, you would then write this code to import that module:
92+
```json
93+
{
94+
"...": "...",
95+
"scripts": {
96+
"start": "esbuild index.js --bundle --outfile=bin/main.js --jsx-factory=m --jsx-fragment='\"[\"' --watch"
97+
}
98+
}
99+
```
188100

101+
4. Create `index.js` file.
189102
```javascript
190-
// index.js
191-
var m = require("mithril")
192-
193-
var MyComponent = require("./mycomponent")
194-
195-
m.mount(document.body, MyComponent)
196-
```
197-
198-
Note that in this example, we're using `m.mount`, which wires up the component to Mithril.js' autoredraw system. In most applications, you will want to use `m.mount` (or `m.route` if your application has multiple screens) instead of `m.render` to take advantage of the autoredraw system, rather than re-rendering manually every time a change occurs.
199-
200-
#### Production build
201-
202-
If you open bin/app.js, you'll notice that the Webpack bundle is not minified, so this file is not ideal for a live application. To generate a minified file, open `package.json` and add a new npm script:
203-
204-
```json
205-
{
206-
"name": "my-project",
207-
"scripts": {
208-
"start": "webpack src/index.js --output bin/app.js -d --watch",
209-
"build": "webpack src/index.js --output bin/app.js -p"
210-
}
211-
}
103+
import m from "mithril";
104+
m.render(document.getElementById("app"), "hello world");
212105
```
213106

214-
You can use hooks in your production environment to run the production build script automatically. Here's an example for [Heroku](https://www.heroku.com/):
215-
216-
```json
217-
{
218-
"name": "my-project",
219-
"scripts": {
220-
"start": "webpack -d --watch",
221-
"build": "webpack -p",
222-
"heroku-postbuild": "webpack -p"
223-
}
224-
}
107+
5. Create `index.html` file.
108+
```html
109+
<!DOCTYPE html>
110+
<body>
111+
<div id="app"></div>
112+
<script src="bin/main.js"></script>
113+
</body>
225114
```
226115

227-
---
228-
229-
### Alternate ways to use Mithril.js
230-
231-
#### Live reload development environment
232-
233-
Live reload is a feature where code changes automatically trigger the page to reload. [Budo](https://github.com/mattdesl/budo) is one tool that enables live reloading.
234-
116+
6. Run your bundler script.
235117
```bash
236-
# 1) install
237-
npm install mithril --save
238-
npm install budo -g
239-
240-
# 2) add this line into the scripts section in package.json
241-
# "scripts": {
242-
# "start": "budo --live --open index.js"
243-
# }
244-
245-
# 3) create an `index.js` file
246-
247-
# 4) run budo
248-
npm start
118+
$ npm run start
249119
```
250120

251-
The source file `index.js` will be compiled (bundled) and a browser window opens showing the result. Any changes in the source files will instantly get recompiled and the browser will refresh reflecting the changes.
252-
253-
#### Vanilla
254-
255-
If you don't have the ability to run a bundler script due to company security policies, there's an options to not use a module system at all:
256-
257-
```html
258-
<html>
259-
<head>
260-
<title>Hello world</title>
261-
</head>
262-
<body>
263-
<script src="https://unpkg.com/mithril/mithril.js"></script>
264-
<script src="index.js"></script>
265-
</body>
266-
</html>
267-
```
268-
269-
```javascript
270-
// index.js
271-
272-
// if a CommonJS environment is not detected, Mithril.js will be created in the global scope
273-
m.render(document.body, "hello world")
274-
```
121+
7. Open `index.html` in a browser. You should see `hello world` rendered on your page.

0 commit comments

Comments
 (0)