NPM Scripts Part 2

Objectives and Outcomes

In this exercise you will learn to build a distribution folder containing the files that can be deployed on a web server hosting your project. This distribution folder would be built from your project files using various NPM packages and scripts. At the end of this exercise, you will be able to:

  • Clean out a folder using the clean NPM module.
  • Copy files from one folder to another
  • Prepare a minified and concatenated css file from all the css files used in your project
  • Prepare an uglified and concatenated JS file containing all the JS code used in your project

Cleaning up a Distribution Folder

  • Install the rimraf npm module by typing the following at the prompt:

 
npm install --save-dev rimraf@2.6.2
 
 
 
  • Then, set up the following script:
 
"clean": "rimraf dist",
 
 
 

Copying Fonts

  • Your project uses font-awesome fonts. These need to be copied to the distribution folder. To help us do this, install the copyfiles NPM module globally as follows:
 
npm -g install copyfiles@2.0.0
 
 
 

Remember to use sudo on mac and Linux.

  • Then set up the following script:
 
"copyfonts": "copyfiles -f node_modules/font-awesome/fonts/* dist/fonts",
 
 
 

Compressing and Minifying Images

  • We use the imagemin-cli NPM module to help us to compress our images to reduce the size of the images being used in our project. Install the imagemin-cli module as follows:
 
 
npm -g install imagemin-cli@3.0.0
 
 
 

Remember to use sudo on mac and Linux. NOTE: Some students have encountered issues with imagemin-cli not installing its plugins due to issues with global permissions on Mac. In that case try

 
sudo npm install -g imagemin-cli@3.0.0 --unsafe-perm=true --allow-root
 
 
 
  • Then set up the following script:

 
"imagemin": "imagemin img/* -o dist/img",
 
 
 

Preparing the Distribution Folder

  • Open .gitignore and update it as follows. We do not want the dist folder to be checked into the git repository.

 
 
node_modules
dist
 
 
 
  • Then, install the usemin-cli, cssmin, uglifyjs and htmlmin NPM packages as follows:
 
 
 
npm install --save-dev usemin-cli@0.5.1 cssmin@0.4.3 uglifyjs@2.4.11 htmlmin@0.0
  .7
 
 
 
  • Add the following two scripts to the package.json file:

 
"usemin": "usemin contactus.html -d dist --htmlmin -o dist/contactus.html &&
      usemin aboutus.html -d dist --htmlmin -o dist/aboutus.html && usemin index
      .html -d dist --htmlmin -o dist/index.html",
"build": "npm run clean && npm run imagemin && npm run copyfonts && npm run
      usemin"
 
 
 
 
  • Open index.html and surround the css links inclusion code as follows:
 
 
 
<!-- build:css css/main.css -->
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min
      .css">
<link rel="stylesheet" href="node_modules/font-awesome/css/font-awesome.min
      .css">
<link rel="stylesheet" href="node_modules/bootstrap-social/bootstrap-social
      .css">
<link href="css/styles.css" rel="stylesheet">
<!-- endbuild -->
 
 
 
  • Do the same change in aboutus.html and contactus.html
  • Similarly, open index.html and surround the js script inclusion code as follows:

 
 
<!-- build:js js/main.js -->
<script src="node_modules/jquery/dist/jquery.slim.min.js"></script>
<script src="node_modules/popper.js/dist/umd/popper.min.js"></script>
<script src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
<script src="js/scripts.js"></script>
<!-- endbuild -->
 
 
 
  • Do the same change in aboutus.html and contactus.html
  • To build the distribution folder, you can type the following at the prompt:

 
 
npm run build
 
 
 
  • This will build the dist folder containing the files that are a self-contained version of your project. You can now copy the contents of this folder to a web server that hosts your website.
  • After verifying that the dist folder is built correctly, you can now do a git commit with the message "NPM Scripts Part 2"

Conclusions

In this exercise, you learnt the various steps to build the project for deployment using NPM scripts.

"scripts": {
"start": "npm run watch:all",
"test": "echo \"Error: no test specified\" && exit 1",
"lite": "lite-server",
"scss": "node-sass -o css/ css/",
"watch:scss": "onchange \"css/*.scss\" -- npm run scss",
"watch:all": "parallelshell \"npm run watch:scss\" \"npm run lite\"",
"clean": "rimraf dist",
"copyfonts": "copyfiles -f node_modules/font-awesome/fonts/* dist/fonts",
"imagemin": "imagemin img/* -o dist/img",
"usemin": "usemin contactus.html -d dist --htmlmin -o dist/contactus.html && usemin aboutus.html -d dist --htmlmin -o dist/aboutus.html && usemin index.html -d dist --htmlmin -o dist/index.html",
"build": "npm run clean && npm run copyfonts && npm run imagemin && npm run usemin"
},
 

NPM Scripts 2 -- rimraf copyfiles imagemin usemin htmlmin uglifyjs的更多相关文章

  1. npm Scripts使用教程【译】

    Why npm Scripts? 原文发表于 2016.2.12,原文地址: https://css-tricks.com/why-npm-scripts/ 以下是访客Damon Bauer发布的一篇 ...

  2. npm scripts + webpack 实践经验(React、Nodejs)

    最近用Webpack+npm scripts+Mongodb+Nodejs+React写了个后台项目,在用Webpack构建过程中遇到了许多坑,就写出来分享一下. 构建工具五花八门,想当年刚学会Gru ...

  3. npm scripts 使用指南

    转载自:http://www.ruanyifeng.com/blog/2016/10/npm_scripts.html Node 开发离不开 npm,而脚本功能是 npm 最强大.最常用的功能之一. ...

  4. 二、npm scripts

    一.执行原理 安装npm 包,会将其package.json bin 字段添加到node_modules bin 里面,创建对应的.cmd文件,因此: 例如: "scripts": ...

  5. [NPM] Make npm scripts cross-environment friendly

    Unfortunately not all shell commands work across various environments. Two main techniques to suppor ...

  6. npm scripts 助力前端开发,实时刷新浏览器

    用browser-sync或者lite-server来监控文件的改变,当文件改变时,就自动刷新浏览器. 用node-sass来实时编译scss文件. 用parallelshell来异步执行npm sc ...

  7. windows下npm scripts不能执行的问题

    最近在学webpack为了方便把运行脚本写入package.json文件中,如下: "scripts": { "start": "webpack-de ...

  8. 5.npm scripts 使用指南

    简单介绍 scripts里面的 "start": "node app" npm run start 相当于 node app { "name" ...

  9. npm scripts 脚本基础指南

    什么是npm脚本? npm 允许在package.json文件里面,使用scripts字段定义脚本命令. 初始化package.json -> npm init -> 经历一系列的问答即可 ...

随机推荐

  1. Robberies---hdu2955(概率dp,01背包)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2955 题目给了每个银行的钱和被抓的概率,由于要抢尽量多的钱,所以要保证尽量不被抓,而抢多个银行之后不被 ...

  2. ubuntu安装mysql步骤

    https://dev.mysql.com/downloads/file/?id=477124 ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get inst ...

  3. git学习------>git-rev-parse命令初识

    一.准备工作 第一步:在d盘git test目录下,新建工作区根目录demo,进入该目录后,执行git init创建版本库. DH207891+OuyangPeng@DH207891 MINGW32 ...

  4. 日志输出:控制台和log文件输出日志

    self_log.py 中 import os import logging import time # 如果日志文件夹不存在,则创建 log_dir = "log" # 日志存放 ...

  5. WebRtc(网页即时通讯技术)知识点总结

    前言 WebRTC,名称源自网页实时通信(Web Real-Time Communication)的缩写,简而言之它是一个支持网页浏览器进行实时语音对话或视频对话的技术.并且还支持跨平台:window ...

  6. 005-Shell echo命令

    一.概述 Shell 的 echo 指令,用于字符串的输出.命令格式: echo string 可以使用echo实现更复杂的输出格式控制. 1.显示普通字符串: echo "It is a ...

  7. Upsource——对已签入的代码进行分享、讨论和审查代码

    Upsource 一.Upsource简介 Upsource ,这是一个专门为软件开发团队所设计的源代码协作工具.Upsource能够与多种版本控制工具进行集成,包括Git.Mercurial.Sub ...

  8. Loadrunner中参数化取值方式分析

    Loadrunner中参数化取值依赖两个维度: 1.取值顺序分为“顺序”“随机”“唯一”.    select next row:Sequential , Random,unique 2.更新值时分为 ...

  9. MySQL协议分析(2)

    MySQL协议分析(2) 此阶段是在压缩传输无加密条件下进行的协议分析 思路 结合Oracle官网的说明和自己用wireshark加python进行数据包分析 步骤 客户端与服务器端是否压缩的协商阶段 ...

  10. 查看Oracle相关日志 ADRCI

    ADRCI 进去以后 show  home