@uni-helper/vite-plugin-uni-pages 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +9 -0
- package/dist/index.cjs +156 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.mjs +149 -0
- package/package.json +63 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 KeJun <https://github.com/kejunmao>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
const vite = require('vite');
|
|
6
|
+
const unconfig = require('unconfig');
|
|
7
|
+
const path = require('path');
|
|
8
|
+
const fs = require('fs');
|
|
9
|
+
const fg = require('fast-glob');
|
|
10
|
+
const compilerDom = require('@vue/compiler-dom');
|
|
11
|
+
const jsoncParser = require('jsonc-parser');
|
|
12
|
+
const defu = require('defu');
|
|
13
|
+
|
|
14
|
+
const isPagePath = (path$1, options) => {
|
|
15
|
+
const dirPath = path.resolve(process.cwd(), options.pagesDir);
|
|
16
|
+
const filter = vite.createFilter(dirPath + "/**/*.(vue|nvue)");
|
|
17
|
+
return filter(path$1);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
function definePages(config) {
|
|
21
|
+
return config;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function defineMiddleware(middleware) {
|
|
25
|
+
return middleware;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function resolveOptions(userOptions) {
|
|
29
|
+
return {
|
|
30
|
+
pagesDir: "src/pages",
|
|
31
|
+
outDir: "src",
|
|
32
|
+
entry: "pages.config",
|
|
33
|
+
extension: "ts",
|
|
34
|
+
...userOptions
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
const loadUserPagesConfig = async (options) => {
|
|
38
|
+
const { config } = await unconfig.loadConfig({
|
|
39
|
+
sources: [
|
|
40
|
+
{
|
|
41
|
+
files: options.entry,
|
|
42
|
+
extensions: [options.extension]
|
|
43
|
+
}
|
|
44
|
+
],
|
|
45
|
+
merge: false
|
|
46
|
+
});
|
|
47
|
+
return config;
|
|
48
|
+
};
|
|
49
|
+
const scanPages = async (options) => {
|
|
50
|
+
const files = await fg("**/*.(vue|nvue)", {
|
|
51
|
+
ignore: ["node_modules", ".git", "**/__*__/*"],
|
|
52
|
+
onlyFiles: true,
|
|
53
|
+
cwd: path.resolve(process.cwd(), options.pagesDir)
|
|
54
|
+
});
|
|
55
|
+
const basePath = path.relative(
|
|
56
|
+
path.resolve(process.cwd(), options.outDir),
|
|
57
|
+
options.pagesDir
|
|
58
|
+
);
|
|
59
|
+
return files.map((file) => {
|
|
60
|
+
return {
|
|
61
|
+
relative: vite.normalizePath(path.join(basePath, file)),
|
|
62
|
+
absolute: vite.normalizePath(path.resolve(process.cwd(), options.pagesDir, file))
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
const parsePage = async ({
|
|
67
|
+
relative: relative2,
|
|
68
|
+
absolute
|
|
69
|
+
}) => {
|
|
70
|
+
const code = await fs.promises.readFile(absolute, {
|
|
71
|
+
encoding: "utf-8"
|
|
72
|
+
});
|
|
73
|
+
const t = compilerDom.parse(code);
|
|
74
|
+
const path$1 = relative2.replace(path.extname(relative2), "");
|
|
75
|
+
let pageMeta = {
|
|
76
|
+
path: path$1,
|
|
77
|
+
type: "page"
|
|
78
|
+
};
|
|
79
|
+
t?.children.forEach((v) => {
|
|
80
|
+
if (v.type !== 1) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (v.tag === "route") {
|
|
84
|
+
v.props.forEach((prop) => {
|
|
85
|
+
if (prop.type === 6 && prop.name === "type") {
|
|
86
|
+
pageMeta.type = prop.value?.content ?? pageMeta.type;
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
const astContent = v.children?.[0];
|
|
90
|
+
if (astContent && astContent.type === 2) {
|
|
91
|
+
const _pageMeta = jsoncParser.parse(astContent.content);
|
|
92
|
+
Object.keys(_pageMeta).forEach((key) => {
|
|
93
|
+
pageMeta[key] = _pageMeta[key];
|
|
94
|
+
});
|
|
95
|
+
pageMeta["path"] = path$1;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
return pageMeta;
|
|
100
|
+
};
|
|
101
|
+
const createOrUpdatePagesJSON = async (options, path$1) => {
|
|
102
|
+
const outputName = "pages.json";
|
|
103
|
+
const resolvedOutput = vite.normalizePath(
|
|
104
|
+
path.resolve(process.cwd(), options.outDir, outputName)
|
|
105
|
+
);
|
|
106
|
+
if (!fs.existsSync(resolvedOutput)) {
|
|
107
|
+
fs.writeFileSync(resolvedOutput, "{}", {
|
|
108
|
+
encoding: "utf-8"
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
const config = await loadUserPagesConfig(options);
|
|
112
|
+
const pagesPathList = await scanPages(options);
|
|
113
|
+
const pagesMeta = await Promise.all(
|
|
114
|
+
pagesPathList.map(async (page) => await parsePage(page))
|
|
115
|
+
);
|
|
116
|
+
const pages = Object.values(defu.defu({}, pagesMeta, config.pages));
|
|
117
|
+
pages.sort((a) => a.type === "home" ? -1 : 0);
|
|
118
|
+
config.pages = pages;
|
|
119
|
+
fs.writeFileSync(resolvedOutput, JSON.stringify(config), {
|
|
120
|
+
encoding: "utf-8"
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
const VitePluginUniPages = async (userOptions = {}) => {
|
|
124
|
+
const options = resolveOptions(userOptions);
|
|
125
|
+
const pagesConfigSource = vite.normalizePath(
|
|
126
|
+
path.resolve(process.cwd(), `${options.entry}.${options.extension}`)
|
|
127
|
+
);
|
|
128
|
+
await createOrUpdatePagesJSON(options);
|
|
129
|
+
return {
|
|
130
|
+
name: "vite-plugin-uni-pages",
|
|
131
|
+
enforce: "pre",
|
|
132
|
+
configureServer({ watcher }) {
|
|
133
|
+
watcher.add(pagesConfigSource);
|
|
134
|
+
watcher.on("change", async (path) => {
|
|
135
|
+
if (vite.normalizePath(path) === pagesConfigSource || isPagePath(path, options)) {
|
|
136
|
+
await createOrUpdatePagesJSON(options);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
watcher.on("add", async (path) => {
|
|
140
|
+
if (isPagePath(path, options)) {
|
|
141
|
+
await createOrUpdatePagesJSON(options);
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
watcher.on("unlink", async (path) => {
|
|
145
|
+
if (isPagePath(path, options)) {
|
|
146
|
+
await createOrUpdatePagesJSON(options);
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
exports.VitePluginUniPages = VitePluginUniPages;
|
|
154
|
+
exports.default = VitePluginUniPages;
|
|
155
|
+
exports.defineMiddleware = defineMiddleware;
|
|
156
|
+
exports.definePages = definePages;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { Plugin } from 'vite';
|
|
2
|
+
|
|
3
|
+
interface Options {
|
|
4
|
+
/**
|
|
5
|
+
* pages dir
|
|
6
|
+
* @default "src/pages"
|
|
7
|
+
*/
|
|
8
|
+
pagesDir: string;
|
|
9
|
+
/**
|
|
10
|
+
* pages.json dir
|
|
11
|
+
* @default "src"
|
|
12
|
+
*/
|
|
13
|
+
outDir: string;
|
|
14
|
+
/**
|
|
15
|
+
* pages config entry
|
|
16
|
+
* @default "pages.config"
|
|
17
|
+
*/
|
|
18
|
+
entry: string;
|
|
19
|
+
/**
|
|
20
|
+
* pages config extension
|
|
21
|
+
* @default "ts"
|
|
22
|
+
*/
|
|
23
|
+
extension: string;
|
|
24
|
+
}
|
|
25
|
+
interface UserOptions extends Partial<Options> {
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface PagesConfig {
|
|
29
|
+
pages: any;
|
|
30
|
+
globalStyle: any;
|
|
31
|
+
easycom: any;
|
|
32
|
+
tabBar: any;
|
|
33
|
+
condition: any;
|
|
34
|
+
subPackages: any;
|
|
35
|
+
preloadRule: any;
|
|
36
|
+
workers: any;
|
|
37
|
+
leftWindow: any;
|
|
38
|
+
topWindow: any;
|
|
39
|
+
rightWindow: any;
|
|
40
|
+
uniIdRouter: any;
|
|
41
|
+
}
|
|
42
|
+
interface UserPagesConfig extends Partial<PagesConfig> {
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
declare function definePages(config: UserPagesConfig): UserPagesConfig;
|
|
46
|
+
|
|
47
|
+
declare type Middleware = (to: string, from: string) => void | boolean | string;
|
|
48
|
+
|
|
49
|
+
declare function defineMiddleware(middleware: Middleware): Middleware;
|
|
50
|
+
|
|
51
|
+
declare const VitePluginUniPages: (userOptions?: UserOptions) => Promise<Plugin>;
|
|
52
|
+
|
|
53
|
+
export { Middleware, PagesConfig, UserPagesConfig, VitePluginUniPages, VitePluginUniPages as default, defineMiddleware, definePages };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { createFilter, normalizePath } from 'vite';
|
|
2
|
+
import { loadConfig } from 'unconfig';
|
|
3
|
+
import { resolve, relative, join, extname } from 'path';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import fg from 'fast-glob';
|
|
6
|
+
import { parse } from '@vue/compiler-dom';
|
|
7
|
+
import { parse as parse$1 } from 'jsonc-parser';
|
|
8
|
+
import { defu } from 'defu';
|
|
9
|
+
|
|
10
|
+
const isPagePath = (path, options) => {
|
|
11
|
+
const dirPath = resolve(process.cwd(), options.pagesDir);
|
|
12
|
+
const filter = createFilter(dirPath + "/**/*.(vue|nvue)");
|
|
13
|
+
return filter(path);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
function definePages(config) {
|
|
17
|
+
return config;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function defineMiddleware(middleware) {
|
|
21
|
+
return middleware;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function resolveOptions(userOptions) {
|
|
25
|
+
return {
|
|
26
|
+
pagesDir: "src/pages",
|
|
27
|
+
outDir: "src",
|
|
28
|
+
entry: "pages.config",
|
|
29
|
+
extension: "ts",
|
|
30
|
+
...userOptions
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
const loadUserPagesConfig = async (options) => {
|
|
34
|
+
const { config } = await loadConfig({
|
|
35
|
+
sources: [
|
|
36
|
+
{
|
|
37
|
+
files: options.entry,
|
|
38
|
+
extensions: [options.extension]
|
|
39
|
+
}
|
|
40
|
+
],
|
|
41
|
+
merge: false
|
|
42
|
+
});
|
|
43
|
+
return config;
|
|
44
|
+
};
|
|
45
|
+
const scanPages = async (options) => {
|
|
46
|
+
const files = await fg("**/*.(vue|nvue)", {
|
|
47
|
+
ignore: ["node_modules", ".git", "**/__*__/*"],
|
|
48
|
+
onlyFiles: true,
|
|
49
|
+
cwd: resolve(process.cwd(), options.pagesDir)
|
|
50
|
+
});
|
|
51
|
+
const basePath = relative(
|
|
52
|
+
resolve(process.cwd(), options.outDir),
|
|
53
|
+
options.pagesDir
|
|
54
|
+
);
|
|
55
|
+
return files.map((file) => {
|
|
56
|
+
return {
|
|
57
|
+
relative: normalizePath(join(basePath, file)),
|
|
58
|
+
absolute: normalizePath(resolve(process.cwd(), options.pagesDir, file))
|
|
59
|
+
};
|
|
60
|
+
});
|
|
61
|
+
};
|
|
62
|
+
const parsePage = async ({
|
|
63
|
+
relative: relative2,
|
|
64
|
+
absolute
|
|
65
|
+
}) => {
|
|
66
|
+
const code = await fs.promises.readFile(absolute, {
|
|
67
|
+
encoding: "utf-8"
|
|
68
|
+
});
|
|
69
|
+
const t = parse(code);
|
|
70
|
+
const path = relative2.replace(extname(relative2), "");
|
|
71
|
+
let pageMeta = {
|
|
72
|
+
path,
|
|
73
|
+
type: "page"
|
|
74
|
+
};
|
|
75
|
+
t?.children.forEach((v) => {
|
|
76
|
+
if (v.type !== 1) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (v.tag === "route") {
|
|
80
|
+
v.props.forEach((prop) => {
|
|
81
|
+
if (prop.type === 6 && prop.name === "type") {
|
|
82
|
+
pageMeta.type = prop.value?.content ?? pageMeta.type;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
const astContent = v.children?.[0];
|
|
86
|
+
if (astContent && astContent.type === 2) {
|
|
87
|
+
const _pageMeta = parse$1(astContent.content);
|
|
88
|
+
Object.keys(_pageMeta).forEach((key) => {
|
|
89
|
+
pageMeta[key] = _pageMeta[key];
|
|
90
|
+
});
|
|
91
|
+
pageMeta["path"] = path;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
return pageMeta;
|
|
96
|
+
};
|
|
97
|
+
const createOrUpdatePagesJSON = async (options, path) => {
|
|
98
|
+
const outputName = "pages.json";
|
|
99
|
+
const resolvedOutput = normalizePath(
|
|
100
|
+
resolve(process.cwd(), options.outDir, outputName)
|
|
101
|
+
);
|
|
102
|
+
if (!fs.existsSync(resolvedOutput)) {
|
|
103
|
+
fs.writeFileSync(resolvedOutput, "{}", {
|
|
104
|
+
encoding: "utf-8"
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const config = await loadUserPagesConfig(options);
|
|
108
|
+
const pagesPathList = await scanPages(options);
|
|
109
|
+
const pagesMeta = await Promise.all(
|
|
110
|
+
pagesPathList.map(async (page) => await parsePage(page))
|
|
111
|
+
);
|
|
112
|
+
const pages = Object.values(defu({}, pagesMeta, config.pages));
|
|
113
|
+
pages.sort((a) => a.type === "home" ? -1 : 0);
|
|
114
|
+
config.pages = pages;
|
|
115
|
+
fs.writeFileSync(resolvedOutput, JSON.stringify(config), {
|
|
116
|
+
encoding: "utf-8"
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
const VitePluginUniPages = async (userOptions = {}) => {
|
|
120
|
+
const options = resolveOptions(userOptions);
|
|
121
|
+
const pagesConfigSource = normalizePath(
|
|
122
|
+
resolve(process.cwd(), `${options.entry}.${options.extension}`)
|
|
123
|
+
);
|
|
124
|
+
await createOrUpdatePagesJSON(options);
|
|
125
|
+
return {
|
|
126
|
+
name: "vite-plugin-uni-pages",
|
|
127
|
+
enforce: "pre",
|
|
128
|
+
configureServer({ watcher }) {
|
|
129
|
+
watcher.add(pagesConfigSource);
|
|
130
|
+
watcher.on("change", async (path) => {
|
|
131
|
+
if (normalizePath(path) === pagesConfigSource || isPagePath(path, options)) {
|
|
132
|
+
await createOrUpdatePagesJSON(options);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
watcher.on("add", async (path) => {
|
|
136
|
+
if (isPagePath(path, options)) {
|
|
137
|
+
await createOrUpdatePagesJSON(options);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
watcher.on("unlink", async (path) => {
|
|
141
|
+
if (isPagePath(path, options)) {
|
|
142
|
+
await createOrUpdatePagesJSON(options);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
export { VitePluginUniPages, VitePluginUniPages as default, defineMiddleware, definePages };
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@uni-helper/vite-plugin-uni-pages",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"packageManager": "pnpm@7.14.0",
|
|
6
|
+
"description": "File system based routing for uni-app applications using Vite",
|
|
7
|
+
"author": "KeJun",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"homepage": "https://github.com/uni-helper/vite-plugin-uni-pages#readme",
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/uni-helper/vite-plugin-uni-pages.git"
|
|
13
|
+
},
|
|
14
|
+
"bugs": "https://github.com/uni-helper/vite-plugin-uni-pages/issues",
|
|
15
|
+
"keywords": [],
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"require": "./dist/index.cjs",
|
|
21
|
+
"import": "./dist/index.mjs"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"main": "./dist/index.mjs",
|
|
25
|
+
"module": "./dist/index.mjs",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"typesVersions": {
|
|
28
|
+
"*": {
|
|
29
|
+
"*": [
|
|
30
|
+
"./dist/*",
|
|
31
|
+
"./dist/index.d.ts"
|
|
32
|
+
]
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
"files": [
|
|
36
|
+
"dist"
|
|
37
|
+
],
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "unbuild",
|
|
40
|
+
"dev": "unbuild --stub",
|
|
41
|
+
"play": "nr -C playground dev:h5",
|
|
42
|
+
"prepublishOnly": "pnpm build",
|
|
43
|
+
"release": "bumpp && npm publish --access public",
|
|
44
|
+
"start": "esno src/index.ts",
|
|
45
|
+
"test": "vitest"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "^18.11.9",
|
|
49
|
+
"bumpp": "^8.2.1",
|
|
50
|
+
"esno": "^0.16.3",
|
|
51
|
+
"typescript": "^4.8.4",
|
|
52
|
+
"unbuild": "^0.9.4",
|
|
53
|
+
"vite": "^3.2.3",
|
|
54
|
+
"vitest": "^0.24.5"
|
|
55
|
+
},
|
|
56
|
+
"dependencies": {
|
|
57
|
+
"@vue/compiler-dom": "^3.2.45",
|
|
58
|
+
"defu": "^6.1.0",
|
|
59
|
+
"fast-glob": "^3.2.12",
|
|
60
|
+
"jsonc-parser": "^3.2.0",
|
|
61
|
+
"unconfig": "^0.3.7"
|
|
62
|
+
}
|
|
63
|
+
}
|