@vizejs/vite-plugin 0.29.0 → 0.31.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 +52 -31
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -187,6 +187,7 @@ function generateOutput(compiled, options) {
|
|
|
187
187
|
const exportDefaultRegex = /^export default /m;
|
|
188
188
|
const hasExportDefault = exportDefaultRegex.test(output);
|
|
189
189
|
const hasNamedRenderExport = /^export function render\b/m.test(output);
|
|
190
|
+
const hasNamedSsrRenderExport = /^export function ssrRender\b/m.test(output);
|
|
190
191
|
const hasSfcMainDefined = /\bconst\s+_sfc_main\s*=/.test(output);
|
|
191
192
|
if (hasExportDefault && !hasSfcMainDefined) {
|
|
192
193
|
output = output.replace(exportDefaultRegex, "const _sfc_main = ");
|
|
@@ -199,6 +200,11 @@ function generateOutput(compiled, options) {
|
|
|
199
200
|
if (compiled.hasScoped && compiled.scopeId) output += `\n_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`;
|
|
200
201
|
output += "\n_sfc_main.render = render;";
|
|
201
202
|
output += "\nexport default _sfc_main;";
|
|
203
|
+
} else if (!hasExportDefault && !hasSfcMainDefined && hasNamedSsrRenderExport) {
|
|
204
|
+
output += "\nconst _sfc_main = {};";
|
|
205
|
+
if (compiled.hasScoped && compiled.scopeId) output += `\n_sfc_main.__scopeId = "data-v-${compiled.scopeId}";`;
|
|
206
|
+
output += "\n_sfc_main.ssrRender = ssrRender;";
|
|
207
|
+
output += "\nexport default _sfc_main;";
|
|
202
208
|
}
|
|
203
209
|
const useDelegatedStyles = hasDelegatedStyles(compiled) && filePath;
|
|
204
210
|
if (useDelegatedStyles) {
|
|
@@ -267,19 +273,25 @@ ${output}`;
|
|
|
267
273
|
//#endregion
|
|
268
274
|
//#region src/virtual.ts
|
|
269
275
|
const LEGACY_VIZE_PREFIX = "\0vize:";
|
|
276
|
+
const VIZE_SSR_PREFIX = "\0vize-ssr:";
|
|
270
277
|
const VIRTUAL_CSS_MODULE = "virtual:vize-styles";
|
|
271
278
|
const RESOLVED_CSS_MODULE = "\0vize:all-styles.css";
|
|
272
279
|
/** Check if a module ID is a vize-compiled virtual module */
|
|
273
280
|
function isVizeVirtual(id) {
|
|
274
|
-
|
|
281
|
+
const pathPart = id.startsWith(VIZE_SSR_PREFIX) ? id.slice(VIZE_SSR_PREFIX.length) : id.slice(1);
|
|
282
|
+
return id.startsWith("\0") && pathPart.endsWith(".vue.ts");
|
|
283
|
+
}
|
|
284
|
+
function isVizeSsrVirtual(id) {
|
|
285
|
+
return id.startsWith(VIZE_SSR_PREFIX);
|
|
275
286
|
}
|
|
276
287
|
/** Create a virtual module ID from a real .vue file path */
|
|
277
|
-
function toVirtualId(realPath) {
|
|
278
|
-
return "\0" + realPath + ".ts";
|
|
288
|
+
function toVirtualId(realPath, ssr = false) {
|
|
289
|
+
return ssr ? `${VIZE_SSR_PREFIX}${realPath}.ts` : "\0" + realPath + ".ts";
|
|
279
290
|
}
|
|
280
291
|
/** Extract the real .vue file path from a virtual module ID */
|
|
281
292
|
function fromVirtualId(virtualId) {
|
|
282
|
-
|
|
293
|
+
const prefix = isVizeSsrVirtual(virtualId) ? VIZE_SSR_PREFIX.length : 1;
|
|
294
|
+
return virtualId.slice(prefix, -3);
|
|
283
295
|
}
|
|
284
296
|
function escapeRegExp(value) {
|
|
285
297
|
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
@@ -586,6 +598,16 @@ function diffPrecompileFiles(files, currentMetadata, previousMetadata) {
|
|
|
586
598
|
deletedFiles
|
|
587
599
|
};
|
|
588
600
|
}
|
|
601
|
+
function getEnvironmentCache(state, ssr) {
|
|
602
|
+
return ssr ? state.ssrCache : state.cache;
|
|
603
|
+
}
|
|
604
|
+
function getCompileOptionsForRequest(state, ssr) {
|
|
605
|
+
return {
|
|
606
|
+
sourceMap: state.mergedOptions?.sourceMap ?? !state.isProduction,
|
|
607
|
+
ssr,
|
|
608
|
+
vapor: !ssr && (state.mergedOptions?.vapor ?? false)
|
|
609
|
+
};
|
|
610
|
+
}
|
|
589
611
|
/**
|
|
590
612
|
* Pre-compile all Vue files matching scan patterns.
|
|
591
613
|
*/
|
|
@@ -610,6 +632,7 @@ async function compileAll(state) {
|
|
|
610
632
|
const cachedFileCount = files.length - changedFiles.length;
|
|
611
633
|
for (const file of deletedFiles) {
|
|
612
634
|
state.cache.delete(file);
|
|
635
|
+
state.ssrCache.delete(file);
|
|
613
636
|
state.collectedCss.delete(file);
|
|
614
637
|
state.precompileMetadata.delete(file);
|
|
615
638
|
state.pendingHmrUpdateTypes.delete(file);
|
|
@@ -631,7 +654,7 @@ async function compileAll(state) {
|
|
|
631
654
|
state.logger.error(`Failed to read ${file}:`, e);
|
|
632
655
|
}
|
|
633
656
|
const result = compileBatch(fileContents, state.cache, {
|
|
634
|
-
ssr:
|
|
657
|
+
ssr: false,
|
|
635
658
|
vapor: state.mergedOptions.vapor ?? false
|
|
636
659
|
});
|
|
637
660
|
for (const file of changedFiles) {
|
|
@@ -693,10 +716,14 @@ function resolveBareImportWithNode(state, id, importer) {
|
|
|
693
716
|
}
|
|
694
717
|
return null;
|
|
695
718
|
}
|
|
696
|
-
async function resolveIdHook(ctx, state, id, importer) {
|
|
719
|
+
async function resolveIdHook(ctx, state, id, importer, options) {
|
|
697
720
|
const isBuild = state.server === null;
|
|
721
|
+
const isSsrRequest = !!options?.ssr || (importer ? isVizeSsrVirtual(importer) : false);
|
|
698
722
|
if (id.startsWith("\0")) {
|
|
699
|
-
if (isVizeVirtual(id))
|
|
723
|
+
if (isVizeVirtual(id)) {
|
|
724
|
+
if (isSsrRequest && !isVizeSsrVirtual(id)) return toVirtualId(fromVirtualId(id), true);
|
|
725
|
+
return null;
|
|
726
|
+
}
|
|
700
727
|
if (id.startsWith(LEGACY_VIZE_PREFIX)) {
|
|
701
728
|
const rawPath = id.slice(LEGACY_VIZE_PREFIX.length);
|
|
702
729
|
const cleanPath$1 = rawPath.endsWith(".ts") ? rawPath.slice(0, -3) : rawPath;
|
|
@@ -837,7 +864,7 @@ async function resolveIdHook(ctx, state, id, importer) {
|
|
|
837
864
|
const hasCache = state.cache.has(resolved);
|
|
838
865
|
const fileExists = fs.existsSync(resolved);
|
|
839
866
|
state.logger.log(`resolveId: id=${id}, resolved=${resolved}, hasCache=${hasCache}, fileExists=${fileExists}, importer=${importer ?? "none"}`);
|
|
840
|
-
if (hasCache || fileExists) return toVirtualId(resolved);
|
|
867
|
+
if (hasCache || fileExists) return toVirtualId(resolved, isSsrRequest);
|
|
841
868
|
if (!fileExists && !path.isAbsolute(id)) {
|
|
842
869
|
const viteResolved = await ctx.resolve(id, importer, { skipSelf: true });
|
|
843
870
|
if (viteResolved && viteResolved.id.endsWith(".vue")) {
|
|
@@ -845,7 +872,7 @@ async function resolveIdHook(ctx, state, id, importer) {
|
|
|
845
872
|
const isResolvedNodeModules = realPath.includes("node_modules");
|
|
846
873
|
if ((isResolvedNodeModules ? handleNodeModules : state.filter(realPath)) && (state.cache.has(realPath) || fs.existsSync(realPath))) {
|
|
847
874
|
state.logger.log(`resolveId: resolved via Vite fallback ${id} to ${realPath}`);
|
|
848
|
-
return toVirtualId(realPath);
|
|
875
|
+
return toVirtualId(realPath, isSsrRequest);
|
|
849
876
|
}
|
|
850
877
|
}
|
|
851
878
|
}
|
|
@@ -890,9 +917,10 @@ function loadHook(state, id, loadOptions) {
|
|
|
890
917
|
const _hasModule = params.has("module");
|
|
891
918
|
const scoped = params.get("scoped");
|
|
892
919
|
const compiled = state.cache.get(realPath);
|
|
920
|
+
const fallbackCompiled = compiled ?? state.ssrCache.get(realPath);
|
|
893
921
|
const blockIndex = indexStr !== null ? parseInt(indexStr, 10) : -1;
|
|
894
|
-
if (
|
|
895
|
-
const block =
|
|
922
|
+
if (fallbackCompiled?.styles && blockIndex >= 0 && blockIndex < fallbackCompiled.styles.length) {
|
|
923
|
+
const block = fallbackCompiled.styles[blockIndex];
|
|
896
924
|
let styleContent = block.content;
|
|
897
925
|
if (scoped && block.scoped && lang && lang !== "css") {
|
|
898
926
|
const lines = styleContent.split("\n");
|
|
@@ -912,7 +940,7 @@ function loadHook(state, id, loadOptions) {
|
|
|
912
940
|
map: null
|
|
913
941
|
};
|
|
914
942
|
}
|
|
915
|
-
if (
|
|
943
|
+
if (fallbackCompiled?.css) return resolveCssImports(fallbackCompiled.css, realPath, state.cssAliasRules, state.server !== null, currentBase);
|
|
916
944
|
return "";
|
|
917
945
|
}
|
|
918
946
|
if (id.startsWith("\0") && id.endsWith("?macro=true")) {
|
|
@@ -935,6 +963,7 @@ function loadHook(state, id, loadOptions) {
|
|
|
935
963
|
}
|
|
936
964
|
if (isVizeVirtual(id)) {
|
|
937
965
|
const realPath = fromVirtualId(id);
|
|
966
|
+
const isSsr = isVizeSsrVirtual(id) || !!loadOptions?.ssr;
|
|
938
967
|
if (!realPath.endsWith(".vue")) {
|
|
939
968
|
state.logger.log(`load: skipping non-vue virtual module ${realPath}`);
|
|
940
969
|
return null;
|
|
@@ -947,14 +976,11 @@ function loadHook(state, id, loadOptions) {
|
|
|
947
976
|
map: null
|
|
948
977
|
};
|
|
949
978
|
}
|
|
950
|
-
|
|
979
|
+
const cache = getEnvironmentCache(state, isSsr);
|
|
980
|
+
let compiled = cache.get(realPath);
|
|
951
981
|
if (!compiled && fs.existsSync(realPath)) {
|
|
952
982
|
state.logger.log(`load: on-demand compiling ${realPath}`);
|
|
953
|
-
compiled = compileFile(realPath,
|
|
954
|
-
sourceMap: state.mergedOptions?.sourceMap ?? !(state.isProduction ?? false),
|
|
955
|
-
ssr: state.mergedOptions?.ssr ?? false,
|
|
956
|
-
vapor: state.mergedOptions?.vapor ?? false
|
|
957
|
-
});
|
|
983
|
+
compiled = compileFile(realPath, cache, getCompileOptionsForRequest(state, isSsr));
|
|
958
984
|
}
|
|
959
985
|
if (compiled) {
|
|
960
986
|
const hasDelegated = hasDelegatedStyles(compiled);
|
|
@@ -965,7 +991,7 @@ function loadHook(state, id, loadOptions) {
|
|
|
965
991
|
};
|
|
966
992
|
const output = rewriteStaticAssetUrls(rewriteDynamicTemplateImports(generateOutput(compiled, {
|
|
967
993
|
isProduction: state.isProduction,
|
|
968
|
-
isDev: state.server !== null,
|
|
994
|
+
isDev: state.server !== null && !isSsr,
|
|
969
995
|
hmrUpdateType: pendingHmrUpdateType,
|
|
970
996
|
extractCss: state.extractCss,
|
|
971
997
|
filePath: realPath
|
|
@@ -1025,11 +1051,8 @@ async function handleHotUpdateHook(state, ctx) {
|
|
|
1025
1051
|
if (file.endsWith(".vue") && state.filter(file)) try {
|
|
1026
1052
|
const source = await read();
|
|
1027
1053
|
const prevCompiled = state.cache.get(file);
|
|
1028
|
-
compileFile(file, state.cache,
|
|
1029
|
-
|
|
1030
|
-
ssr: state.mergedOptions?.ssr ?? false,
|
|
1031
|
-
vapor: state.mergedOptions?.vapor ?? false
|
|
1032
|
-
}, source);
|
|
1054
|
+
compileFile(file, state.cache, getCompileOptionsForRequest(state, false), source);
|
|
1055
|
+
state.ssrCache.delete(file);
|
|
1033
1056
|
const newCompiled = state.cache.get(file);
|
|
1034
1057
|
try {
|
|
1035
1058
|
const stat = fs.statSync(file);
|
|
@@ -1139,11 +1162,8 @@ function createPostTransformPlugin(state) {
|
|
|
1139
1162
|
if (!id.endsWith(".vue") && !id.endsWith(".vue.ts") && !id.includes("node_modules") && id.endsWith(".setup.ts") && /<script\s+setup[\s>]/.test(code)) {
|
|
1140
1163
|
state.logger.log(`post-transform: compiling virtual SFC content from ${id}`);
|
|
1141
1164
|
try {
|
|
1142
|
-
const
|
|
1143
|
-
|
|
1144
|
-
ssr: state.mergedOptions?.ssr ?? false,
|
|
1145
|
-
vapor: state.mergedOptions?.vapor ?? false
|
|
1146
|
-
}, code);
|
|
1165
|
+
const isSsr = !!transformOptions?.ssr;
|
|
1166
|
+
const compiled = compileFile(id, getEnvironmentCache(state, isSsr), getCompileOptionsForRequest(state, isSsr), code);
|
|
1147
1167
|
const output = generateOutput(compiled, {
|
|
1148
1168
|
isProduction: state.isProduction,
|
|
1149
1169
|
isDev: state.server !== null,
|
|
@@ -1172,6 +1192,7 @@ function createPostTransformPlugin(state) {
|
|
|
1172
1192
|
function vize(options = {}) {
|
|
1173
1193
|
const state = {
|
|
1174
1194
|
cache: new Map(),
|
|
1195
|
+
ssrCache: new Map(),
|
|
1175
1196
|
collectedCss: new Map(),
|
|
1176
1197
|
precompileMetadata: new Map(),
|
|
1177
1198
|
pendingHmrUpdateTypes: new Map(),
|
|
@@ -1319,8 +1340,8 @@ function vize(options = {}) {
|
|
|
1319
1340
|
await compileAll(state);
|
|
1320
1341
|
state.logger.log("Cache keys:", [...state.cache.keys()].slice(0, 3));
|
|
1321
1342
|
},
|
|
1322
|
-
resolveId(id, importer) {
|
|
1323
|
-
return resolveIdHook(this, state, id, importer);
|
|
1343
|
+
resolveId(id, importer, options$1) {
|
|
1344
|
+
return resolveIdHook(this, state, id, importer, options$1);
|
|
1324
1345
|
},
|
|
1325
1346
|
load(id, loadOptions) {
|
|
1326
1347
|
return loadHook(state, id, loadOptions);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vizejs/vite-plugin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"description": "High-performance native Vite plugin for Vue SFC compilation powered by Vize",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"tinyglobby": "^0.2.0",
|
|
47
|
-
"@vizejs/native": "0.
|
|
47
|
+
"@vizejs/native": "0.31.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "tsdown",
|