• Stars
    star
    488
  • Rank 90,182 (Top 2 %)
  • Language
    JavaScript
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple boilerplate for using NPM tasks to build and compile JavaScript, CSS, and image files.

Build Tool Boilerplate

A simple boilerplate for using NPM tasks to build and compile JavaScript, CSS, and image files.

Version 2 adds watch and server tasks, and removes the need for Windows-specific tasks.

Install

Quick Start

Each task has just one or two dependencies (except for image optimization), so I recommend deleting the ones you don't need before running npm install. Learn more in the documentation below.

  1. In bash/terminal/command line, cd into your project directory.
  2. Run npm install.
  3. Run npm run build.

Documentation

This is a boilerplate that you can use as a starting point for your projects.

Running Tasks · JavaScript · Sass => CSS · SVG Optimization · Image Optimization · Copy Files · Clean · Complete Build · Watch for Changes · Server

Running Tasks

The boilerplate uses the npm run command to run tasks. These work on macOS, Linux, and Windows systems.

# Main Tasks
npm run js     # compile and minify
npm run css    # compile and minify Sass into CSS
npm run svg    # optimize SVGs with SVGO
npm run img    # optimize image files
npm run copy   # copy files from the src/copy directory as-is into /dist
npm run clean  # delete the /dist directory
npm run build  # run all tasks
npm run watch  # watch for changes and rebuild
npm run server # run a localhost server that reloads when files change

# Modular Tasks
npm run watch-js     # watch for changes to the /js directory
npm run watch-css    # watch for changes to the /css directory
npm run watch-svg    # watch for changes to the /svg directory
npm run watch-img    # watch for changes to the /img directory
npm run watch-copy   # watch for changes to the /copy directory
npm run build-dirty  # run a new build without deleting the /dist directory
npm run server-start # start a server without watching for changes

JavaScript

The boilerplate uses rollup.js with the terser plugin to parse, compile, and minify JavaScript files.

{
    "devDependencies": {
        "rollup": "^2.6.1",
        "rollup-plugin-terser": "^7.0.2"
    }
}

In the rollup.config.js file, there's a configs object that you can use to control what rollup.js does.

// Configs
var configs = {
    name: 'MyProject',                // Global namespace to use for IIFEs [optional]
    files: ['main.js', 'detects.js'], // The files to process
    formats: ['iife', 'es'],          // The formats to output - will be added as a suffix to the filename (ex. main.es.js)
    default: 'iife',                  // Files with this format will not have a format suffix [optional]
    pathIn: 'src/js',                 // The source directory for your JS files
    pathOut: 'dist/js',               // The directory to compile JS files into
    minify: true,                     // If true, a minified version will also be created with the .min suffix
    sourceMap: false                  // If true, sourcemaps are created for each processed file †
};

A banner is automatically generated from your package.json data.

It includes the project name and version, a copyright notice with the current year and the package author name, the license type, and a link to the project repository.

If a configs.name property is included, that will be used. If not, the banner defaults to the name property in your package.json file.

// Banner
var banner = `/*! ${configs.name ? configs.name : pkg.name} v${pkg.version} | (c) ${new Date().getFullYear()} ${pkg.author.name} | ${pkg.license} License | ${pkg.repository.url} */`;

To concatentate multiple files into one, use the ES modules import feature.

// myplugin.js
// This will compile into /dist/js/myplugin.js, and will include helpers.js, app.js, and event-listeners.js

import * as Helpers from './helpers.js';
import app from './app.js';
import './event-listeners.js';

JavaScript files should be in the src/js directory. Use this task to run the build.

npm run js

Note for FireFox users: ensure that 'Use Source Maps', and 'Show original sources' options are enabled in Developer Tools.

Sass => CSS

The boilerplate uses the Node implementation of dart-sass to parse .scss files into CSS.

{
    "devDependencies": {
        "sass": "^1.26.5"
    }
}

In the sass.js file, there's a configs object that you can use to control what dart-sass does.

// Configs
var configs = {
    name: 'MyProject',    // The name to use in the file banner
    files: ['main.scss'], // The files to process
    pathIn: 'src/scss',   // The source directory for your Sass files
    pathOut: 'dist/css',  // The directory to compile CSS files into
    indentType: 'tab',    // The type of indenting to use ['tab'|'spaces']
    indentWidth: 1,       // How many tabs or spaces to indent
    minify: true,         // If true, a minified version will also be created with the .min suffix
    sourceMap: false,     // If true, sourcemaps are created for each processed file †
};

A banner is automatically generated from your package.json data.

It includes the project name and version, a copyright notice with the current year and the package author name, the license type, and a link to the project repository.

If a configs.name property is included, that will be used. If not, the banner defaults to the name property in your package.json file.

// Banner
var banner = `/*! ${configs.name ? configs.name : pkg.name} v${pkg.version} | (c) ${new Date().getFullYear()} ${pkg.author.name} | ${pkg.license} License | ${pkg.repository.url} */`;

Sass files should be in the src/scss directory. Use this task to run the build.

npm run css

Note for FireFox users: ensure that 'Use Source Maps', and 'Show original sources' options are enabled in Developer Tools.

SVG Optimization

The boilerplate uses svgo to remove the cruft that gets added to SVG files by many editors.

{
    "devDependencies": {
        "svgo": "^1.3.2"
    }
}

For accessibility reasons, the boilerplate disables the settings that remove the title element and viewBox attribute.

You can make additional command line configurations under the svg tasks in the scripts property of the package.json file.

svgo -f src/svg dist/svg -r --disable=removeViewBox,removeTitle

SVGs should be in the src/svg directory. Use this task to run the build.

npm run svg

Image Optimization

The boilerplate uses imagemin, with the MozJPEG, pngcrush, pngquant, and zopfli plugins.

(Yea, that's kind of lot, isn't it?)

{
    "devDependencies": {
        "imagemin-cli": "^6.0.0",
        "imagemin-mozjpeg": "^8.0.0",
        "imagemin-pngcrush": "^6.0.0",
        "imagemin-pngquant": "^8.0.0",
        "imagemin-zopfli": "^6.0.0"
    }
}

Image files should be in the src/img directory. Use this task to run the build.

npm run img

Copy Files

The boilerplate uses recursive-fs to provide a cross-OS copying solution. This package is also used for the clean task, so only remove it if you're deleting both tasks.

{
    "devDependencies": {
        "recursive-fs": "^2.1.0"
    }
}

If you have files you want copied as-is, place them in the src/copy directory.

Use this task to run the build.

npm run copy

Clean

The boilerplate uses recursive-fs to provide a cross-OS recursive directory deleting solution. This package is also used for the copy task, so only remove it if you're deleting both tasks.

{
    "devDependencies": {
        "recursive-fs": "^2.1.0"
    }
}

You can delete the /dist directory before running a build to clean up any junk that might have ended up there. The build task runs this task before doing anything else.

npm run clean

Complete Build

You can run all of your build tasks in a single command.

Use this task to run the build.

npm run build

If you want to run your build without first deleting the /dist directory, run this task instead.

npm run build-dirty

Regardless of which task you use, be sure to delete any tasks you're not using from the build-dirty task under scripts in your package.json file first. The npm-run-all -p command is used to run all tasks in parallel (see below for more details).

# default build-dirty task
npm-run-all -p js css svg img copy

Watch for Changes

The boilerplate uses Chokidar CLI to watch for changes to the /src directory and run tasks in response.

{
    "devDependencies": {
        "chokidar-cli": "^2.1.0"
    }
}

Use this task to watch for changes and run a build. It will also run a fresh build when it starts.

npm run watch

If you only want to watch for changes to a specific directory in /src, you can use a task-specific watcher task.

npm run watch-js   # watch for changes to the /js directory
npm run watch-css  # watch for changes to the /css directory
npm run watch-svg  # watch for changes to the /svg directory
npm run watch-img  # watch for changes to the /img directory
npm run watch-copy # watch for changes to the /copy directory

Server

The boilerplate uses Browsersync to run a local server and automatically update it whenever your files change.

{
    "devDependencies": {
        "browser-sync": "^2.26.14"
    }
}

Use this task to watch for changes. It will also run the watch task, and automatically rebuild whenever a file in /src changes.

npm run server

If you want to run the server without the watch task, run this task instead.

npm run server-start

Core Dependencies

The boilerplate uses npm-run-all to run tasks consistently across different operating systems, and in parallel.

{
    "devDependencies": {
        "npm-run-all": "^4.1.5"
    }
}

The npm-run-all package removes the need for Windows-specific tasks.

It also allows you to run tasks in parallel. By running all of the tasks in the build tasks at the same time, you dramatically reduce the build time. This is also what makes it possible to run a localhost server and watch for file changes in one task.

In other words, don't remove this dependency.

Why does this exist?

For years, I've been an avid Gulp user. Gulp is great. But it's also a lot.

I wanted a simpler, more resilient, leaner set of build tools.

I'm tired of having to repair my build anytime I don't use it for a few months. I'm tired of installing 270mb of node_modules dependencies to build a simple website or web app.

With NPM, you can build a simplish build tool that does just what you want (and nothing more) with a fraction of the footprint.

❤️ Major kudos to Keith Cirkel for teaching me about this years ago, before I was ready to hear it.

More Repositories

1

smooth-scroll

A lightweight script to animate scrolling to anchor links.
JavaScript
5,481
star
2

reef

A lightweight library for creating reactive, state-based components and UI.
JavaScript
1,113
star
3

gulp-boilerplate

A boilerplate for building web projects with Gulp.js.
JavaScript
846
star
4

kraken

A lightweight, mobile-first boilerplate for front-end web developers.
HTML
768
star
5

gumshoe

A simple vanilla JS scrollspy script.
JavaScript
731
star
6

social-sharing

Add social sharing links and buttons without the bloat.
CSS
641
star
7

tabby

Lightweight, accessible vanilla JS toggle tabs.
JavaScript
577
star
8

atomic

A tiny, Promise-based vanilla JS Ajax/HTTP plugin with great browser support.
HTML
540
star
9

bouncer

A lightweight form validation script that augments native HTML5 form validation elements and attributes.
JavaScript
308
star
10

validate

A lightweight form validation script.
JavaScript
230
star
11

houdini

A simple, accessible show-and-hide/accordion script.
JavaScript
158
star
12

ebook-boilerplate

A lightweight boilerplate for self-publishing ebooks with markdown and command line.
CSS
135
star
13

htaccess

[DEPRECATED] Best practice server rules for making web pages fast and secure.
ApacheConf
93
star
14

slider

[DEPRECATED] A simple, fluid, touch-enabled carousel.
JavaScript
87
star
15

right-height

Dynamically set content areas of different lengths to the same height.
CSS
87
star
16

modals

Simple modal dialogue windows
JavaScript
85
star
17

bin

A tiny (<1kb) localStorage and sessionStorage helper library.
JavaScript
84
star
18

vanilla-javascript-cheat-sheet

Moved
80
star
19

vanilla-js-toolkit

A growing collection of vanilla JavaScript code snippets, helper functions, polyfills, plugins, and learning resources.
CSS
79
star
20

drop

A small CSS component that turns browser-native <details> elements into dropdown menus.
JavaScript
75
star
21

form-saver

A simple script that lets users save and reuse form data.
JavaScript
68
star
22

hugo-starter

A barebones starter project and theme for learning Hugo.
HTML
67
star
23

gmt-wordpress-for-web-apps

[DEPRECATED] A plugin that provides the essential components you need to power your web app with WordPress.
PHP
67
star
24

events

A tiny event delegation library.
JavaScript
62
star
25

gmt-html-minify

Minify your HTML output in WordPress.
PHP
61
star
26

buoy

[DEPRECATED] A lightweight collection of helper methods for getting stuff done with native JavaScript.
JavaScript
54
star
27

astro

Mobile-first navigation patterns.
JavaScript
53
star
28

sticky-footer

Dynamic, responsive sticky footers.
JavaScript
47
star
29

keel

A lightweight boilerplate for WordPress theme developers.
JavaScript
46
star
30

x-ray

X-Ray is a script that lets users toggle password visibility in forms.
JavaScript
46
star
31

table-of-contents

Automatically generate a table of contents from the headings on the page.
JavaScript
39
star
32

jar

A tiny (< 1kb) library that makes working with cookies easier.
JavaScript
38
star
33

saferInnerHTML

Set the HTML of an element while protecting yourself from XSS attacks.
JavaScript
29
star
34

dom-manipulation-source-code

HTML
24
star
35

gmt-image-compress-and-sharpen

[DEPRECATED] Change the default WordPress compression rate for JPGs, convert images to progressive JPGs, and sharpen images.
PHP
19
star
36

learn-vanilla-js

A roadmap for learning vanilla JavaScript
HTML
19
star
37

jellyfish

[DEPRECATED] A progressively enhanced image lazy loader.
JavaScript
18
star
38

frontend-horse-js-library

The boilerplate for the Frontend Horse livestream.
JavaScript
17
star
39

state-based-ui-source-code

JavaScript
15
star
40

petfinderAPI4everybody

A JavaScript plugin that makes it easier to use the Petfinder API.
JavaScript
14
star
41

vanilla-js-guides

HTML
14
star
42

wp-shopping-cart

[DEPRECATED - DO NOT USE] A simple PayPal shopping cart for WordPress.
PHP
14
star
43

alerts

Simple alert messages.
JavaScript
14
star
44

wp-theme-options

[DEPRECATED] Create a theme options menu that admin can use to adjust theme settings from the dashboard.
PHP
14
star
45

reef-js

The website for ReefJS.
HTML
14
star
46

progress-bars

Simple CSS progress bars.
CSS
13
star
47

writing-libraries-source-code

HTML
13
star
48

learn-vanilla-js-source-code

HTML
12
star
49

kelp

A collection of small functions for creating reactive, state-based UIs.
JavaScript
12
star
50

project-star-rating-system

A vanilla JavaScript project
HTML
11
star
51

gmt-mailchimp-wp-rest-api

Add WP Rest API hooks for JS use of the Mailchimp API.
PHP
11
star
52

apis-source-code

HTML
11
star
53

vanilla-js-list

A growing list of organizations that build sites and apps with vanilla JS
CSS
10
star
54

petfinder-api-for-wordpress

A collection of functions to help you display Petfinder listings on your WordPress site.
PHP
10
star
55

browser-storage-source-code

HTML
10
star
56

gmt-pricing-parity

Display country-specific EDD discounts to visitors.
PHP
9
star
57

vanilla-js-academy

PHP
9
star
58

gmt-remove-header-junk

[DEPRECATED] Remove the unneccessary junk WordPress adds to the header.
PHP
9
star
59

snapshot

Simple image styling.
CSS
9
star
60

dom-injection-source-code

HTML
8
star
61

the-lean-web

CSS
8
star
62

sw-fonts

Demo for https://gomakethings.com/improving-web-font-performance-with-service-workers/
HTML
8
star
63

gmt-paypal-ipn-forwarder

[DEPRECATED] Forward PayPal IPN to multiple other IPN services in WordPress.
PHP
8
star
64

service-worker-pages-demo

A demo for https://gomakethings.com/saving-recently-viewed-pages-with-service-workers-and-vanilla-js/
HTML
8
star
65

gmt-slack-invites-for-edd

[DEPRECATED] Automatically invite members to your Slack team when they purchase a product.
PHP
8
star
66

vanilla-js-guidebook-labs

The labs for "The Vanilla JS Guidebook."
HTML
7
star
67

service-workers-source-code

JavaScript
7
star
68

sw-offline-first

Demo for https://gomakethings.com/offline-first-with-service-workers-and-vanilla-js/
HTML
7
star
69

harbor-wp-theme

A free mobile-friendly WordPress theme built specifically for animal rescue organizations.
JavaScript
7
star
70

adventure

A simplish, imagination-based kitchen table RPG.
CSS
7
star
71

vanilla-js-projects

HTML
6
star
72

games

Simple vanilla JS game demos.
HTML
6
star
73

variable-function-scope-source-code

HTML
6
star
74

gmt-automated-slack-invites

Automate Slack invites with WordPress.
PHP
6
star
75

google-hosted-jquery

[DEPRECATED] Use the Google CDN version of jQuery in WordPress, with a local fallback.
PHP
6
star
76

gomakethings

The WordPress theme for Go Make Things
JavaScript
5
star
77

service-worker-demo

A demo for https://gomakethings.com/writing-your-first-service-worker-with-vanilla-js/
HTML
5
star
78

origami

[DEPRECATED] Origami has been merged into the Kraken front-end boilerplate and is no longer supported.
HTML
5
star
79

timezones

Easily calculate timezones for your next meeting.
HTML
4
star
80

arrays-objects-source-code

HTML
4
star
81

labels

Lightweight CSS labels.
CSS
4
star
82

gmt-antispambot

A shortcode for the antispambot function that's built into WordPress.
PHP
4
star
83

cferdinandi

4
star
84

web-components-source-code

HTML
4
star
85

string-array-object-source-code

HTML
4
star
86

vanilla-js-podcast

A show about JavaScript for people who hate the complexity of modern front‑end web development.
CSS
4
star
87

es-modules-demo

https://gomakethings.com/an-intro-to-import-and-export-with-es-modules/
JavaScript
3
star
88

javascript-essentials-source-code

Source code for the JavaScript Essentials workshop.
HTML
3
star
89

accessible-components-source-code

HTML
3
star
90

es-modules-source-code

JavaScript
3
star
91

ajax-source-code

HTML
3
star
92

strings-numbers-source-code

HTML
3
star
93

go-mobile-first

[DEPRECATED] A mobile-first boilerplate for WordPress.
PHP
3
star
94

gmt-allow-iframes

[DEPRECATED] Prevent wp_kses from removing iframe embeds
PHP
3
star
95

gmt-donations

[DEPRECATED] A WordPress plugin that lets you create powerful donation forms that integrate with Stripe and PayPal Express Checkout.
PHP
3
star
96

token-based-authentication-source-code

HTML
3
star
97

advanced-academy-common-issue-not-modularizing-your-code-enough

JavaScript
3
star
98

fetch

The Fetch for Petfinder plugin
JavaScript
2
star
99

es-modules-default

https://gomakethings.com/how-to-define-a-default-export-with-vanilla-js-es-modules/
JavaScript
2
star
100

careers

CSS
2
star