NPM Scripts 2 -- rimraf copyfiles imagemin usemin htmlmin uglifyjs

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 [email protected]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 [email protected]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-[email protected]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-[email protected]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-[email protected]0.5.1 [email protected]0.4.3 [email protected]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"

},

Additional Resources

NPM Modules

原文地址:https://www.cnblogs.com/marcocao/p/9083047.html

时间: 2024-09-30 04:53:03

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

npm scripts 使用指南

转载自:http://www.ruanyifeng.com/blog/2016/10/npm_scripts.html Node 开发离不开 npm,而脚本功能是 npm 最强大.最常用的功能之一. 本文介绍如何使用 npm 脚本(npm scripts). 一.什么是 npm 脚本? npm 允许在package.json文件里面,使用scripts字段定义脚本命令. { // ... "scripts": { "build": "node build.

npm Scripts使用教程【译】

Why npm Scripts? 原文发表于 2016.2.12,原文地址: https://css-tricks.com/why-npm-scripts/ 以下是访客Damon Bauer发布的一篇文章.近来直接使用node package提供的命令行界面的情绪持续高涨,反之,人们对通过运行任务从而屏蔽抽象功能的热情逐渐降温.在一定程度是,你无论如何都要使用npm,而同时npm提供了脚本功能,为什么不用呢?但是我们使用npm的脚本功能的原因有很多.Damon会帮我们理解这样做的原因,并确切的告

[NPM] Pass arguments to npm scripts

Often times you’ll have variations that you’ll want to make to your npm scripts and repeating yourself is inefficient and verbose. Instead you can create a base script and pass arguments into it from another script. For example, you have a script: "t

我为何放弃Gulp与Grunt,转投npm scripts(上)

本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2016/02/gulp-grunt-npm-scripts-part1 Cory House是"Building Applications with React and Flux"与"Clean Code: Writing Code for Humans"的作者,同时也是Pluralsight上众多课程的讲师.他是VinSolutions的软件架构师,在

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

最近用Webpack+npm scripts+Mongodb+Nodejs+React写了个后台项目,在用Webpack构建过程中遇到了许多坑,就写出来分享一下. 构建工具五花八门,想当年刚学会Grunt,Grunt就被淘汰了,取而代之的是Gulp,其任务流式的机制,有着逻辑清晰,灵活多变的特点,而且容易上手,相比Grunt真的要少写太多配置文件代码了,立马就学的风声水起,刚熟练Gulp,Webpack又如构建工具界的一颗新星冉冉升起,其独特的模块打包机制和各种各样好用的loader,让无数Co

[NPM] Run npm scripts in series

After creating several npm script it becomes useful to run multiple scripts back-to-back in series. This is a nice feature because you can enforce that one script needs to complete before starting another one. "scripts": { "start": &qu

[NPM] Use a shorthand syntax for running multiple npm scripts with npm-run-all

Running multiple scripts in series or in parallel can become very verbose. Using a tool such as npm-run-all can help reduce the amount of overhead you have to type in order to get the same behavior. Install: npm i -D npm-run-all "scripts": { &qu

npm scripts设置环境变量方法

windows set NODE_ENV=production "scripts": { "release": "set NODE_ENV=production && gulp rtm", "dev": "set NODE_ENV=development && gulp watch", } linux & mac export NODE_ENV=production

npm scripts构建

管道(|)运算符会将一个命令的输出以流的方式作为另一个命令的输入. 重定向(>)运算符则会将输出重定向到文件. < 将文件内容输入到一个命令 在Unix中,还可以通过“&”运算符同时运行两个命令.npm run script1.js & npm run script2.js npm-run-all   A CLI tool to run multiple npm-scripts in parallel or sequential. run-s is for sequential