@metajoy-preview/unplugin 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.
@@ -0,0 +1,6 @@
1
+ import { Options } from "./option.mjs";
2
+
3
+ //#region src/rollup.d.ts
4
+ declare const _default: (options?: Options | undefined) => any;
5
+ //#endregion
6
+ export { _default as default };
@@ -0,0 +1,6 @@
1
+ import { n as metajoyUnpluginFactory } from "./src-2HigwrEC.mjs";
2
+ import { createRollupPlugin } from "unplugin";
3
+ //#region src/rollup.ts
4
+ var rollup_default = createRollupPlugin(metajoyUnpluginFactory);
5
+ //#endregion
6
+ export { rollup_default as default };
@@ -0,0 +1,6 @@
1
+ import { Options } from "./option.mjs";
2
+
3
+ //#region src/rspack.d.ts
4
+ declare const _default: (options?: Options | undefined) => RspackPluginInstance;
5
+ //#endregion
6
+ export { _default as default };
@@ -0,0 +1,6 @@
1
+ import { n as metajoyUnpluginFactory } from "./src-2HigwrEC.mjs";
2
+ import { createRspackPlugin } from "unplugin";
3
+ //#region src/rspack.ts
4
+ var rspack_default = createRspackPlugin(metajoyUnpluginFactory);
5
+ //#endregion
6
+ export { rspack_default as default };
@@ -0,0 +1,118 @@
1
+ import { getConfig, loadConfig, useConfig, watchConfig } from "./config.mjs";
2
+ import { normalizeOptions } from "./option.mjs";
3
+ import { config } from "dotenv";
4
+ import { isArray, keys } from "lodash-es";
5
+ import shelljs from "shelljs";
6
+ import { createUnplugin } from "unplugin";
7
+ import { genObjectFromRawEntries } from "knitwork";
8
+ import { createCommonJS, resolveImports, resolvePath } from "mlly";
9
+ import { joinURL } from "ufo";
10
+ //#region src/plugins/api.ts
11
+ const { __filename } = createCommonJS(import.meta.url);
12
+ const moduleId = "virtual:@metajoy/unplugin/api";
13
+ const apiUnplugin = (options) => {
14
+ return {
15
+ name: "metajoy-unplugin-api",
16
+ resolveId(id) {
17
+ if (id === moduleId) return `\0${id}`;
18
+ },
19
+ async load(id) {
20
+ if (id === `\0${moduleId}`) {
21
+ const { baseUrl, overrides } = getConfig("api", { overrides: {
22
+ baseUrl: options.baseUrl ?? process.env.VITE_API_BASE_URL,
23
+ overrides: options.overrides
24
+ } });
25
+ const codes = [];
26
+ codes.push(await resolveImports(`import { joinURL } from "ufo"`, { url: __filename }));
27
+ const targets = new Map([baseUrl ? ["default", `"${baseUrl}"`] : void 0, ...Object.entries(overrides || {}).map(([name, target]) => [name, `"${typeof target === "string" ? target : target.baseUrl}"`])].filter((x) => !!x));
28
+ codes.push(`const targets = ${genObjectFromRawEntries(Array.from(targets.entries()))};`);
29
+ codes.push(`
30
+ export function getBaseUrl(tag = "default", options = { proxy: false }) {
31
+ // 如果要求代理其代理已启用并且在开发环境,则使用代理地址
32
+ if (options.proxy && ${options.proxy} && import.meta.env.DEV) {
33
+ return joinURL(import.meta.env.BASE_URL ?? "", \`/@proxy/\${tag}\`);
34
+ }
35
+ return targets[tag] ?? targets["default"];
36
+ }
37
+ `);
38
+ return codes.join("\n");
39
+ }
40
+ },
41
+ vite: {
42
+ config(config) {
43
+ if (!options.proxy) return;
44
+ const apiConfig = useConfig("api", { overrides: {
45
+ baseUrl: options.baseUrl ?? process.env.VITE_API_BASE_URL,
46
+ overrides: options.overrides
47
+ } });
48
+ config.server ||= {};
49
+ config.server.proxy ||= {};
50
+ config.server.proxy[joinURL(config.base ?? "", "/@proxy")] = {
51
+ changeOrigin: true,
52
+ configure(_, options) {
53
+ options.bypass = async (req) => {
54
+ const targets = new Map([apiConfig.baseUrl ? ["default", apiConfig.baseUrl] : void 0, ...Object.entries(apiConfig.overrides || {}).map(([name, target]) => [name, typeof target === "string" ? target : target.baseUrl])].filter((x) => !!x));
55
+ const tag = req.url?.replace(joinURL(config.base ?? "", "/@proxy"), "").split("/")?.[1] || "default";
56
+ options.target = (isArray(req.headers["x-base-url"]) ? req.headers["x-base-url"][0] : req.headers["x-base-url"]) || targets.get(tag) || targets.get("default");
57
+ };
58
+ },
59
+ rewrite(path) {
60
+ return path.replace(joinURL(config.base ?? "", "/@proxy"), "").split("/").slice(2).join("/");
61
+ }
62
+ };
63
+ },
64
+ async configureServer(server) {
65
+ await updateType();
66
+ async function updateType() {
67
+ const path = await resolvePath("./client", {
68
+ url: __filename,
69
+ extensions: [
70
+ ".d.ts",
71
+ ".d.mts",
72
+ ".d.cts"
73
+ ]
74
+ });
75
+ if (shelljs.test("-f", path)) {
76
+ const apiConfig = getConfig("api", { overrides: {
77
+ baseUrl: options.baseUrl ?? process.env.VITE_API_BASE_URL,
78
+ overrides: options.overrides
79
+ } });
80
+ shelljs.sed("-i", /type Tags = .*?;/g, `type Tags = ${[apiConfig.baseUrl ? "default" : void 0, ...Object.keys(getConfig("api.overrides") || {})].filter(Boolean).map((key) => `"${key}"`).join(" | ") || "never"};`, path);
81
+ }
82
+ }
83
+ async function updateModule() {
84
+ const module = server.moduleGraph.getModuleById(`\0${moduleId}`);
85
+ if (module) await server.reloadModule(module);
86
+ }
87
+ watchConfig("api.baseUrl", updateModule);
88
+ watchConfig("api.overrides", async () => {
89
+ await updateType();
90
+ await updateModule();
91
+ });
92
+ }
93
+ }
94
+ };
95
+ };
96
+ //#endregion
97
+ //#region src/index.ts
98
+ shelljs.config.silent = true;
99
+ let lastParsed;
100
+ const metajoyUnpluginFactory = (options) => {
101
+ const { api } = normalizeOptions(options);
102
+ return [{
103
+ name: "metajoy-unplugin",
104
+ vite: { async config() {
105
+ await loadConfig();
106
+ const { parsed } = config({
107
+ path: [".env", process.env.NODE_ENV ? `.env.${process.env.NODE_ENV}` : void 0].filter((x) => !!x).flatMap((x) => [x, `${x}.local`]),
108
+ override: true,
109
+ quiet: true
110
+ });
111
+ for (const key of keys(lastParsed ?? {}).filter((key) => !(key in (parsed ?? {})))) delete process.env[key];
112
+ lastParsed = parsed;
113
+ } }
114
+ }, api ? apiUnplugin(api, {}) : []].flat();
115
+ };
116
+ const metajoyUnplugin = /* @__PURE__ */ createUnplugin(metajoyUnpluginFactory);
117
+ //#endregion
118
+ export { metajoyUnpluginFactory as n, metajoyUnplugin as t };
@@ -0,0 +1,7 @@
1
+ import { Options } from "./option.mjs";
2
+ import * as _$unplugin from "unplugin";
3
+
4
+ //#region src/vite.d.ts
5
+ declare const _default: (options?: Options | undefined) => _$unplugin.VitePlugin<any> | _$unplugin.VitePlugin<any>[];
6
+ //#endregion
7
+ export { _default as default };
package/dist/vite.mjs ADDED
@@ -0,0 +1,6 @@
1
+ import { n as metajoyUnpluginFactory } from "./src-2HigwrEC.mjs";
2
+ import { createVitePlugin } from "unplugin";
3
+ //#region src/vite.ts
4
+ var vite_default = createVitePlugin(metajoyUnpluginFactory);
5
+ //#endregion
6
+ export { vite_default as default };
@@ -0,0 +1,6 @@
1
+ import { Options } from "./option.mjs";
2
+
3
+ //#region src/webpack.d.ts
4
+ declare const _default: (options?: Options | undefined) => WebpackPluginInstance;
5
+ //#endregion
6
+ export { _default as default };
@@ -0,0 +1,6 @@
1
+ import { n as metajoyUnpluginFactory } from "./src-2HigwrEC.mjs";
2
+ import { createWebpackPlugin } from "unplugin";
3
+ //#region src/webpack.ts
4
+ var webpack_default = createWebpackPlugin(metajoyUnpluginFactory);
5
+ //#endregion
6
+ export { webpack_default as default };
package/package.json ADDED
@@ -0,0 +1,66 @@
1
+ {
2
+ "name": "@metajoy-preview/unplugin",
3
+ "version": "0.0.1",
4
+ "description": "Metajoy 项目的 unplugin",
5
+ "files": [
6
+ "dist"
7
+ ],
8
+ "type": "module",
9
+ "exports": {
10
+ ".": "./dist/index.mjs",
11
+ "./astro": "./dist/astro.mjs",
12
+ "./client": "./dist/client.mjs",
13
+ "./config": "./dist/config.mjs",
14
+ "./esbuild": "./dist/esbuild.mjs",
15
+ "./farm": "./dist/farm.mjs",
16
+ "./nuxt": "./dist/nuxt.mjs",
17
+ "./option": "./dist/option.mjs",
18
+ "./rollup": "./dist/rollup.mjs",
19
+ "./rspack": "./dist/rspack.mjs",
20
+ "./vite": "./dist/vite.mjs",
21
+ "./webpack": "./dist/webpack.mjs",
22
+ "./package.json": "./package.json"
23
+ },
24
+ "publishConfig": {
25
+ "access": "public"
26
+ },
27
+ "dependencies": {
28
+ "dotenv": "^17.4.2",
29
+ "hookable": "^6.1.1",
30
+ "knitwork": "^1.3.0",
31
+ "lodash-es": "^4.18.1",
32
+ "mlly": "^1.8.2",
33
+ "shelljs": "^0.10.0",
34
+ "ufo": "^1.6.4",
35
+ "unplugin": "^3.0.0",
36
+ "@metajoy-preview/config": "0.0.1"
37
+ },
38
+ "devDependencies": {
39
+ "@nuxt/kit": "^4.4.6",
40
+ "@types/lodash-es": "^4.17.12",
41
+ "@types/shelljs": "^0.10.0",
42
+ "type-fest": "^5.6.0"
43
+ },
44
+ "inlinedDependencies": {
45
+ "@nuxt/kit": "4.4.6",
46
+ "confbox": "0.2.4",
47
+ "consola": "3.4.2",
48
+ "defu": "6.1.7",
49
+ "exsolve": "1.0.8",
50
+ "ignore": "7.0.5",
51
+ "jiti": "2.7.0",
52
+ "pathe": "2.0.3",
53
+ "pkg-types": "2.3.1",
54
+ "semver": "7.8.1",
55
+ "tagged-tag": "1.0.0",
56
+ "type-fest": "5.6.0",
57
+ "unctx": "2.5.0",
58
+ "untyped": "2.0.0"
59
+ },
60
+ "scripts": {
61
+ "build": "vp pack",
62
+ "dev": "vp pack --watch",
63
+ "test": "vp test",
64
+ "check": "vp check"
65
+ }
66
+ }