vanilla-es6-jspm
ES6 is here and you can't avoid it. We have great tools to make it work, one of them is jspm.
jspm is great but all-in-one yeoman generators or seed projects (with build/sass/livereload/sourcemaps/unit-tests ...) are still lacking, so I decided to make my own angular/ES6/jspm stack.
This project is the first step: a vanillaJS/ES6/jspm boilerplate (more infos on the Wiki).
- Requirements / Installation / Launch
- Build / Test
- Git workflow & Continuous Integration
- Generate Docs
- Deployment / Folder organization
Features
- Simple ES6 app using basic ES6 features
- so that you won't have to struggle to understand the code base
- but advanced enough to be used as an example
- Use ES6 Modules via SystemJS module loader (built on top of ES6 Module Loader Polyfill)
- Manage development and production workflow with jspm, SystemJS Builder and Gulp.
- Backend mocking / stubs / overriding of the module loader
- Karma unit tests (using mocks and stubs)
- e2e testing with protractor
- Produce optimized, production ready code for deployment
- Continuous Integration ready (tests via Travis CI with SauceLabs integration for e2e tests)
- Automated documentation generation
TL;DR
Want to get started right now and bother about all the great features later ?
$ npm install -g jspm
$ git clone https://github.com/topheman/vanilla-es6-jspm.git
$ cd vanilla-es6-jspm
$ npm install
$ gulp serve
You're ready to develop in ES6 the project in the src
folder!
Requirements
- node/npm (tested on both node v4, v5 & v6 - see tests)
- gulp
npm install -g gulp-cli
- jspm
npm install -g jspm
Why all dependencies in local (see wiki) ?
Installation
All you need is to run npm install
npm install
: install all npm dependencies + will automatically triggers the following at postinstall (so you don't need to run those):jspm install
: installs front dependencies via jspmnpm run webdriver-manager-update
installs necessary binaries for Selenium (for e2e testing)
Launch
You can launch your app in different modes (dev/dist/test):
gulp serve
: will launch a dev servergulp serve --env dist
: will launch the version of the site built inbuild/dist
folder (need togulp build
before) - usefull to check your site before deploying it.gulp serve --env test
: will launch a dev server with test configuration - usefull to debug / create unit tests:- jspm configuration overridden by the
test/jspm.override.json
file - the app will appear with "TEST" in background, thanks to
test/fixtures/bs.snippet.html
injected on the the fly (containing specific css)
- jspm configuration overridden by the
You can pass the following flags to the gulp serve
command:
--env
: acceptsdev
,dist
,test
(dev
by default)--port
: overrides the port of the server you'll launch--disable-watch
: disables watching/reloading--open false
: won't open the site in the browser
Build
gulp build
: builds a production ready version of the site inbuild/dist
foldergulp build --env test
: same asgulp build
, but bundles a test version of your website (usingtest/jspm.override.json
)- can be usefull if you want to do end-to-end testing on a built version of your website
You can also pass the following flags to gulp build
:
--with-docs
: will generate the documentation from source code inbuild/dist/docs
(see generate docs section)
Test
Unit
The unit tests are in test/unit/spec
.
You can see exactly which commands match the following npm tasks in the package.json.
npm test
: runs all the tests (will be triggered ongit-pre-commit
)npm run test-unit
: runs the unit tests throughkarma
npm run test-build
: tests thegulp build
task (will be triggered ongit-pre-push
)- if
build/dist
folder is under git management (see deployment section), this task will git stash/unstash before and after testing.
- if
e2e
The e2e tests are in test/e2e/spec
. It's using protractor - an end-to-end test framework for AngularJS applications, based on Selenium Webdriver. It can also be used on non-angular websites (more on wiki).
You can run the e2e tests two ways (either way, they need a server in order to run):
- standalone (no need to have a server launched / make sure you don't) - launch :
npm run test-e2e-standalone
: this will:- start a test server
- run the e2e tests
- stop the test server
- if you want to run them against your current server (this should be a server launched in test mode with
gulp serve --env test
), open a new terminal tab and runnpm run test-e2e
Git workflow & Continuous Integration
Git hooks
To prevent you from sending broken code, some client-side git hooks are enabled:
- pre-commit: will run
npm test
(checks jshint, htmlhint and karma unit tests) - pre-push: will run
npm run test-build
(checks if the build runs smoothly)- I decided to put this check on pre-push because: it takes time and git stash/unstash
build/dist
folder repo - it could be put on pre-commit ...
- I decided to put this check on pre-push because: it takes time and git stash/unstash
You can bypass those checks by adding to your git command the flag --no-verify
(but know that the Travis CI build will fail).
Travis CI
On each push, Travis CI will run the following tests/builds (see complete steps in .travis.yml):
gulp build
: runs the build routine (to make sure it works fine)gulp build --env test
: builds a test version of the app (to be served for e2e tests)npm test
: runs unit testsnpm run test-e2e
: runs end to end tests via SauceLabs
If either one of them fails, the build will be flagged as failed.
- Travis CI setup
- The
WITH_DOCS=true
env var is exported at the beginning so that the doc generation should also be tested on thegulp build
task (without changing the commands) - As you'll see e2e tests don't run on pull requests. See explanation on the FAQ.
SauceLabs
You can skip this if e2e testing isn't part of your Continuous Integration workflow.
SauceLabs is a cross-browser automation tool built on top of Selenium WebDriver (Protractor uses Selenium WebDriver). It lets you run e2e tests accross multiple devices and is well integrated to Travis CI.
If you want to set it up for your own project, read this post.
To avoid traffic on the Sauce Connect tunnel that could lead to timeouts, SauceLabs tests are run against a server serving the build/dist
folder, containing a bundled version of the site in test
mode (to benefit from the stubs and mocks) which was created thanks to gulp build --env test
.
More infos on this commit: #7898239
See the SauceLabs Report (much like Travis but for e2e tests)
Generate Docs
Documentation generation is currently based on YUIDoc. A set of tasks is at your disposal:
npm run yuidoc
: Will generate documentation inbuild/docs
gulp build --with-docs
: Same as above, but will also copy the generated documentation tobuild/dist/docs
(can be usefull if you have an opensource project and want to share docs)
The configuration of YUIDoc is specified in the package.json
, in the yuidoc
entry - you can override this config (the theme for example).
Here is an example of the output.
Notes:
- I'm still looking for a replacement for yuidoc, feel free to use your own doc generator - if you have one more suited for ES6, please let me know.
- I opened an issue here, for the moment, the doc doesn't display well when served over https.
Deployment
On github pages
You can host your project on github pages like this (source), pushing to the github pages the dist
folder where the project is built.
$ gulp build
$ cd build/dist
$ git init
$ git remote add origin https://github.com/username/vanilla-es6-jspm
$ git fetch origin
$ git checkout --orphan gh-pages
$ gulp build
$ git add .
$ git commit -m "first push to production"
$ git push -u origin gh-pages
Next time, you'll only have to do the last 4 steps ...
Folder Organization
Development
src
βββ 404.html
βββ analytics.snippet.html --> analytics snippet (only added at build)
βββ app --> folder containing app js files
βΒ Β βββ bootstrap.js --> js module entry point
βΒ Β βββ components
βΒ Β βββ main.js
βΒ Β βββ services
βββ favicon-128x128.png
βββ favicon-32x32.png
βββ favicon.ico
βββ images --> images files
βββ index.html --> app main file
βββ styles --> styles folder (.scss)
Production
build
βββ dist --> distribution source code that goes to production
βΒ Β βββ 404.html
βΒ Β βββ docs --> generate doc with "gulp build --with-docs"
β βββ favicon-128x128.png
β βββ favicon-32x32.png
β βββ favicon.ico
β βββ images --> images files
β βββ index.html --> app main file
β βββ scripts --> js files
β βΒ Β βββ app.bootstrap.build-597baeaa0a.js --> concat, minify, reved app js files and cached templates
β βββ styles --> css files
β βββ main.min-b8451af87d.css --> concat & minify app css files
βββ docs --> generate doc with "npm run yuidoc"
Test
test
βββ e2e
βΒ Β βββ spec --> e2e tests run by protractor with jasmine
βΒ Β βββ utils.js
βββ fixtures
βΒ Β βββ bs.snippet.html --> html snippet added on gulp serve:test
βββ forever.gulp.serve.dist.json
βββ forever.gulp.serve.test.json
βββ jspm.override.json --> file to override jspm.config.js in test mode
βββ stubs --> replacements for existing modules, injected in test mode (configured in jspm.override.json)
βΒ Β βββ app
βΒ Β βββ services
βββ unit
βββ spec --> unit tests run by karma with jasmine
βββ components
βββ services
Changelog
See releases section.
Contributing
This section is in progress, you can still take a look at the public trello board.