Systematic
An opinionated, mostly framework-agnostic toolchain to package ES6 applications and libraries for the browser.
Features :
- Stay lazy: the toolchain already made the good choices for you.
- Full ES6 & PostCSS support through carefully selected & configured Webpack plugins.
- Paranoid code linting & styling with standard
- Framework-agnostic, standard (GNU gettext) translation file handling.
- Application settings management, human-editable INI files get converted into JS.
- Library creation, with dependencies exclusion from build
- Pluggable: it's easy to add commands or override features
Installation
npm install systematic
The file structure expected for your application or library.
├── dist
│ ├── app.settings.js
│ ├── bundle.js
│ ├── bundle.js.map
│ ├── translations.json
│ ├── an_asset.png
│ └── index.html
|
├── src
│ ├── some_module/
| | ├── enums.js
| | ├── index.js
| | ├── index.spec.js
| | └── models.js
| |
│ ├── utils.js
│ ├── index.html
│ ├── index.spec.js
│ └── index.js
|
├── webpack.config.js
├── Makefile
└── systematic.ini
Config file
-
Systematic requires a systematic.ini
configuration file in the root folder of your project.
[build]
type = library
profile = vanilla
output_dir = dist
src_dir = src
public_path = /
locales = en_US en_GB
no_parse[] = vuetify
keep_dependency[] = lodash
analyze = 1
[serve]
host = 127.0.0.1
port = 8080
[test]
file_pattern = src/**/*tests.js
Makefile
Systematic uses GNU make
. Create a Makefile at the root of your project, to import systematic commands:
include node_modules/systematic/mk/main.mk
Entry points
-
The default source folder (containing your source code and tests) is src
. It must contain an entrypoint file named index.js
. Example:
import somelib from 'some-lib'
import m1 from './module1'
import m2 from './module1'
somelib.bootstrap(m1, m2)
-
If your project is an application, there must be an HTML entry point named index.html
in the source folder, containing the primary page. Your JS entry point will be automatically added.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Your website</title>
</head>
<body>
</body>
</html>
Usage
make help
gives a list of all commands.
Build
make serve
to run a local server.
make dist
for a prod build.
Run tests
make test
runs all test that match the test file pattern (default **/*tests.js
).
make livetest
run test continuously, when a file changes.
Translations
make makemessages
extract translations from your HTML and JS files using easygettext.
The resulting .po
files will be in /locale
.
make translations
generates a JSON file from them, located at dist/translations.json
. It is automatically run with serve
, dist
and test
.
You can then load them in your JS as an object:
import translations from 'dist/translations.json'`
Settings
make settings
generates a file dist/app.settings.js
from all INI files in src/settings/
if your project is an application.
Settings files are processed in alphabetical order, the last one overriding the previous. It is automatically run with serve
, dist
and test
.
The generated file dist/app.settings.js
needs to be included in your index.html, since it will not be added by Webpack.
After the app is deployed, you might want to change the settings. This part is not handled by systematic, but we recommand to regenerate the settings from INI files.
INI files are an ideal format, as it is not error prone, ensures only settings values are changed and no javascript is added.
Build profiles
From systematic.ini's profile
option. For now, only the angular
profile is supported on top of the
natural vanilla
profile.
Angular
Value : angular
Adds the ng-annotate Babel plugin.
VueJS
Value : vue
Adds translation management with the vue translation token.
Override Jest or Webpack config
It's possible to override the build or test config by adding config files at the root of the projet.
-
For Webpack: webpack.config.js
. Example :
const webpackDefaults = require('systematic').webpack_get_defaults(__dirname)
webpackDefaults.module.loaders.push({ test: /\.file_extension_example$/, loader: 'my-loader' })
module.exports = webpackDefaults
-
For Jest: jest.conf.js
. Example:
const jestDefaults = require('systematic').jest_get_defaults(__dirname)
jestDefaults.plugins.push('my-plugin')
module.exports = jest => jest.set(jestDefaults)
Building components
A component is halfway between a library and an application. You get to have an index.html page that is useful for development, but the dependencies
aren't bundled with the final package, just like a library.
You also have the choice of having the component used directly within a page, without being required from within another JS application that
goes through webpack.
In that regard, you have to pay attention to NOT import your dependencies using Webpack-based import
feature and use the /* global _, L */
notation in your sources (use import
in your tests, why not) so that your application will try and find those symbols on window
;
also, use the keep_dependency
feature in your configuration file for any dependency yo uwant bundled with.
Building libraries
When building a library, we don't want the dependencies included in the bundle. It can cause version conflict with other packages or duplicated library imports.
Systematic will set all dependencies as webpack "externals", which means they have to be required by the app.
Polyfilling
Systematic uses Babel to transpile ES6, ES6+ code to plain ES5.
Polyfilling is still needed:
- For methods that can't be transpiled: when Babel can't guess the correct transpilation for instance.
console.log('blah'.repeat(2))
- To ensure expected ES6 features are present, whatever browser is used (Array.contains, for instance)
Applications
You have two solutions:
- Use
babel-polyfill
just like in tests. You have to include it in the app entry point, before any other import:
import 'babel-polyfill'
This has the advantage of simplicity and consistency across browsers but it pollutes the global scope quite broadly.
- Import what you need from
core-js
(on which babel-polyfill
is built) on a per case basis:
import _repeat from 'core-js/library/fn/string/repeat';
const myStr = _repeat('blah', 2);
This method is preferred as you can precisely choose what's being added to the environment. But it's more tedious.
Libraries
Avoid a global polyfill as it modifies global entities such as "Function" or "String".
Use precise imports from core-js
as described above if needed.
Troubleshooting
Webpack's livereload is not working properly
You should probably allow more watchers on your machine, see how on webpack's
doc.
License
MIT