gulp入门(gulp 或者 npm run xxx来执行),个人觉得和直接用node.js写区别不大

故事扮演 提交于 2020-12-18 09:36:34

1)安装gulp  参考:https://www.gulpjs.com.cn/

sudo npm install gulp-cli -g --unsafe-perm=true --allow-root
sudo npm install gulp -D --unsafe-perm=true --allow-root
sudo npx -p touch nodetouch gulpfile.js --unsafe-perm=true --allow-root
gulp --help

2)创建工程

安装 gulp 命令行工具
npm install --global gulp-cli

创建项目目录并进入
npx mkdirp my-project
cd my-project

在项目目录下创建 package.json 文件
npm init

安装 gulp,作为开发时依赖项
npm install --save-dev gulp

检查 gulp 版本
gulp --version

3)实现逻辑功能

➜  my-project tree
.
|____readfile.js
|____package-lock.json
|____package.json
|____gulpfile.js
|____.idea

当然了node_modules也是存在的

4)实现如下
|____readfile.js

let fs = require("fs");
let path = require("path")

async function asyncAwaitTask() {
    let str = await fs.readFileSync(path.join(__dirname, "package.json")).toString();
    let obj = JSON.parse(str);
    console.log(obj.version);
    return new Promise(function (resolve, reject) {
        resolve("read file version done");
    });
}

module.exports = {
    asyncAwaitTask: asyncAwaitTask,
};

|____package-lock.json
|____package.json

{
  "name": "my-project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "deploy": "gulp"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "gulp": "^4.0.2"
  }
}

|____gulpfile.js

let readfile = require("./readfile");

async function defaultTask(cb) {
    let str = await readfile.asyncAwaitTask();
    console.log(str)
    cb();
}

exports.default = defaultTask;

5)运行

➜  my-project gulp
[14:53:55] Using gulpfile ~/Desktop/my-project/gulpfile.js
[14:53:55] Starting 'default'...
1.0.0
read file version done
[14:53:55] Finished 'default' after 2.56 ms

或者用npm执行

➜  my-project npm run deploy

> my-project@1.0.0 deploy /Users/jianan/Desktop/my-project
> gulp

[15:00:26] Using gulpfile ~/Desktop/my-project/gulpfile.js
[15:00:26] Starting 'default'...
1.0.0
read file version done
[15:00:26] Finished 'default' after 1.84 ms

 

 

 

 

 

 

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!