Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

小程序gulp相关配置 #35

Open
mtonhuang opened this issue May 25, 2021 · 0 comments
Open

小程序gulp相关配置 #35

mtonhuang opened this issue May 25, 2021 · 0 comments

Comments

@mtonhuang
Copy link
Owner

var gulp = require("gulp");
var path = require("path");
var rename = require("gulp-rename");
var less = require("gulp-less");
var postcss = require("gulp-postcss");
var changed = require("gulp-changed");
var autoprefixer = require("autoprefixer");
var clear = require("gulp-clean");
var del = require("del");
var sourcemaps = require("gulp-sourcemaps");
var jsonTransform = require("gulp-json-transform");
var projectConfig = require("./package.json");
var alias = require("gulp-ts-path-alias");

const through = require("through2");
const dependency = require("dependency-tree");
const webpack = require("webpack");
const readPkg = require("read-pkg-up");
const matches = require("match-requires");

function handlerRelivePath(relative) {
    if (path.sep === "\\") {
        relative = relative.split(path.sep).join("/");
    }
    return /^\./.test(relative) ? relative : `./${relative}`;
}

// 项目路径
var option = {
    base: "src",
    allowEmpty: true
};
var dist = __dirname + "/dist";
var copyPath = ["src/**/!(_)*.*", "!src/**/*.less", "!src/**/*.ts"];
var lessPath = ["src/**/*.less", "src/app.less"];
var watchLessPath = ["src/**/*.less", "src/css/**/*.less", "src/app.less"];

// 清空目录
gulp.task("clear", () => {
    return gulp.src(dist, { allowEmpty: true }).pipe(clear());
});

// 复制不包含less和图片的文件
gulp.task("copy", () => {
    return gulp.src(copyPath, option).pipe(gulp.dest(dist));
});
// 复制不包含less和图片的文件(只改动有变动的文件)
gulp.task("copyChange", () => {
    return gulp
        .src(copyPath, option)
        .pipe(changed(dist))
        .pipe(gulp.dest(dist));
});

gulp.task("npm", () => {
    return gulp
        .src("dist/**/*.js")
        .pipe(
            npm({
                dest: "dist"
            })
        )
        .pipe(gulp.dest("dist"));
});

// 编译less
gulp.task("less", () => {
    return gulp
        .src(lessPath, option)
        .pipe(
            less().on("error", function (e) {
                console.error(e.message);
                this.emit("end");
            })
        )
        .pipe(postcss([autoprefixer]))
        .pipe(
            rename(function (path) {
                path.extname = ".wxss";
            })
        )
        .pipe(gulp.dest(dist));
});

// 编译less(只改动有变动的文件)
gulp.task("lessChange", () => {
    return gulp
        .src(lessPath, option)
        .pipe(changed(dist))
        .pipe(
            less().on("error", function (e) {
                console.error(e.message);
                this.emit("end");
            })
        )
        .pipe(postcss([autoprefixer]))
        .pipe(
            rename(function (path) {
                path.extname = ".wxss";
            })
        )
        .pipe(gulp.dest(dist));
});

// 更改project.config.json的miniprogramRoot,并将其打包到dist目录
gulp.task("addProjectConfig", () => {
    return gulp.src("project.config.json")
        .pipe(
            jsonTransform(function (data, file) {
                data.miniprogramRoot = "/";
                return data;
            })
        )
        .pipe(gulp.dest("dist"));
});

// 监听
gulp.task("watch", () => {
    var watcher = gulp.watch(copyPath, gulp.series("npm", "copyChange"));
    gulp.watch(watchLessPath, gulp.series("less")); // Change

    // 监听dist目录,有变动就执行npm
    // gulp.watch(tsPath, gulp.series('npm'));

    watcher.on("change", function (event) {
        if (event.type === "deleted") {
            var filepath = event.path;
            var filePathFromSrc = path.relative(path.resolve("src"), filepath);
            // Concatenating the 'build' absolute path used by gulp.dest in the scripts task
            var destFilePath = path.resolve("dist", filePathFromSrc);
            // console.log({filepath, filePathFromSrc, destFilePath})
            del.sync(destFilePath);
        }
    });
});

// 开发并监听
gulp.task(
    "default",
    gulp.series(
        // sync
        gulp.parallel(
            "copy",
            "less"
        ),
        "npm",
        "watch"
    )
);

// 上线
gulp.task(
    "build",
    gulp.series(
        // sync
        "clear",
        gulp.parallel(
            // async
            "copy",
            "less"
            "addProjectConfig"
        ),
        "npm"
    )
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant