Ai
0 Star 0 Fork 0

ctrlc-git/uni-vue-cli

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
generator.js 4.38 KB
一键复制 编辑 原始数据 按行查看 历史
提莫打野迷了路 提交于 2023-10-17 18:06 +08:00 . feat: 提交项目
const fs = require("fs");
const path = require("path");
const fse = require("fs-extra");
const isBinary = require("isbinaryfile");
async function generate(dir, files, base = "", rootOptions = {}) {
const glob = require("glob");
glob
.sync("**/*", {
cwd: dir,
nodir: true,
})
.forEach((rawPath) => {
const sourcePath = path.resolve(dir, rawPath);
const filename = path.join(base, rawPath);
if (isBinary.sync(sourcePath)) {
files[filename] = fs.readFileSync(sourcePath); // return buffer
} else {
let content = fs.readFileSync(sourcePath, "utf-8");
if (path.basename(filename) === "manifest.json") {
content = content.replace("{{name}}", rootOptions.projectName || "");
}
if (filename.charAt(0) === "_" && filename.charAt(1) !== "_") {
files[`.${filename.slice(1)}`] = content;
} else if (filename.charAt(0) === "_" && filename.charAt(1) === "_") {
files[`${filename.slice(1)}`] = content;
} else {
files[filename] = content;
}
}
});
}
module.exports = (api, options, rootOptions) => {
// 扩展包
api.extendPackage((pkg) => {
return {
scripts: {
"custom:h5": "cross-env NODE_ENV=development UNI_PLATFORM=h5 CUSTOM_ENV=custom vue-cli-service uni-serve",
"custom:app-plus": "cross-env NODE_ENV=development UNI_PLATFORM=app-plus CUSTOM_ENV=custom vue-cli-service uni-build --watch",
"lint": "vue-cli-service lint --fix"
},
dependencies: {
// 锁定版本,避免高版本在小程序中出错
"regenerator-runtime": "^0.12.1",
"@dcloudio/uni-helper-json": "*",
},
devDependencies: {
// 临时指定版本,7.13.x 会报错
"@babel/runtime": "~7.12.0",
"postcss-comment": "^2.0.0",
"@dcloudio/types": "*",
"miniprogram-api-typings": "*",
"mini-types": "*",
"@commitlint/cli": "^12.0.1",
"@commitlint/config-conventional": "^12.0.1",
"lint-staged": "^10.5.4"
},
eslintConfig: {},
gitHooks: {
"pre-commit": "sh git.sh && lint-staged",
"commit-msg": "commitlint -e $GIT_PARAMS",
},
"lint-staged": {
"src/**/*.{js,vue}": ["vue-cli-service lint", "git add"],
},
};
});
if (options.template === "default-ts") {
// 启用 typescript
api.extendPackage((pkg) => {
return {
dependencies: {
"vue-class-component": "^6.3.2",
"vue-property-decorator": "^8.0.0",
},
devDependencies: {
"@babel/plugin-syntax-typescript": "^7.2.0",
"@vue/cli-plugin-typescript": "*",
typescript: api.hasPlugin("eslint") ? "~3.1.1" : "^3.0.0",
},
};
});
}
if (options.template === "space/tpl-wx") {
// 启用 微信ci上传
api.extendPackage((pkg) => {
return {
scripts: {
"serve": "npm run dev:mp-weixin",
"build": "npm run build:mp-weixin",
"build:mp-weixin": "cross-env NODE_ENV=production UNI_PLATFORM=mp-weixin vue-cli-service uni-build",
"dev:mp-weixin": "cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin CUSTOM_ENV=custom vue-cli-service uni-build --watch",
"cli": "npm run build && node cli/miniprogram.js robot=1",
"cli:up": "node cli/miniprogram.js robot=1",
},
devDependencies: {
"miniprogram-ci": "^1.2.0",
},
};
});
}
// 创建模板
api.render(async function (files) {
Object.keys(files).forEach((name) => {
delete files[name];
});
const template = options.repo || options.template;
const base = "src";
await generate(path.resolve(__dirname, "./template/common"), files);
if (template === "default" || template === "space/tpl-wx") {
await generate(
path.resolve(__dirname, "./template/default"),
files,
base,
rootOptions
);
} else if (template === "default-ts") {
await generate(path.resolve(__dirname, "./template/common-ts"), files);
await generate(
path.resolve(__dirname, "./template/default-ts"),
files,
base,
rootOptions
);
}
if (template === "space/tpl-wx") {
await generate(
path.resolve(__dirname, "./template/cli-wx"),
files,
'cli',
rootOptions
);
}
});
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/ctrlc-git/uni-vue-cli.git
git@gitee.com:ctrlc-git/uni-vue-cli.git
ctrlc-git
uni-vue-cli
uni-vue-cli
develop

搜索帮助