A simple workflow for static websites with live-reload local server, Babel transpiler for JavaScript and bundling CSS with PostCSS and postcss-preset-env.
π https://staticbuild.website/
- Processing styles using PostCSS with postcss-preset-env
- Babel transpiler for JavaScript (ES6)
- JavaScript concatenating and minification
- CSS minification
- HTML minification
- Image compression
- Assets copying
- Templating & partial HTML injection
- Cache-busting
- Local development server
- Live-reload for the development environment
- Distribution files:
build/
.
This project requires node version 7.5+. This is the only global dependency.
- NodeJS https://nodejs.org/
- Clone Repository: https://github.com/DEEP-IMPACT-AG/staticweb-build
- Install node packages:
$ npm install
To start the development server just run the dev
task:
$ npm run dev
This will start the development server. The server is based on browserSync, supports live reloading which enables hot swapping of CSS styles without reloading the page.
To avoid repetitive HTML code the build uses gulp-file-include, it allow us to re-use chunks of code written in separate files. It is recommended to place the included files in the /src/includes
directory to keep track of changes and live-reload.
Simple Include
@@include(β./includes/header.html')
Include with Parameters
@@include('./includes/helmet.html', {
"title": "Static Web Build"
})
/includes/helmet.html
<title>@@title</title>
If Statement
@@include(β./includes/header.htmlβ{
"menu": true
});
/includes/header.html
<article>
<h1>@@title</h1>
@@text
</article>
Loop
<body>
@@loop(β./includes/loop-article.htmlβ, [
{ "title": "My post title", "text": "<p>lorem ipsum...</p>" },
{ "title": "Another post", "text": "<p>lorem ipsum...</p>" },
{ "title": "One more post", "text": "<p>lorem ipsum...</p>" }
])
</body>
/includes/loop-article.html
<article>
<h1>@@title</h1>
@@text
</article>
βββ build/ # Build files
βββ dist/ # Distribution files
βββ src/ # Source files
β βββ assets/ # Assets directory
β βββ css/ # CSS files
β βββ fonts/ # Fonts directory
β βββ img/ # Image directory
β βββ js/ # JavaScript files
β βββ etc/ # Additional build files
β βββ includes/ # Included partials
βββ tools/ # Tools and utilities
β βββ stylelintrc.json # Stylelint configuration file
β βββ IntelliJ.xml # IntelliJ code style
βββ .babelrc # Babel configuration
βββ .gitignore # Git ignored files
βββ gulpfile.js # Gulp configuration
βββ LICENSE # License agreements
βββ package.json # Node packages
βββ README.md # You are reading this
Note: Your project files: src/
To build the production files run the prod
task:
$ npm run prod
The files will be generated in the app/
directory. The production build automatically minifies the html and css. By default also the javascript files are concatenated in one bundle: assets/js/bundle.js
.
Note: The Gulpfile.js
requires a build restart for any changes to take effect.
Currently, PostCSS has more than 200 plugins. YouΒ can find all of the plugins in the plugins list or in the searchable catalog. ostcss-preset-env is installed in the default configuration.
/* -------------------------------------------------------------------------------------------------
PostCSS Plugins
------------------------------------------------------------------------------------------------- */
const pluginsDev = [
postcssImport,
postcssPresetEnv({
stage: 0,
features: {
'nesting-rules': true,
'color-mod-function': true,
'custom-media': true,
},
}),
];
const pluginsProd = [
postcssImport,
postcssPresetEnv({
stage: 0,
features: {
'nesting-rules': true,
'color-mod-function': true,
'custom-media': true,
},
}),
require('cssnano')({
preset: [
'default',
{
discardComments: true,
},
],
}),
];
//--------------------------------------------------------------------------------------------------
JavaScript files located in the project source directory src/assets/js/
and are automatically concatenated and included in the build process. However you can add additional / external JavaScript libraries by including the files in the Gulp configuration.
/* -------------------------------------------------------------------------------------------------
Your JavaScript Files
------------------------------------------------------------------------------------------------- */
const headerJS = [
'./src/etc/analytics.js',
'./node_modules/aos/dist/aos.js'
];
const footerJS = [
'./node_modules/jquery/dist/jquery.js',
'./src/assets/js/**'
];
//--------------------------------------------------------------------------------------------------
The headerJS
is included before the DOM is loaded and it does not use Babel for transpiling JavaScript. The footerJS
is included after the DOM is loaded, and it goes thourgh Babel.
- NodeJS
- Gulp
- browserSync
- Babel
- PostCSS
- postcss-preset-env
The static web build repository comes with its own set of code style rules that can be imported into IntelliJ. The codestyle file can be found here: tools/IntelliJ.xml
It is advised to run the command $ npm run lint:css
before pushing changes, to make sure the codestyle is consistent!
MIT
v0.0.7
- Improve cache busting - switched to gulp-rev-all
v0.0.5
- Added support for static server using Express.
- Switched from postcss-cssnext to postcss-preset-env.
v0.0.4
- Fix IE animation keyframe minification.
v0.0.3
- Update dependencies.
- Code legibility.
v0.0.2
- HTML Templating with
gulp-include
. - Bugfixes & speed improvements.
v0.0.1
- Initializing Static Web Build.