@vizejs/nuxt 0.9.0 → 0.11.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/dist/index.js +102 -2
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import { addVitePlugin, defineNuxtModule } from "@nuxt/kit";
|
|
2
3
|
import vize from "@vizejs/vite-plugin";
|
|
3
4
|
import { musea } from "@vizejs/vite-plugin-musea";
|
|
4
5
|
|
|
@@ -17,7 +18,106 @@ var src_default = defineNuxtModule({
|
|
|
17
18
|
},
|
|
18
19
|
setup(options, nuxt) {
|
|
19
20
|
nuxt.options.vite.plugins = nuxt.options.vite.plugins || [];
|
|
20
|
-
if (options.compiler !== false)
|
|
21
|
+
if (options.compiler !== false) {
|
|
22
|
+
nuxt.options.vite.plugins.push(vize());
|
|
23
|
+
nuxt.hook("vite:configResolved", (config) => {
|
|
24
|
+
for (let i = config.plugins.length - 1; i >= 0; i--) {
|
|
25
|
+
const p = config.plugins[i];
|
|
26
|
+
const name = p && typeof p === "object" && "name" in p ? p.name : "";
|
|
27
|
+
if (name === "vite:vue") config.plugins.splice(i, 1);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
let unimportCtx = null;
|
|
32
|
+
nuxt.hook("imports:context", (ctx) => {
|
|
33
|
+
unimportCtx = ctx;
|
|
34
|
+
});
|
|
35
|
+
let nuxtComponents = [];
|
|
36
|
+
nuxt.hook("components:extend", (comps) => {
|
|
37
|
+
nuxtComponents = comps;
|
|
38
|
+
});
|
|
39
|
+
addVitePlugin({
|
|
40
|
+
name: "vizejs:nuxt-transform-bridge",
|
|
41
|
+
enforce: "post",
|
|
42
|
+
async transform(code, id) {
|
|
43
|
+
if (!id.startsWith("\0") || !id.endsWith(".vue.ts")) return;
|
|
44
|
+
let result = code;
|
|
45
|
+
let changed = false;
|
|
46
|
+
if (nuxtComponents.length > 0) {
|
|
47
|
+
const compImports = [];
|
|
48
|
+
let counter = 0;
|
|
49
|
+
result = result.replace(/_?resolveComponent\s*\(\s*["'`]([^"'`]+)["'`]\s*(?:,\s*[^)]+)?\)/g, (match, name) => {
|
|
50
|
+
const comp = nuxtComponents.find((c) => c.pascalName === name || c.kebabName === name || c.name === name);
|
|
51
|
+
if (comp) {
|
|
52
|
+
const varName = `__nuxt_component_${counter++}`;
|
|
53
|
+
const exportName = comp.export || "default";
|
|
54
|
+
if (exportName === "default") compImports.push(`import ${varName} from ${JSON.stringify(comp.filePath)};`);
|
|
55
|
+
else compImports.push(`import { ${exportName} as ${varName} } from ${JSON.stringify(comp.filePath)};`);
|
|
56
|
+
return varName;
|
|
57
|
+
}
|
|
58
|
+
return match;
|
|
59
|
+
});
|
|
60
|
+
if (compImports.length > 0) {
|
|
61
|
+
result = compImports.join("\n") + "\n" + result;
|
|
62
|
+
changed = true;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const i18nFnRe = /(?<![.\w])\$([tdn]|rt|tm|te)\s*\(/;
|
|
66
|
+
if (i18nFnRe.test(result) && !result.includes("useI18n")) {
|
|
67
|
+
const i18nMap = {
|
|
68
|
+
$t: "t: $t",
|
|
69
|
+
$rt: "rt: $rt",
|
|
70
|
+
$d: "d: $d",
|
|
71
|
+
$n: "n: $n",
|
|
72
|
+
$tm: "tm: $tm",
|
|
73
|
+
$te: "te: $te"
|
|
74
|
+
};
|
|
75
|
+
const usedFns = [];
|
|
76
|
+
for (const [fn, destructure] of Object.entries(i18nMap)) if (new RegExp(`(?<![.\\w])\\${fn}\\s*\\(`).test(result)) usedFns.push(destructure);
|
|
77
|
+
if (usedFns.length > 0) {
|
|
78
|
+
const setupMatch = result.match(/setup\s*\(__props[\s\S]*?\)\s*\{/);
|
|
79
|
+
if (setupMatch && setupMatch.index !== void 0) {
|
|
80
|
+
const insertPos = setupMatch.index + setupMatch[0].length;
|
|
81
|
+
const injection = `\nconst { ${usedFns.join(", ")} } = useI18n();\n`;
|
|
82
|
+
result = result.slice(0, insertPos) + injection + result.slice(insertPos);
|
|
83
|
+
changed = true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (unimportCtx) try {
|
|
88
|
+
const injected = await unimportCtx.injectImports(result, id);
|
|
89
|
+
if (injected.imports && injected.imports.length > 0) {
|
|
90
|
+
result = injected.code;
|
|
91
|
+
changed = true;
|
|
92
|
+
}
|
|
93
|
+
} catch {}
|
|
94
|
+
if (changed) return {
|
|
95
|
+
code: result,
|
|
96
|
+
map: null
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
addVitePlugin({
|
|
101
|
+
name: "vizejs:unocss-bridge",
|
|
102
|
+
configResolved(config) {
|
|
103
|
+
for (const plugin of config.plugins) if (plugin.name?.startsWith("unocss:") && typeof plugin.transform === "function") {
|
|
104
|
+
const origTransform = plugin.transform;
|
|
105
|
+
const isExtractionOnly = plugin.name.startsWith("unocss:global");
|
|
106
|
+
plugin.transform = function(code, id, ...args) {
|
|
107
|
+
if (id.startsWith("\0") && id.endsWith(".vue.ts")) {
|
|
108
|
+
const normalizedId = id.slice(1).replace(/\.ts$/, "");
|
|
109
|
+
let effectiveCode = code;
|
|
110
|
+
if (isExtractionOnly) try {
|
|
111
|
+
const originalSource = fs.readFileSync(normalizedId, "utf-8");
|
|
112
|
+
effectiveCode = code + "\n" + originalSource;
|
|
113
|
+
} catch {}
|
|
114
|
+
return origTransform.call(this, effectiveCode, normalizedId, ...args);
|
|
115
|
+
}
|
|
116
|
+
return origTransform.call(this, code, id, ...args);
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
});
|
|
21
121
|
if (options.musea !== false) {
|
|
22
122
|
const museaBasePath = options.musea && typeof options.musea === "object" && "basePath" in options.musea ? options.musea.basePath : "/__musea__";
|
|
23
123
|
nuxt.options.vite.plugins.push(...musea(options.musea || {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vizejs/nuxt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.11.0",
|
|
4
4
|
"description": "Nuxt module for Vize - compiler, musea gallery, linter, and type checker",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -34,10 +34,10 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@nuxt/kit": "^4.0.0",
|
|
37
|
-
"@vizejs/vite-plugin
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"@vizejs/musea
|
|
37
|
+
"@vizejs/vite-plugin": "0.11.0",
|
|
38
|
+
"@vizejs/musea-nuxt": "0.11.0",
|
|
39
|
+
"vize": "0.11.0",
|
|
40
|
+
"@vizejs/vite-plugin-musea": "0.11.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"tsdown": "^0.9.0",
|