Commit 23278ae7 authored by 谢卓城's avatar 谢卓城

新增:1.gulp prod

parent fa9bbe9a
...@@ -11,18 +11,18 @@ const tap = require("gulp-tap"); ...@@ -11,18 +11,18 @@ const tap = require("gulp-tap");
const path = require("path"); const path = require("path");
const notify = require("gulp-notify"); const notify = require("gulp-notify");
const yargs = require("yargs"); const yargs = require("yargs");
const fs = require('fs'); const fs = require("fs");
const srcPath = "./src/**"; const srcPath = "./src/**";
const distPath = "./dist/"; const distPath = "./dist/";
//存放variable和mixin的sass文件在被引用时直接导入,不引入dist目录中 //存放variable和mixin的sass文件在被引用时直接导入,不引入dist目录中
const DIRECTIMPORT = ["/scss/", "/font/"]; const DIRECTIMPORT = ["/scss/", "/font/"];
const onError = function(err) { const onError = function (err) {
notify.onError({ notify.onError({
title: "Gulp", title: "Gulp",
subtitle: "Failure!", subtitle: "Failure!",
message: "Error: <%= error.message %>", message: "Error: <%= error.message %>",
sound: "Beep" sound: "Beep",
})(err); })(err);
this.emit("end"); this.emit("end");
...@@ -34,11 +34,11 @@ const jsFiles = [`${srcPath}/*.js`, `!${srcPath}/env/*.js`]; ...@@ -34,11 +34,11 @@ const jsFiles = [`${srcPath}/*.js`, `!${srcPath}/env/*.js`];
const jsonFiles = [`${srcPath}/*.json`]; const jsonFiles = [`${srcPath}/*.json`];
const imageFiles = [ const imageFiles = [
`${srcPath}/images/*.{png,jpeg,jpg,gif,ico}`, `${srcPath}/images/*.{png,jpeg,jpg,gif,ico}`,
`${srcPath}/images/**/*.{png,jpeg,jpg,gif,ico}` `${srcPath}/images/**/*.{png,jpeg,jpg,gif,ico}`,
]; ];
/* 清除dist目录 */ /* 清除dist目录 */
gulp.task("clean", done => { gulp.task("clean", (done) => {
del.sync(["dist/**", "!dist/miniprogram_npm"]); del.sync(["dist/**", "!dist/miniprogram_npm"]);
done(); done();
}); });
...@@ -57,7 +57,7 @@ const js = () => { ...@@ -57,7 +57,7 @@ const js = () => {
}; };
gulp.task(js); gulp.task(js);
const envJs = env => { const envJs = (env) => {
return () => { return () => {
return gulp return gulp
.src(`./src/env/${env}.js`) .src(`./src/env/${env}.js`)
...@@ -80,12 +80,15 @@ gulp.task(json); ...@@ -80,12 +80,15 @@ gulp.task(json);
const wxss = () => { const wxss = () => {
return gulp return gulp
.src([...sassFiles, ...DIRECTIMPORT.map(item => `!${srcPath}/${item}/*`)], { .src(
since: gulp.lastRun(wxss) [...sassFiles, ...DIRECTIMPORT.map((item) => `!${srcPath}/${item}/*`)],
}) {
since: gulp.lastRun(wxss),
}
)
.pipe(plumber({ errorHandler: onError })) .pipe(plumber({ errorHandler: onError }))
.pipe( .pipe(
tap(file => { tap((file) => {
const filePath = path.dirname(file.path); const filePath = path.dirname(file.path);
// console.log("filepath", filePath); // console.log("filepath", filePath);
file.contents = Buffer.from( file.contents = Buffer.from(
...@@ -94,7 +97,7 @@ const wxss = () => { ...@@ -94,7 +97,7 @@ const wxss = () => {
($1, $2) => { ($1, $2) => {
// console.log("$1", $1); // console.log("$1", $1);
// console.log("$2", $2); // console.log("$2", $2);
return DIRECTIMPORT.some(item => { return DIRECTIMPORT.some((item) => {
return $2.indexOf(item) > -1; return $2.indexOf(item) > -1;
}) })
? $1 ? $1
...@@ -127,7 +130,7 @@ const img = () => { ...@@ -127,7 +130,7 @@ const img = () => {
}; };
gulp.task(img); gulp.task(img);
const newfile = done => { const newfile = (done) => {
yargs yargs
.example("gulp new -p mypage", "创建mypage的page目录") .example("gulp new -p mypage", "创建mypage的page目录")
.example("gulp new -c mycomponent", "创建mycomponent的component目录") .example("gulp new -c mycomponent", "创建mycomponent的component目录")
...@@ -141,30 +144,30 @@ const newfile = done => { ...@@ -141,30 +144,30 @@ const newfile = done => {
alias: "src", alias: "src",
describe: "模板", describe: "模板",
type: "string", type: "string",
default: "template" default: "template",
}, },
p: { p: {
alias: "page", alias: "page",
describe: "page名称", describe: "page名称",
type: "string" type: "string",
}, },
c: { c: {
alias: "component", alias: "component",
describe: "component名称", describe: "component名称",
type: "string" type: "string",
}, },
t: { t: {
alias: "template", alias: "template",
describe: "components里的模板", describe: "components里的模板",
type: "string" type: "string",
}, },
i: { i: {
alias: "index", alias: "index",
describe: "page、components生成的文件与文件夹名称一致", describe: "page、components生成的文件与文件夹名称一致",
type: "string" type: "string",
} },
}) })
.fail(msg => { .fail((msg) => {
done(); done();
console.error("创建失败"); console.error("创建失败");
console.log(msg); console.log(msg);
...@@ -177,7 +180,7 @@ const newfile = done => { ...@@ -177,7 +180,7 @@ const newfile = done => {
const source = args.s; const source = args.s;
const filePaths = { const filePaths = {
p: "pages", p: "pages",
c: "components" c: "components",
}; };
let name, let name,
...@@ -208,31 +211,31 @@ const newfile = done => { ...@@ -208,31 +211,31 @@ const newfile = done => {
} }
if (type == "pages") { if (type == "pages") {
let jsonPath = './src/app.json'; let jsonPath = "./src/app.json";
fs.readFile(jsonPath,'utf8',function (err, data) { fs.readFile(jsonPath, "utf8", function (err, data) {
if(err) console.log(err); if (err) console.log(err);
var json = JSON.parse(data);//读取的值 var json = JSON.parse(data); //读取的值
json.pages.push(`${type}/${name}/${name}`); json.pages.push(`${type}/${name}/${name}`);
//把数据读出来,然后进行修改 //把数据读出来,然后进行修改
var str = JSON.stringify(json,null,2); var str = JSON.stringify(json, null, 2);
//console.log(str); //console.log(str);
fs.writeFile(jsonPath,str,function(err){ fs.writeFile(jsonPath, str, function (err) {
if(err){ if (err) {
console.error(err); console.error(err);
} }
}); });
}); });
}else { } else {
stream = stream.pipe(replace('container', `${name}-container`)); stream = stream.pipe(replace("container", `${name}-container`));
} }
return stream.pipe(gulp.dest(`src/${type}/${name}/`)); return stream.pipe(gulp.dest(`src/${type}/${name}/`));
}; };
gulp.task("new", newfile); gulp.task("new", newfile);
gulp.task("watch", done => { gulp.task("watch", (done) => {
const watchSassFiles = [ const watchSassFiles = [
...sassFiles, ...sassFiles,
...DIRECTIMPORT.map(item => `!${srcPath}/${item}/**/*`) ...DIRECTIMPORT.map((item) => `!${srcPath}/${item}/**/*`),
]; ];
gulp.watch(watchSassFiles, wxss); gulp.watch(watchSassFiles, wxss);
gulp.watch(jsFiles, js); gulp.watch(jsFiles, js);
...@@ -249,6 +252,13 @@ gulp.task( ...@@ -249,6 +252,13 @@ gulp.task(
gulp.parallel("wxml", "js", "json", "wxss", "img", "prodEnv") gulp.parallel("wxml", "js", "json", "wxss", "img", "prodEnv")
) )
); );
gulp.task(
"prod",
gulp.series(
"clean",
gulp.parallel("wxml", "js", "json", "wxss", "img", "prodEnv")
)
);
gulp.task( gulp.task(
"dev", "dev",
gulp.series( gulp.series(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment