Skip to content

Commit 15a06f0

Browse files
authored
Merge pull request #1 from coderYarn/coderYarn/init
Init Unitify Project
2 parents af4b6ed + f01d1c5 commit 15a06f0

File tree

122 files changed

+28277
-1
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+28277
-1
lines changed

.gitignore

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.DS_Store
2+
node_modules
3+
/dist
4+
5+
6+
# local env files
7+
.env.local
8+
.env.*.local
9+
10+
# Log files
11+
npm-debug.log*
12+
yarn-debug.log*
13+
yarn-error.log*
14+
pnpm-debug.log*
15+
16+
# Editor directories and files
17+
.idea
18+
.vscode
19+
*.suo
20+
*.ntvs*
21+
*.njsproj
22+
*.sln
23+
*.sw?

.npmignore

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 忽略目录
2+
.idea
3+
.vscode
4+
build/
5+
docs/
6+
examples/
7+
packages/
8+
public/
9+
node_modules/
10+
typings/
11+
12+
# 忽略指定文件
13+
babel.config.js
14+
tsconfig.json
15+
tslint.json
16+
vue.config.js
17+
.gitignore
18+
.browserslistrc
19+
*.map

README.md

-1
This file was deleted.

build/dts.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { resolve, dirname } from 'path'
2+
import fs from 'fs/promises'
3+
import { createFilter } from '@rollup/pluginutils'
4+
import { normalizePath } from 'vite'
5+
import { Project } from 'ts-morph'
6+
7+
import type { Plugin } from 'vite'
8+
import type { SourceFile } from 'ts-morph'
9+
10+
export default (): Plugin => {
11+
const filter = createFilter(['**/*.vue', '**/*.ts'], 'node_modules/**')
12+
const sourceFiles: SourceFile[] = []
13+
14+
const project = new Project({
15+
compilerOptions: {
16+
declaration: true,
17+
emitDeclarationOnly: true,
18+
noEmitOnError: true,
19+
allowJs: true, // 如果想兼容 js 语法需要加上
20+
outDir: 'dist' // 可以设置自定义的打包文件夹,如 'types'
21+
},
22+
tsConfigFilePath: resolve(__dirname, '../tsconfig.json'),
23+
skipAddingFilesFromTsConfig: true
24+
})
25+
26+
let root: string
27+
28+
return {
29+
name: 'gen-dts',
30+
apply: 'build',
31+
enforce: 'pre', // 需要在 pre 才能正确拿到 ts 的 script 部分
32+
configResolved(config) {
33+
root = config.root
34+
},
35+
transform(code, id) {
36+
if (!code || !filter(id)) return null
37+
38+
// 拆分后的文件 id 具有一些特征,可以用正则的方式来捕获
39+
if (/\.vue(\?.*type=script.*)$/.test(id)) {
40+
const filePath = resolve(root, normalizePath(id.split('?')[0]))
41+
42+
sourceFiles.push(
43+
project.createSourceFile(filePath + (/lang.ts/.test(id) ? '.ts' : '.js'), code)
44+
)
45+
} else if (/\.ts$/.test(id)) {
46+
const filePath = resolve(root, normalizePath(id))
47+
48+
sourceFiles.push(project.addSourceFileAtPath(filePath))
49+
}
50+
},
51+
async generateBundle() {
52+
const diagnostics = project.getPreEmitDiagnostics()
53+
54+
55+
project.emitToMemory()
56+
57+
// 随后将解析完的文件写道打包路径
58+
for (const sourceFile of sourceFiles) {
59+
const emitOutput = sourceFile.getEmitOutput()
60+
61+
for (const outputFile of emitOutput.getOutputFiles()) {
62+
const filePath = outputFile.getFilePath()
63+
64+
await fs.mkdir(dirname(filePath), { recursive: true })
65+
await fs.writeFile(filePath, outputFile.getText(), 'utf8')
66+
}
67+
}
68+
}
69+
}
70+
}
71+
72+

build/vite-plugin-Insert.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import fs from "fs";
2+
import { resolve } from "path";
3+
4+
export default function framework(config) {
5+
const { targetPath, fileName, originPath } = config;
6+
7+
return {
8+
closeBundle() {
9+
fs.readFile(originPath, "utf-8", (err, data) => {
10+
if (err) {
11+
console.log(originPath, "读取失败");
12+
return;
13+
}
14+
fs.writeFile(resolve(targetPath, fileName), data, (err) => {
15+
if (!err) {
16+
console.log(resolve(targetPath, fileName), "写入完成");
17+
} else {
18+
console.log(resolve(targetPath, fileName), "写入失败");
19+
}
20+
});
21+
});
22+
},
23+
};
24+
}

build/vite.config.build.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { defineConfig } from "vite";
2+
import vue from "@vitejs/plugin-vue";
3+
import jsx from "@vitejs/plugin-vue-jsx";
4+
import InsertAfter from "./vite-plugin-Insert";
5+
import dts from "./dts";
6+
import { resolve } from "path";
7+
8+
export default defineConfig({
9+
mode:"production",
10+
11+
plugins: [
12+
vue(),
13+
jsx(),
14+
dts(),
15+
InsertAfter({
16+
targetPath: resolve(__dirname, "../lib"),
17+
fileName: "package.json",
18+
originPath: resolve(__dirname, "../packages/core/package.json"),
19+
}),
20+
],
21+
resolve: {
22+
alias:{
23+
'/@': resolve('../node_modules/vue'),
24+
'@': '/src'
25+
},
26+
preserveSymlinks: false
27+
},
28+
build: {
29+
minify: "terser",
30+
outDir: resolve(__dirname, "../lib"),
31+
lib: {
32+
entry: "./packages/core/index.ts",
33+
name: "unitify2",
34+
fileName: (format) =>`unitfiy.${format}.js`
35+
},
36+
37+
rollupOptions: {
38+
external: ["vue"],
39+
output:{
40+
globals: {
41+
vue: "Vue",
42+
},
43+
}
44+
45+
},
46+
},
47+
});

build/vite.config.serve.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { defineConfig } from "vite";
2+
import vue from "@vitejs/plugin-vue";
3+
4+
import { resolve } from "path";
5+
6+
export default defineConfig({
7+
mode: "development",
8+
plugins: [vue()],
9+
resolve: {
10+
alias: {
11+
'@': '/src'
12+
}
13+
},
14+
css:{
15+
preprocessorOptions: {
16+
scss: {
17+
additionalData: '@import "../globalVariable.scss";'
18+
}
19+
}
20+
},
21+
build: {
22+
minify: "terser",
23+
outDir: resolve(__dirname, "../dist"),
24+
25+
rollupOptions: {
26+
external: ["vue"],
27+
output: {
28+
globals: {
29+
vue: "Vue",
30+
},
31+
},
32+
},
33+
},
34+
});

index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" href="/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite App</title>
8+
</head>
9+
<body>
10+
<div id="app"></div>
11+
<script type="module" src="/examples/main.ts"></script>
12+
</body>
13+
</html>

0 commit comments

Comments
 (0)