mithril
Version:
A framework for building brilliant applications
216 lines (212 loc) • 11.6 kB
HTML
<head>
<meta charset=UTF-8>
<title> ES6+ on legacy browsers - Mithril.js</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel=stylesheet>
<link href=style.css rel=stylesheet>
<link rel=icon type=image/png sizes=32x32 href=favicon.png>
<meta name=viewport content="width=device-width,initial-scale=1">
</head>
<body>
<header>
<section>
<a class=hamburger href=javascript:;>≡</a>
<h1><img src=logo.svg> Mithril <small>2.0.3</small></h1>
<nav>
<a href=index.html>Guide</a>
<a href=api.html>API</a>
<a href=https://gitter.im/MithrilJS/mithril.js>Chat</a>
<a href=https://github.com/MithrilJS/mithril.js>GitHub</a>
</nav>
</section>
</header>
<main>
<section>
<h1 id=es6+-on-legacy-browsers><a href=#es6+-on-legacy-browsers>ES6+ on legacy browsers</a></h1>
<ul>
<li>Getting Started<ul>
<li><a href=index.html>Introduction</a></li>
<li><a href=installation.html>Installation</a></li>
<li><a href=simple-application.html>Tutorial</a></li>
<li><a href=learning-mithril.html>Learning Resources</a></li>
<li><a href=support.html>Getting Help</a></li>
</ul>
</li>
<li>Resources<ul>
<li><a href=jsx.html>JSX</a></li>
<li><strong><a href=es6.html>ES6+ on legacy browsers</a></strong><ul>
<li><a href=#setup>Setup</a></li>
<li><a href=#using-babel-with-webpack>Using Babel with Webpack</a></li>
</ul>
</li>
<li><a href=animation.html>Animation</a></li>
<li><a href=testing.html>Testing</a></li>
<li><a href=examples.html>Examples</a></li>
<li><a href=integrating-libs.html>3rd Party Integration</a></li>
<li><a href=paths.html>Path Handling</a></li>
</ul>
</li>
<li>Key concepts<ul>
<li><a href=vnodes.html>Vnodes</a></li>
<li><a href=components.html>Components</a></li>
<li><a href=lifecycle-methods.html>Lifecycle methods</a></li>
<li><a href=keys.html>Keys</a></li>
<li><a href=autoredraw.html>Autoredraw system</a></li>
</ul>
</li>
<li>Social<ul>
<li><a href=https://github.com/MithrilJS/mithril.js/wiki/JOBS>Mithril Jobs</a></li>
<li><a href=contributing.html>How to contribute</a></li>
<li><a href=credits.html>Credits</a></li>
<li><a href=code-of-conduct.html>Code of Conduct</a></li>
</ul>
</li>
<li>Misc<ul>
<li><a href=framework-comparison.html>Framework comparison</a></li>
<li><a href=change-log.html>Change log/Migration</a></li>
<li><a href=https://mithril.js.org/archive/v1.1.6/ >v1 Documentation</a></li>
<li><a href=https://mithril.js.org/archive/v0.2.5/ >v0.2 Documentation</a></li>
</ul>
</li>
</ul>
<hr>
<p>Mithril is written in ES5, but it's fully compatible with ES6 and later as well. All modern browsers do support it natively, up to and even including native module syntax. (They don't support Node's magic module resolution, so you can't use <code>import * as _ from "lodash-es"</code> or similar. They just support relative and URL paths.) And so you can feel free to use <a href=components.html>arrow functions for your closure components and classes for your class components</a>.</p>
<p>But, if like many of us, you still need to support older browsers like Internet Explorer, you'll need to transpile that down to ES5, and this is what this page is all about, using <a href=https://babeljs.io>Babel</a> to make modern ES6+ code work on older browsers.</p>
<hr>
<h3 id=setup><a href=#setup>Setup</a></h3>
<p>First, if you haven't already, make sure you have <a href=https://nodejs.org/en/ >Node</a> installed. It comes with <a href=https://www.npmjs.com/ >npm</a> pre-bundled, something we'll need soon.</p>
<p>Once you've got that downloaded, open a terminal and run these commands:</p>
<pre><code class=language-bash># Replace this with the actual path to your project. Quote it if it has spaces,
# and single-quote it if you're on Linux/Mac and it contains a `
<head>
<meta charset=UTF-8>
<title> ES6+ on legacy browsers - Mithril.js</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel=stylesheet>
<link href=style.css rel=stylesheet>
<link rel=icon type=image/png sizes=32x32 href=favicon.png>
<meta name=viewport content="width=device-width,initial-scale=1">
</head>
<body>
<header>
<section>
<a class=hamburger href=javascript:;>≡</a>
<h1><img src=logo.svg> Mithril <small>2.0.3</small></h1>
<nav>
<a href=index.html>Guide</a>
<a href=api.html>API</a>
<a href=https://gitter.im/MithrilJS/mithril.js>Chat</a>
<a href=https://github.com/MithrilJS/mithril.js>GitHub</a>
</nav>
</section>
</header>
<main>
<section>
anywhere.
cd "/path/to/your/project"
# If you have a `package.json` there already, skip this command.
npm init</section></main></code></pre>
<p>Now, you can go one of a couple different routes:</p>
<ul>
<li><a href=#using-babel-standalone>Use Babel standalone, with no bundler at all</a></li>
<li><a href=#using-babel-with-webpack>Use Babel and bundle with Webpack</a></li>
</ul>
<h4 id=using-babel-standalone><a href=#using-babel-standalone>Using Babel standalone</a></h4>
<p>First, we need to install a couple dependencies we need.</p>
<ul>
<li><code>@babel/cli</code> installs the core Babel logic as well as the <code>babel</code> command.</li>
<li><code>@babel/preset-env</code> helps Babel know what to transpile and how to transpile them.</li>
</ul>
<pre><code class=language-bash>npm install @babel/cli @babel/preset-env --save-dev</code></pre>
<p>Now, create a <code>.babelrc</code> file and set up with <code>@babel/preset-env</code>.</p>
<pre><code class=language-json>{
"presets": ["@babel/preset-env"],
"sourceMaps": true
}</code></pre>
<p>And finally, if you have <em>very</em> specific requirements on what you need to support, you may want to <a href=https://github.com/browserslist/browserslist>configure Browserslist</a> so Babel (and other libraries) know what features to target.</p>
<p><em>By default, if you don't configure anything, Browserslist uses a fairly sensible query: <code>> 0.5%, last 2 versions, Firefox ESR, not dead</code>. Unless you have very specific circumstances that require you to change this, like if you need to support IE 8 with a lot of polyfills, don't bother with this step.</em></p>
<p>Whenever you want to compile your project, run this command, and everything will be compiled.</p>
<pre><code class=language-bash>babel src --out-dir dist</code></pre>
<p>You may find it convenient to use an npm script so you're not having to remember this and typing it out every time. Add a <code>"build"</code> field to the <code>"scripts"</code> object in your <code>package.json</code>:</p>
<pre><code class=language-json>{
"scripts": {
"build": "babel src --out-dir dist"
}
}</code></pre>
<p>And now, the command is a little easier to type and remember.</p>
<pre><code class=language-bash>npm run build</code></pre>
<h4 id=using-babel-with-webpack><a href=#using-babel-with-webpack>Using Babel with Webpack</a></h4>
<p>If you want to use Webpack to bundle, it's a few more steps to set up. First, we need to install all the dependencies we need for both Babel and Webpack.</p>
<ul>
<li><code>webpack</code> is the core Webpack code and <code>webpack-cli</code> gives you the <code>webpack</code> command.</li>
<li><code>@babel/core</code> is the core Babel code, a peer dependency for <code>babel-loader</code>.</li>
<li><code>babel-loader</code> lets you teach Webpack how to use Babel to transpile your files.</li>
<li><code>@babel/preset-env</code> helps Babel know what to transpile and how to transpile them.</li>
</ul>
<pre><code class=language-bash>npm install webpack webpack-cli @babel/core babel-loader @babel/preset-env --save-dev</code></pre>
<p>Now, create a <code>.babelrc</code> file and set up with <code>@babel/preset-env</code>.</p>
<pre><code class=language-json>{
"presets": ["@babel/preset-env"],
"sourceMaps": true
}</code></pre>
<p>Next, if you have <em>very</em> specific requirements on what you need to support, you may want to <a href=https://github.com/browserslist/browserslist>configure Browserslist</a> so Babel (and other libraries) know what features to target.</p>
<p><em>By default, if you don't configure anything, Browserslist uses a fairly sensible query: <code>> 0.5%, last 2 versions, Firefox ESR, not dead</code>. Unless you have very specific circumstances that require you to change this, like if you need to support IE 8 with a lot of polyfills, don't bother with this step.</em></p>
<p>And finally, set up Webpack by creating a file called <code>webpack.config.js</code>.</p>
<pre><code class=language-javascript>const path = require('path')
module.exports = {
entry: path.resolve(__dirname, 'src/index.js'),
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.js',
},
module: {
loaders: [{
test: /\.js$/,
exclude: /\/node_modules\//,
loader: 'babel-loader'
}]
}
}</code></pre>
<p>This configuration assumes the source code file for the application entry point is in <code>src/index.js</code>, and this will output the bundle to <code>dist/app.js</code>.</p>
<p>Now, to run the bundler, you just run this command:</p>
<pre><code class=language-bash>webpack -d --watch</code></pre>
<p>You may find it convenient to use an npm script so you're not having to remember this and typing it out every time. Add a <code>"build"</code> field to the <code>"scripts"</code> object in your <code>package.json</code>:</p>
<pre><code class=language-json>{
"scripts": {
"start": "webpack -d --watch"
}
}</code></pre>
<p>And now, the command is a little easier to type and remember.</p>
<pre><code class=language-bash>npm start</code></pre>
<p>For production builds, you'll want to minify your scripts. Luckily, this is also pretty easy: it's just running Webpack with a different option.</p>
<pre><code class=language-bash>webpack -p</code></pre>
<p>You may want to also add this to your npm scripts, so you can build it quickly and easily.</p>
<pre><code class=language-json>{
"scripts": {
"start": "webpack -d --watch",
"build": "webpack -p"
}
}</code></pre>
<p>And then running this is a little easier to remember.</p>
<pre><code class=language-bash>npm run build</code></pre>
<p>And of course, you can do this in automatic production build scripts, too. Here's how it might look if you're using <a href=https://www.heroku.com/ >Heroku</a>, for example:</p>
<pre><code class=language-json>{
"scripts": {
"start": "webpack -d --watch",
"build": "webpack -p",
"heroku-postbuild": "webpack -p"
}
}</code></pre>
<hr>
<small>License: MIT. © Leo Horie.</small>
</section>
</main>
<script src=https://cdnjs.cloudflare.com/ajax/libs/prism/1.6.0/prism.min.js defer></script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/prism/1.6.0/components/prism-jsx.min.js defer></script>
<script src=https://unpkg.com/mithril@2.0.3/mithril.js async></script>
<script>
document.querySelector(".hamburger").onclick = function() {
document.body.className = document.body.className === "navigating" ? "" : "navigating"
document.querySelector("h1 + ul").onclick = function() {
document.body.className = ''
}
}
</script>