I studied a lot the way front-end developers are working in general with newest tools that exist in the Javascript and web development world, and lately lines have moved a lot to improve code organization, automatic processes (including testing) and even code writing itself so I wanted to share my findings with you guys, so we can decide which direction to go. Of course I have an opinion :) But before doing any pull request I prefer to explain first.
MXCuBE 3 client-side code is depending on React, Bootstrap, and many other little libraries. There is going to be more and more dependencies as features grow, which is normal. On the server dependencies are handled in requirements.txt and distutils in general, that's great - I always wanted to have a similar mechanism for Javascript code. I realized it exists, in different forms... It turns out people are setting up tool chains to handle dependencies (and much more), mixing tools like bower, grunt, gulp, yeoman, browserify, webpack...
Whatever the tool chain is, the idea is always the same:
- to make it easy to download Javascript libraries (possibly with CSS and images) and have them integrated in the project
- to maintain a registry of library versions
- to do some processing on the code (syntax checking, transpiling, minifying, etc.) in order to produce a final .js file
- the final .js file is highly optimized, "production-ready" and contains all dependencies inline
In order to do all this, my personal choice is webpack since it does the job of browserify + gulp (or grunt), using "standard" tools (like Node and npm) for package download and versions management.
So in my MXCuBE 3 repository I configured webpack and node modules to work with our code base.
There is more... I think we should write our Javascript code in ES6 syntax ; this is the syntax for the upcoming versions of Javascript, it is a standard that just needs to be implemented in all browsers.
It makes code much more readable, in particular when working with React components if we decide to follow the principle of having "1 .jsx file === 1 component" since it becomes possible to use ES6 modules syntax to import code from other modules just like we do in... Python. Thanks to the webpack installation it is the same for third-party dependencies too :) So one can write "import React" or "import { Button } from Bootstrap" for example.
Babel is a tool that converts ES6 code to ES5, to make it runnable on all web browsers. I added it to the webpack configuration, so it becomes transparent : we can write ES6 code and the final .js file is in ES5.
I followed guides on the net and I also configured webpack-dev-server : it allows to test the UI without starting the "real" MXCuBE 3 server. So the front-end developer can focus on the user interface, not worrying about real hardware or whatever. A very interesting feature of webpack is hot reloading ; it allows code changes to be immediately visible in the web browser during development.
I would like to get your opinion on all this, as soon as possible in order to see if we can merge a
PR from my repository and to continue development on good rails, or if you disagree we can
see how to organize all this differently.
Let's try it !
You can clone my repository be careful to use branch webpack:
git clone git://github.com/mguijarr/mxcube3 -b webpack
How to use webpack ?
-
first install Node and npm, required version is 0.12 at least ; for Debian-based systems I recommend doing this:
# Note the new setup script name for Node.js v0.12
curl -sL https://deb.nodesource.com/setup_0.12 | sudo bash -
# Then install with:
sudo apt-get install -y nodejs
-
then just install MXCuBE 3 dependencies:
cd mxcube3-repo
npm install
-
finally, run webpack in development mode:
Open a web browser to http://localhost:8090 and see what I am working on...
What do you think ?