Giter Club home page Giter Club logo

npm-build-boilerplate's Introduction

npm-build-boilerplate

A collection of packages that build a website using npm scripts.

List of packages used

autoprefixer, browser-sync, eslint, imagemin-cli, node-sass, onchange, npm-run-all, postcss-cli, svgo, svg-sprite-generator, uglify-js.

Many, many thanks go out to Keith Cirkel for his post and his useful CLI tools!

Using in your project

  • First, ensure that node.js & npm are both installed. If not, choose your OS and installation method from this page and follow the instructions.
  • Next, use your command line to enter your project directory.
    • If this a new project (without a package.json file), start by running npm init. This will ask a few questions and use your responses to build a basic package.json file. Next, copy the "devDependencies" object into your package.json.
    • If this is an existing project, copy the contents of "devDependencies" into your package.json.
  • Now, copy any tasks you want from the "scripts" object into your package.json "scripts" object.
  • Finally, run npm install to install all of the dependencies into your project.

You're ready to go! Run any task by typing npm run task (where "task" is the name of the task in the "scripts" object). The most useful task for rapid development is watch. It will start a new server, open up a browser and watch for any SCSS or JS changes in the src directory; once it compiles those changes, the browser will automatically inject the changed file(s)!

List of available tasks

clean

rm -f dist/{css/*,js/*,images/*}

Delete existing dist files

autoprefixer

postcss -u autoprefixer -r dist/css/*

Add vendor prefixes to your CSS automatically

scss

node-sass --output-style compressed -o dist/css src/scss

Compile Scss to CSS

lint

eslint src/js

"Lint" your JavaScript to enforce a uniform style and find errors

uglify

mkdir -p dist/js && uglifyjs src/js/*.js -m -o dist/js/app.js && uglifyjs src/js/*.js -m -c -o dist/js/app.min.js

Uglify (minify) a production ready bundle of JavaScript

imagemin

imagemin src/images/* -o dist/images

Compress all types of images

icons

svgo -f src/images/icons && mkdir -p dist/images && svg-sprite-generate -d src/images/icons -o dist/images/icons.svg

Compress separate SVG files and combine them into one SVG "sprite"

serve

browser-sync start --server --files 'dist/css/*.css, dist/js/*.js, **/*.html, !node_modules/**/*.html'

Start a new server and watch for CSS & JS file changes in the dist folder

build:css

run-s scss autoprefixer

Alias to run the scss and autoprefixer tasks. Compiles Scss to CSS & add vendor prefixes

build:js

run-s lint concat uglify

Alias to run the lint, concat and uglify tasks. Lints JS, combines src JS files & uglifies the output

build:images

run-s imagemin icons

Alias to run the imagemin and icons tasks. Compresses images, generates an SVG sprite from a folder of separate SVGs

build

run-s build:*

Alias to run all of the build commands

watch:css

onchange 'src/**/*.scss' -- run-s build:css

Watches for any .scss file in src to change, then runs the build:css task

watch:js

onchange 'src/**/*.js' -- run-s build:js

Watches for any .js file in src to change, then runs the build:js task

watch:images

onchange 'src/images/**/*' -- run-s build:images

Watches for any images in src to change, then runs the build:images task

watch

run-p serve watch:*

Run the following tasks simultaneously: serve, watch:css, watch:js & watch:images. When a .scss or .js file changes in src or an image changes in src/images, the task will compile the changes to dist, and the server will be notified of the change. Any browser connected to the server will then inject the new file from dist

postinstall

run-s build watch

Runs watch after npm install is finished

Need help?

Feel free to create an issue, tweet me, or send me an email. I'd be glad to help where I can!

npm-build-boilerplate's People

Contributors

hamishrouse avatar jacobalvarez avatar rootman avatar shenqihui avatar staeff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

npm-build-boilerplate's Issues

build all before watch all.

I suggest to build add before watch all.

So i think when run npm install ,it will call postinstall, and i change my postinstall script into

npm run build:all && npm run watch:all

This will make another quickly start use this repo.

Watch does not work for SCSS files in windows 10

Hi @damonbauer ,
I read your article in css-tricks.com and tried to replicate it based on my needs,
I use a windows-10-x64 but the scripts only watch HTML files and ignore SCSS files completely(on the build level and page reload level).
Here is my "Package.json" and i hope you can help me.

{
  "name": "devhints",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "autoprefixer": "postcss -u autoprefixer -r dist/css/*",
    "scss": "node-sass --output-style compressed -o dist/css src/scss",
    "serve": "browser-sync start --server --files 'dist/css/*.css, **/*.html, !node_modules/**/*.html'",
    "build:css": "npm run scss && npm run autoprefixer",
    "watch:css": "onchange 'src/scss/**/*.scss' -- npm run build:css",
    "watch:all": "npm-run-all -p serve watch:css",
    "postinstall": "npm run build:css && npm run watch:all"
  },
  "author": "HomamGhassemy",
  "license": "ISC",
  "devDependencies": {
    "autoprefixer": "^6.3.6",
    "browser-sync": "^2.13.0",
    "node-sass": "^3.7.0",
    "npm-run-all": "^2.1.1",
    "onchange": "^2.5.0",
    "postcss-cli": "^2.5.2"
  }
}

Please tell me if there is anything more needed.
Thanks

HTML Compress

how do you deal with compressing HTML with this boilerplate

Auto image minify

Thanks for that,

Is there a way that while the scripts are running in the background, when we add new images to src/images/ that they will automatically be minified into dist/images/ ?
Or at least get automatically copied from src/images/ to dist/images/ until npm restart ?

Right now when all npm scripts are running, images copied to src/images/ are not minified/copied to dist/images/ so a restart is a must and that's time consuming.

imagemin-cli — update to v.3

We're using
"imagemin-cli": "^2.1.0",
but v.3 is the latest: https://github.com/imagemin/imagemin-cli

It looks like there was a breaking change, -o flag is now required.
Double-check, but I think:
"imagemin": "imagemin src/images dist/images -p",
should be changed to:
"imagemin": "imagemin src/images -o dist/images",

I also removed -p as there's no plugins to override. Once I did that, everything started to work fine again after update.

svgo: Output to dist/

Currently, svgo optimizes the files in-place. However, it would be better not mutating any files in src/ as they may have to be manually changed/inspected in their raw state or optimizer options tweaked (which would be harder or even impossible with a optimized svg for example).

SVG Sprites not working

Whenever I run the task for generating the svg spritesheet it doesn't work. No errors in the terminal but no "sprite.svg" file is written to the dist/images/icons/ folder…

.DS_Store in images

Should probably be globally gitignored on your system. I guess it shouldn't make its way into the repo

onchange (osx)

on the npm build boilerplate I seem to be having issues with onchange, when I test everything out separately that seems to be my issue. onchange appears to run but then doesn't seem to notice when the file has changed?

also. will onchange run async/at the same time as browser sync?

I am also trying others like npm watch no luck yet. ill let you know if I figure it out, after this I just have to setup my browsersync config and this solution will be ready for me! no more gulp!

npm task for zip files

Thanks for all these tips. I am trying to achieve something similar to zip (or gzip or tar) my artifacts. So far I've seen zlib, node-tar and node-zip to accomplish that. However, they are not one line of code which I could use in my package.json file.

Do you know how to do that in a simple manner?

npm run clean not working in windows

When I enter npm run clean I receive the error:

'rm' is not recognized as an internal or external command, operable program or batch file.

When I change rm to del I then receive this error:

Invalid switch - "{css".

Can anyone tell me how I need to rewrite the npm scripts file paths so that windows will not throw an error?

Windows

this doesn't seem to work on windows machines. I get an error on the uglify script when installing and the watch task for scss files also won't work

Caching to speed up build process

I have been using gulp for some time. For the same reasons you mentioned in your css trick article, I decided to switch to npm scripts. One thing I noticed is that the build speed is quite slow after the first build compared to how it was when I used gulp. Is there any way to speed this up? perhaps some caching ...

[Question] How to minify all HTML files in folder (and all folders within)?

Hi Damon,

Thanks for this amazing boilerplate. I'm using it to run my static website (Hugo) build. The problem with rendered HTML pages is that their source ends messed up, with weird indentations and gaps in the HTML code.

Do you know, is it possible to minify all the .html files recursively in a folder in root (public/) using npm run script? I tried minimize from npm and while it's easy to minify one HTML file:

minimize --output public/index.html public/index.html

it's not so easy to feed the folder and make it overwrite all HTML files. The following doesn't work:

minimize --output public/**/*.html public/**/*.html

Do you know any way to minify (and overwrite) all HTML files for given folder using npm scripts?

Thank you.

Here's my question on StackOverflow, nobody replied nearly in a week.

SVG Sprites Stylesheet

I am trying to use SVG Sprite Icons but it is not creating the positions of the icons in a stylesheet. How to use SVG Spritesheet?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.