-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.cjs
96 lines (89 loc) · 2.69 KB
/
build.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const { build } = require("vite");
const fg = require("fast-glob");
const vue = require("@vitejs/plugin-vue");
const vueJsx = require("@vitejs/plugin-vue-jsx");
const path = require("path");
const gulp = require("gulp");
const fs = require("fs");
async function buildTask(mode) {
// 获取默认为项目根目录下 src 文件夹包括子文件夹的所有 js、ts、jsx、tsx、vue、sevele 文件路径,除开 .test.*、.spec.* 和 .d.ts 三种后缀名的文件
// 返回格式为 ['src/**/*.ts', 'src/**/*.tsx']
const srcFilePaths = fg.sync(
[`src/**/*.ts`, `src/**/*.tsx`, `src/**/*.vue`],
{
ignore: [
`**/*.spec.*`,
"**/*.test.*",
"**/*.d.ts",
"**/__tests__/**",
"**/__test__/**",
],
}
);
for (let i = 0; i < srcFilePaths.length; i++) {
const item = srcFilePaths[i];
const pathSS = path.resolve(__dirname, item);
const outDir = item.split("/");
const filename = outDir.splice(outDir.length - 1, 1);
await build({
mode: "production",
configFile: false,
logLevel: "error",
build: {
outDir: outDir
.join("/")
.replaceAll("src", `dist/${mode == "es" ? mode : "lib"}`),
emptyOutDir: false,
sourcemap: true,
lib: {
entry: pathSS,
formats: [mode],
// the proper extensions will be added
// fileName: filename[0].split('.')[0]
fileName: () => {
return (
filename[0].split(".")[0] + `.${mode == "es" ? "mjs" : "js"}`
);
},
},
rollupOptions: {
// 确保外部化处理那些你不想打包进库的依赖
external: ["vue"],
output: {
// preserveModules: true,
exports: "named",
// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
globals: {
vue: "Vue",
},
},
},
},
plugins: [
vue(),
vueJsx({
// options are passed on to @vue/babel-plugin-jsx
}),
],
});
}
// fs.copyFileSync(path.resolve(__dirname, 'lib/_base/base.css'), './dist/_base/base.css');
}
gulp.task("buildES", function () {
return buildTask("es");
});
gulp.task("buildLib", function () {
return buildTask("cjs");
});
gulp.task("generateTypes", async function () {
return gulp.src(`dist/es/**/*.d.ts`).pipe(gulp.dest(`dist/lib`));
});
gulp.task(
"compileBuild",
gulp.series(["buildES", "buildLib", "generateTypes"])
);
const taskInstance = gulp.task("compileBuild");
if (taskInstance === undefined) {
console.error("no task named compileLib registered");
}
taskInstance.apply(gulp);