diff --git a/docs/installation.md b/docs/installation.md index 13c1e4fb4..487334170 100644 --- a/docs/installation.md +++ b/docs/installation.md @@ -16,14 +16,14 @@ If you're new to JavaScript or just want a very simple setup to get your feet we ``` -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). +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). --- ### npm ```bash -$ npm install mithril --save +$ npm install mithril ``` TypeScript type definitions are available from DefinitelyTyped. They can be installed with: @@ -42,233 +42,80 @@ $ npm install -D MithrilJS/mithril.d.ts#next --- -### Quick start with Webpack +### Create a project locally -1. Initialize the directory as an npm package -```bash -$ npm init --yes -``` +You can use one of several existing Mithril.js starter templates such as +* [mithril-vite-starter](https://github.com/ArthurClemens/mithril-vite-starter) +* [mithril-esbuild-starter](https://github.com/kevinfiol/mithril-esbuild-starter) +* [mithril-rollup-starter](https://github.com/kevinfiol/mithril-rollup-starter) -2. install required tools +For example, if you'd like to get started with `mithril-esbuild-starter`, run the following commands: ```bash -$ npm install mithril --save -$ npm install webpack webpack-cli --save-dev -``` - -3. Add a "start" entry to the scripts section in `package.json`. -```json -{ - "...": "...", - "scripts": { - "start": "webpack ./src/index.js --output-path ./bin --watch" - } -} -``` - -4. Create `src/index.js` file. -```javascript -import m from "mithril"; -m.render(document.body, "hello world"); -``` - -5. create `index.html` -```html - -
- - -``` - -6. run bundler -```bash -$ npm start -``` - -7. open `index.html` in a browser +# Clone the the template to a directory of your choice +npx degit kevinfiol/mithril-esbuild-starter hello-world -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: -```js -plugins: [ - new webpack.ProvidePlugin({m: "mithril"}), - // ... -] -``` -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. - -#### Step by step - -For production-level projects, the recommended way of installing Mithril.js is to use npm. +# Navigate to newly scaffolded project +cd ./hello-world/ -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. +# Install dependencies +npm install -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`. - -```bash -npm init --yes -# creates a file called package.json +# Build the app and watch for changes +npm run dev ``` -Then, to install Mithril.js, run: +### Quick start with [esbuild](https://esbuild.github.io/) +1. Initialize the directory as an npm package. ```bash -npm install mithril --save -``` - -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 - -You are now ready to start using Mithril. The recommended way to structure code is to modularize it via CommonJS modules: - -```javascript -// index.js -var m = require("mithril") - -m.render(document.body, "hello world") +$ npm init --yes ``` -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. - -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/). - -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. - -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: - +2. Install required tools. ```bash -npm install webpack webpack-cli --save-dev +$ npm install mithril +$ npm install esbuild --save-dev ``` -Open the `package.json` that you created earlier, and add an entry to the `scripts` section: - -```json -{ - "name": "my-project", - "scripts": { - "start": "webpack src/index.js --output bin/app.js -d --watch" +3. Add a "start" entry to the scripts section in `package.json`. + ```json + { + "...": "...", + "scripts": { + "start": "esbuild index.js --bundle --outfile=bin/main.js --watch" + } } -} -``` - -Remember this is a JSON file, so object key names such as `"scripts"` and `"start"` must be inside of double quotes. - -The `-d` flag tells webpack to use development mode, which produces source maps for a better debugging experience. + ``` -The `--watch` flag tells webpack to watch the file system and automatically recreate `app.js` if file changes are detected. + Optionally, if you'd like to use JSX, you can use the `--jsx-factory` and `--jsx-fragment` flags with esbuild. -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. - -``` -npm start -``` - -Now that you have created a bundle, you can then reference the `bin/app.js` file from an HTML file: - -```html - - -