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. style2paints、deepcolor、sketchkeras项目

    数据集不够怎么办? 1 一些传统的边缘提取算法可以提取图像边缘. 2 这里我们有一个使用神经网络提取线稿图的项目——sketchkeras 源码:https://github.com/lllyasvi ...

  2. Linux基础配置

    1  常用软件安装 yum install -y bash-completion vim lrzsz wget expect net-tools nc nmap tree dos2unix htop ...

  3. rest_framework 之版本控制

    一 作用 用于版本的控制 二 内置的版本控制类 from rest_framework.versioning import QueryParameterVersioning,AcceptHeaderV ...

  4. python基础之小数据池、代码块、编码

    一.代码块.if True: print(333) print(666) while 1: a = 1 b = 2 print(a+b) for i in '12324354': print(i) 虽 ...

  5. git mv与直接mv的区别

    git mv 行为: 1.创建一个和之前文件内容一样的文件,文件名为新的文件名 2.将原来的文件删除 3.将删除的文件添加到暂存区 4.将新建的文件添加到暂存区 $ git mv a a1 $ git ...

  6. mysql 数据库查询最后两条数据

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011925175/article/details/24186917  有一个mysql数据库的 ...

  7. Spark2.0机器学习系列之3:决策树

    概述 分类决策树模型是一种描述对实例进行分类的树形结构. 决策树可以看为一个if-then规则集合,具有“互斥完备”性质 .决策树基本上都是 采用的是贪心(即非回溯)的算法,自顶向下递归分治构造. 生 ...

  8. ftp 工作原理

  9. IBM Java 7 新特性和在 WAS 8.5 中的配置【转载】

    IBM Java 7新特性以及在WAS V8.5 中的安装与版本切换 简介: 本文介绍了 IBM Java 7 的基本新特性以及 IBM 特有的新特性,并详细的介绍和分析了 JVM 所采用的新的垃圾回 ...

  10. STL: fill,fill_n,generate,generate_n

    fill Assigns the same new value to every element in a specified range. template<class ForwardIter ...