vite-plugin-uni-inject 0.1.0 → 0.2.0
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/README.md +1 -1
- package/dist/index.cjs +101 -6
- package/dist/index.d.cts +14 -14
- package/dist/index.d.mts +20 -0
- package/dist/index.mjs +76 -0
- package/package.json +10 -9
- package/dist/index.d.ts +0 -19
- package/dist/index.js +0 -6
package/README.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,101 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
//#endregion
|
|
23
|
+
let _babel_parser = require("@babel/parser");
|
|
24
|
+
let _vue_compiler_sfc = require("@vue/compiler-sfc");
|
|
25
|
+
let fs = require("fs");
|
|
26
|
+
fs = __toESM(fs);
|
|
27
|
+
let path = require("path");
|
|
28
|
+
path = __toESM(path);
|
|
29
|
+
let magic_string = require("magic-string");
|
|
30
|
+
magic_string = __toESM(magic_string);
|
|
31
|
+
//#region src/index.ts
|
|
32
|
+
function src_default(opts) {
|
|
33
|
+
const { injectPath = "App.inject.vue" } = opts || {};
|
|
34
|
+
const pageSet = /* @__PURE__ */ new Set();
|
|
35
|
+
let injectTemplate = "";
|
|
36
|
+
let injectScriptSetup = "";
|
|
37
|
+
return {
|
|
38
|
+
name: "vite-plugin-uni-inject",
|
|
39
|
+
enforce: "pre",
|
|
40
|
+
configResolved(config) {
|
|
41
|
+
const srcRoot = path.default.resolve(config.root, "src");
|
|
42
|
+
const injPath = path.default.join(srcRoot, injectPath);
|
|
43
|
+
if (!fs.default.existsSync(injPath)) return;
|
|
44
|
+
const pagesJsonPath = path.default.join(srcRoot, "pages.json");
|
|
45
|
+
if (!fs.default.existsSync(pagesJsonPath)) return;
|
|
46
|
+
const { descriptor } = (0, _vue_compiler_sfc.parse)(fs.default.readFileSync(injPath, "utf-8"));
|
|
47
|
+
if (descriptor.template) injectTemplate = descriptor.template.content.trim();
|
|
48
|
+
if (descriptor.scriptSetup) injectScriptSetup = descriptor.scriptSetup.content.trim();
|
|
49
|
+
const pagesJsonContent = fs.default.readFileSync(pagesJsonPath, "utf-8");
|
|
50
|
+
const pagesJson = JSON.parse(pagesJsonContent);
|
|
51
|
+
const subPackages = pagesJson.subPackages ?? [];
|
|
52
|
+
const collectPages = (pages, rootDir = "") => {
|
|
53
|
+
pages.forEach((page) => {
|
|
54
|
+
const pageVuePath = path.default.join(srcRoot, rootDir, `${page.path}.vue`);
|
|
55
|
+
pageSet.add(path.default.normalize(pageVuePath));
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
collectPages(pagesJson.pages);
|
|
59
|
+
subPackages.forEach((sub) => {
|
|
60
|
+
collectPages(sub.pages, sub.root);
|
|
61
|
+
});
|
|
62
|
+
},
|
|
63
|
+
transform(code, id) {
|
|
64
|
+
if (!id.endsWith(".vue")) return;
|
|
65
|
+
const normalizedId = path.default.normalize(id);
|
|
66
|
+
if (!pageSet.has(normalizedId)) return;
|
|
67
|
+
let newCode = code;
|
|
68
|
+
const { descriptor } = (0, _vue_compiler_sfc.parse)(code);
|
|
69
|
+
if (injectTemplate && descriptor.template) {
|
|
70
|
+
const tplContent = descriptor.template.content.trim();
|
|
71
|
+
const injectedTemplate = `${injectTemplate}\n${tplContent}`.trim();
|
|
72
|
+
newCode = newCode.slice(0, descriptor.template.loc.start.offset) + `\n${injectedTemplate}\n` + newCode.slice(descriptor.template.loc.end.offset);
|
|
73
|
+
}
|
|
74
|
+
if (injectScriptSetup && descriptor.scriptSetup) {
|
|
75
|
+
const start = descriptor.scriptSetup.loc.start.offset;
|
|
76
|
+
newCode = newCode.slice(0, start) + `\n${injectScriptSetup}` + newCode.slice(start);
|
|
77
|
+
}
|
|
78
|
+
const { descriptor: newDescriptor } = (0, _vue_compiler_sfc.parse)(newCode);
|
|
79
|
+
if (newDescriptor.scriptSetup) {
|
|
80
|
+
const start = newDescriptor.scriptSetup.loc.start.offset;
|
|
81
|
+
const end = newDescriptor.scriptSetup.loc.end.offset;
|
|
82
|
+
const code = newDescriptor.scriptSetup.content;
|
|
83
|
+
const s = new magic_string.default(code);
|
|
84
|
+
const ast = (0, _babel_parser.parse)(code, {
|
|
85
|
+
sourceType: "module",
|
|
86
|
+
plugins: ["typescript"]
|
|
87
|
+
});
|
|
88
|
+
const imports = [];
|
|
89
|
+
for (const node of ast.program.body) if (node.type === "ImportDeclaration") {
|
|
90
|
+
imports.push(code.slice(node.start, node.end));
|
|
91
|
+
s.remove(node.start, node.end + 1);
|
|
92
|
+
}
|
|
93
|
+
if (imports.length) s.prepend("\n" + imports.join("\n"));
|
|
94
|
+
newCode = newCode.slice(0, start) + s.toString() + newCode.slice(end);
|
|
95
|
+
}
|
|
96
|
+
return { code: newCode };
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
//#endregion
|
|
101
|
+
module.exports = src_default;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
1
2
|
/** 插件配置 */
|
|
2
3
|
interface PluginOptions {
|
|
3
|
-
|
|
4
|
-
|
|
4
|
+
/** 要注入的文件路径 - 基于项目 src 目录 */
|
|
5
|
+
injectPath?: string;
|
|
5
6
|
}
|
|
6
|
-
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/index.d.ts
|
|
7
9
|
declare function export_default(opts?: PluginOptions): {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
} | undefined;
|
|
10
|
+
name: string;
|
|
11
|
+
enforce: "pre";
|
|
12
|
+
configResolved(config: {
|
|
13
|
+
root: string;
|
|
14
|
+
}): void;
|
|
15
|
+
transform(code: string, id: string): {
|
|
16
|
+
code: string;
|
|
17
|
+
} | undefined;
|
|
17
18
|
};
|
|
18
|
-
|
|
19
|
-
export { export_default as default };
|
|
19
|
+
export = export_default;
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/** 插件配置 */
|
|
3
|
+
interface PluginOptions {
|
|
4
|
+
/** 要注入的文件路径 - 基于项目 src 目录 */
|
|
5
|
+
injectPath?: string;
|
|
6
|
+
}
|
|
7
|
+
//#endregion
|
|
8
|
+
//#region src/index.d.ts
|
|
9
|
+
declare function export_default(opts?: PluginOptions): {
|
|
10
|
+
name: string;
|
|
11
|
+
enforce: "pre";
|
|
12
|
+
configResolved(config: {
|
|
13
|
+
root: string;
|
|
14
|
+
}): void;
|
|
15
|
+
transform(code: string, id: string): {
|
|
16
|
+
code: string;
|
|
17
|
+
} | undefined;
|
|
18
|
+
};
|
|
19
|
+
//#endregion
|
|
20
|
+
export { export_default as default };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { parse } from "@babel/parser";
|
|
2
|
+
import { parse as parse$1 } from "@vue/compiler-sfc";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import path from "path";
|
|
5
|
+
import MagicString from "magic-string";
|
|
6
|
+
//#region src/index.ts
|
|
7
|
+
function src_default(opts) {
|
|
8
|
+
const { injectPath = "App.inject.vue" } = opts || {};
|
|
9
|
+
const pageSet = /* @__PURE__ */ new Set();
|
|
10
|
+
let injectTemplate = "";
|
|
11
|
+
let injectScriptSetup = "";
|
|
12
|
+
return {
|
|
13
|
+
name: "vite-plugin-uni-inject",
|
|
14
|
+
enforce: "pre",
|
|
15
|
+
configResolved(config) {
|
|
16
|
+
const srcRoot = path.resolve(config.root, "src");
|
|
17
|
+
const injPath = path.join(srcRoot, injectPath);
|
|
18
|
+
if (!fs.existsSync(injPath)) return;
|
|
19
|
+
const pagesJsonPath = path.join(srcRoot, "pages.json");
|
|
20
|
+
if (!fs.existsSync(pagesJsonPath)) return;
|
|
21
|
+
const { descriptor } = parse$1(fs.readFileSync(injPath, "utf-8"));
|
|
22
|
+
if (descriptor.template) injectTemplate = descriptor.template.content.trim();
|
|
23
|
+
if (descriptor.scriptSetup) injectScriptSetup = descriptor.scriptSetup.content.trim();
|
|
24
|
+
const pagesJsonContent = fs.readFileSync(pagesJsonPath, "utf-8");
|
|
25
|
+
const pagesJson = JSON.parse(pagesJsonContent);
|
|
26
|
+
const subPackages = pagesJson.subPackages ?? [];
|
|
27
|
+
const collectPages = (pages, rootDir = "") => {
|
|
28
|
+
pages.forEach((page) => {
|
|
29
|
+
const pageVuePath = path.join(srcRoot, rootDir, `${page.path}.vue`);
|
|
30
|
+
pageSet.add(path.normalize(pageVuePath));
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
collectPages(pagesJson.pages);
|
|
34
|
+
subPackages.forEach((sub) => {
|
|
35
|
+
collectPages(sub.pages, sub.root);
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
transform(code, id) {
|
|
39
|
+
if (!id.endsWith(".vue")) return;
|
|
40
|
+
const normalizedId = path.normalize(id);
|
|
41
|
+
if (!pageSet.has(normalizedId)) return;
|
|
42
|
+
let newCode = code;
|
|
43
|
+
const { descriptor } = parse$1(code);
|
|
44
|
+
if (injectTemplate && descriptor.template) {
|
|
45
|
+
const tplContent = descriptor.template.content.trim();
|
|
46
|
+
const injectedTemplate = `${injectTemplate}\n${tplContent}`.trim();
|
|
47
|
+
newCode = newCode.slice(0, descriptor.template.loc.start.offset) + `\n${injectedTemplate}\n` + newCode.slice(descriptor.template.loc.end.offset);
|
|
48
|
+
}
|
|
49
|
+
if (injectScriptSetup && descriptor.scriptSetup) {
|
|
50
|
+
const start = descriptor.scriptSetup.loc.start.offset;
|
|
51
|
+
newCode = newCode.slice(0, start) + `\n${injectScriptSetup}` + newCode.slice(start);
|
|
52
|
+
}
|
|
53
|
+
const { descriptor: newDescriptor } = parse$1(newCode);
|
|
54
|
+
if (newDescriptor.scriptSetup) {
|
|
55
|
+
const start = newDescriptor.scriptSetup.loc.start.offset;
|
|
56
|
+
const end = newDescriptor.scriptSetup.loc.end.offset;
|
|
57
|
+
const code = newDescriptor.scriptSetup.content;
|
|
58
|
+
const s = new MagicString(code);
|
|
59
|
+
const ast = parse(code, {
|
|
60
|
+
sourceType: "module",
|
|
61
|
+
plugins: ["typescript"]
|
|
62
|
+
});
|
|
63
|
+
const imports = [];
|
|
64
|
+
for (const node of ast.program.body) if (node.type === "ImportDeclaration") {
|
|
65
|
+
imports.push(code.slice(node.start, node.end));
|
|
66
|
+
s.remove(node.start, node.end + 1);
|
|
67
|
+
}
|
|
68
|
+
if (imports.length) s.prepend("\n" + imports.join("\n"));
|
|
69
|
+
newCode = newCode.slice(0, start) + s.toString() + newCode.slice(end);
|
|
70
|
+
}
|
|
71
|
+
return { code: newCode };
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
//#endregion
|
|
76
|
+
export { src_default as default };
|
package/package.json
CHANGED
|
@@ -1,39 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-uni-inject",
|
|
3
|
-
"type": "module",
|
|
4
|
-
"version": "0.1.0",
|
|
5
3
|
"description": "vite-plugin-uni-inject",
|
|
6
4
|
"author": "Kriac",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"version": "0.2.0",
|
|
7
8
|
"homepage": "https://github.com/Kriac/vite-plugin-uni-inject",
|
|
8
9
|
"repository": {
|
|
9
10
|
"type": "git",
|
|
10
11
|
"url": "git+https://github.com/Kriac/vite-plugin-uni-inject.git"
|
|
11
12
|
},
|
|
12
13
|
"keywords": [
|
|
13
|
-
"vite-plugin"
|
|
14
|
-
"uni-inject"
|
|
14
|
+
"vite-plugin-uni-inject"
|
|
15
15
|
],
|
|
16
16
|
"scripts": {
|
|
17
17
|
"lint": "vue-tsc -b --noEmit",
|
|
18
|
-
"build": "
|
|
18
|
+
"build": "tsdown",
|
|
19
19
|
"release": "npm publish --access public"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@vue/compiler-sfc": "3.5.27"
|
|
22
|
+
"@vue/compiler-sfc": "3.5.27",
|
|
23
|
+
"@babel/parser": "7.29.0",
|
|
24
|
+
"magic-string": "0.30.21"
|
|
23
25
|
},
|
|
24
26
|
"devDependencies": {
|
|
25
27
|
"@types/node": "25.0.10",
|
|
26
|
-
"
|
|
28
|
+
"tsdown": "0.21.0",
|
|
27
29
|
"vue-tsc": "3.2.1"
|
|
28
30
|
},
|
|
29
|
-
"license": "MIT",
|
|
30
31
|
"files": [
|
|
31
32
|
"dist"
|
|
32
33
|
],
|
|
33
34
|
"exports": {
|
|
34
35
|
".": {
|
|
35
36
|
"types": "./dist/index.d.ts",
|
|
36
|
-
"import": "./dist/index.
|
|
37
|
+
"import": "./dist/index.mjs",
|
|
37
38
|
"require": "./dist/index.cjs"
|
|
38
39
|
}
|
|
39
40
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/** 插件配置 */
|
|
2
|
-
interface PluginOptions {
|
|
3
|
-
/** 要注入的文件路径 - 基于项目 src 目录 */
|
|
4
|
-
injectPath?: string;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
declare function export_default(opts?: PluginOptions): {
|
|
8
|
-
name: string;
|
|
9
|
-
enforce: "pre";
|
|
10
|
-
configResolved(config: {
|
|
11
|
-
root: string;
|
|
12
|
-
}): void;
|
|
13
|
-
transform(code: string, id: string): {
|
|
14
|
-
code: string;
|
|
15
|
-
map: null;
|
|
16
|
-
} | undefined;
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
export { export_default as default };
|
package/dist/index.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import{parse as d}from"@vue/compiler-sfc";import a from"fs";import o from"path";function k(P){let{injectPath:S="App.inject.vue"}=P||{},g=new Set,p="",l="";return{name:"vite-plugin-uni-inject",enforce:"pre",configResolved(i){let s=o.resolve(i.root,"src"),r=o.join(s,S);if(!a.existsSync(r))return;let e=a.readFileSync(r,"utf-8"),{descriptor:t}=d(e);t.template&&(p=t.template.content.trim()),t.scriptSetup&&(l=t.scriptSetup.content.trim());let n=o.join(s,"pages.json");if(!a.existsSync(n))return;let f=a.readFileSync(n,"utf-8"),m=JSON.parse(f),u=(c,j="")=>{c.forEach(h=>{let v=o.join(s,j,`${h.path}.vue`);g.add(o.normalize(v))})};u(m.pages),(m.subPackages??[]).forEach(c=>{u(c.pages,c.root)})},transform(i,s){if(!s.endsWith(".vue"))return;let r=o.normalize(s);if(!g.has(r))return;let{descriptor:e}=d(i),t=i;if(p&&e.template){let n=e.template.content.trim(),f=`${p}
|
|
2
|
-
${n}`.trim();t=t.slice(0,e.template.loc.start.offset)+`
|
|
3
|
-
${f}
|
|
4
|
-
`+t.slice(e.template.loc.end.offset)}if(l&&e.scriptSetup){let n=e.scriptSetup.loc.start.offset;t=t.slice(0,n)+`
|
|
5
|
-
${l}
|
|
6
|
-
`+t.slice(n)}return{code:t,map:null}}}}export{k as default};
|