vinext 0.1.3 → 0.1.5
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/build/client-build-config.d.ts +11 -2
- package/dist/build/client-build-config.js +17 -6
- package/dist/build/css-url-assets.d.ts +1 -1
- package/dist/build/css-url-assets.js +9 -7
- package/dist/build/prerender.js +3 -1
- package/dist/cache/cache-adapters-virtual.js +1 -1
- package/dist/client/pages-router-link-navigation.d.ts +33 -7
- package/dist/client/pages-router-link-navigation.js +32 -2
- package/dist/client/vinext-next-data.js +2 -0
- package/dist/cloudflare/src/cache/kv-data-adapter.runtime.d.ts +1 -1
- package/dist/config/config-matchers.d.ts +11 -1
- package/dist/config/config-matchers.js +14 -2
- package/dist/config/tsconfig-paths.js +14 -1
- package/dist/deploy.js +20 -13
- package/dist/entries/app-rsc-entry.js +27 -22
- package/dist/entries/pages-client-entry.js +14 -13
- package/dist/entries/pages-server-entry.js +8 -27
- package/dist/index.js +365 -147
- package/dist/plugins/css-data-url.js +30 -26
- package/dist/plugins/dynamic-preload-metadata.js +2 -4
- package/dist/plugins/extensionless-dynamic-import.js +27 -24
- package/dist/plugins/fonts.js +5 -4
- package/dist/plugins/import-meta-url.js +21 -15
- package/dist/plugins/instrumentation-client.js +1 -1
- package/dist/plugins/middleware-server-only.js +7 -6
- package/dist/plugins/og-assets.js +48 -46
- package/dist/plugins/optimize-imports.js +9 -3
- package/dist/plugins/remove-console.d.ts +7 -1
- package/dist/plugins/remove-console.js +4 -1
- package/dist/plugins/require-context.js +21 -20
- package/dist/plugins/strip-server-exports.d.ts +16 -8
- package/dist/plugins/strip-server-exports.js +496 -46
- package/dist/routing/app-route-graph.js +2 -2
- package/dist/server/app-bfcache-identity.d.ts +26 -0
- package/dist/server/app-bfcache-identity.js +127 -0
- package/dist/server/app-browser-action-result.js +1 -1
- package/dist/server/app-browser-entry.js +22 -12
- package/dist/server/app-browser-navigation-controller.d.ts +1 -1
- package/dist/server/app-browser-navigation-controller.js +1 -1
- package/dist/server/app-browser-state.d.ts +3 -22
- package/dist/server/app-browser-state.js +23 -139
- package/dist/server/app-browser-stream.js +1 -1
- package/dist/server/app-browser-visible-commit.d.ts +1 -1
- package/dist/server/app-browser-visible-commit.js +3 -2
- package/dist/server/app-fallback-renderer.d.ts +1 -1
- package/dist/server/app-layout-param-observation.d.ts +1 -1
- package/dist/server/app-layout-param-observation.js +1 -1
- package/dist/server/app-middleware.js +2 -1
- package/dist/server/app-page-boundary-render.d.ts +1 -1
- package/dist/server/app-page-boundary.js +1 -1
- package/dist/server/app-page-cache-finalizer.d.ts +62 -0
- package/dist/server/app-page-cache-finalizer.js +122 -0
- package/dist/server/app-page-cache-render.d.ts +2 -2
- package/dist/server/app-page-cache-render.js +1 -1
- package/dist/server/app-page-cache.d.ts +2 -53
- package/dist/server/app-page-cache.js +5 -131
- package/dist/server/app-page-dispatch.d.ts +2 -2
- package/dist/server/app-page-dispatch.js +10 -8
- package/dist/server/app-page-probe.js +3 -2
- package/dist/server/app-page-render-observation.js +2 -2
- package/dist/server/app-page-render.d.ts +3 -3
- package/dist/server/app-page-render.js +3 -2
- package/dist/server/app-page-stream.d.ts +2 -9
- package/dist/server/app-page-stream.js +1 -35
- package/dist/server/app-pages-bridge.d.ts +5 -1
- package/dist/server/app-pages-bridge.js +5 -13
- package/dist/server/app-request-context.d.ts +1 -2
- package/dist/server/app-request-context.js +2 -1
- package/dist/server/app-route-handler-dispatch.js +3 -2
- package/dist/server/app-route-handler-execution.d.ts +1 -1
- package/dist/server/app-route-handler-execution.js +1 -1
- package/dist/server/app-route-handler-response.d.ts +1 -1
- package/dist/server/app-router-entry.js +2 -1
- package/dist/server/app-rsc-handler.d.ts +3 -0
- package/dist/server/app-rsc-handler.js +73 -31
- package/dist/server/app-rsc-response-finalizer.js +1 -1
- package/dist/server/app-rsc-route-matching.js +6 -2
- package/dist/server/app-server-action-execution.d.ts +1 -1
- package/dist/server/app-server-action-execution.js +10 -6
- package/dist/server/app-ssr-entry.d.ts +1 -1
- package/dist/server/app-ssr-entry.js +12 -38
- package/dist/server/app-ssr-router-instance.d.ts +6 -0
- package/dist/server/app-ssr-router-instance.js +24 -0
- package/dist/server/app-ssr-stream.js +1 -1
- package/dist/server/artifact-compatibility.js +1 -1
- package/dist/server/before-interactive-head.d.ts +17 -0
- package/dist/server/before-interactive-head.js +35 -0
- package/dist/server/client-reuse-manifest.js +1 -1
- package/dist/server/csp.js +1 -4
- package/dist/server/defer-until-stream-consumed.d.ts +7 -0
- package/dist/server/defer-until-stream-consumed.js +34 -0
- package/dist/server/dev-server.js +82 -37
- package/dist/server/instrumentation.js +1 -1
- package/dist/server/isr-cache.d.ts +1 -1
- package/dist/server/isr-cache.js +1 -1
- package/dist/server/isr-decision.d.ts +1 -1
- package/dist/server/middleware-matcher.js +20 -9
- package/dist/server/middleware-runtime.d.ts +3 -4
- package/dist/server/middleware-runtime.js +4 -2
- package/dist/server/navigation-planner.d.ts +3 -12
- package/dist/server/navigation-planner.js +24 -0
- package/dist/server/navigation-trace.d.ts +2 -1
- package/dist/server/navigation-trace.js +1 -0
- package/dist/server/open-redirect.d.ts +12 -0
- package/dist/server/open-redirect.js +21 -0
- package/dist/server/operation-token.d.ts +40 -0
- package/dist/server/operation-token.js +85 -0
- package/dist/server/pages-data-route.d.ts +1 -1
- package/dist/server/pages-data-route.js +7 -4
- package/dist/server/pages-dev-module-url.d.ts +4 -0
- package/dist/server/pages-dev-module-url.js +15 -0
- package/dist/server/pages-document-initial-props.d.ts +4 -15
- package/dist/server/pages-document-initial-props.js +27 -56
- package/dist/server/pages-i18n.js +2 -2
- package/dist/server/pages-page-data.d.ts +1 -1
- package/dist/server/pages-page-data.js +3 -1
- package/dist/server/pages-page-handler.js +3 -1
- package/dist/server/pages-page-response.d.ts +3 -1
- package/dist/server/pages-page-response.js +6 -6
- package/dist/server/pages-readiness.js +1 -1
- package/dist/server/pages-request-pipeline.d.ts +7 -7
- package/dist/server/pages-request-pipeline.js +63 -21
- package/dist/server/prod-server.d.ts +3 -1
- package/dist/server/prod-server.js +43 -11
- package/dist/server/request-pipeline.d.ts +1 -24
- package/dist/server/request-pipeline.js +1 -33
- package/dist/server/seed-cache.d.ts +1 -1
- package/dist/server/static-file-cache.js +16 -4
- package/dist/shims/before-interactive-context.d.ts +14 -3
- package/dist/shims/cache-handler.d.ts +106 -0
- package/dist/shims/cache-handler.js +176 -0
- package/dist/shims/cache-request-state.d.ts +47 -0
- package/dist/shims/cache-request-state.js +126 -0
- package/dist/shims/cache-runtime.d.ts +2 -2
- package/dist/shims/cache-runtime.js +3 -14
- package/dist/shims/cache.d.ts +3 -231
- package/dist/shims/cache.js +17 -383
- package/dist/shims/cdn-cache.d.ts +1 -1
- package/dist/shims/cdn-cache.js +1 -1
- package/dist/shims/document.d.ts +15 -20
- package/dist/shims/document.js +5 -8
- package/dist/shims/error-boundary-navigation.d.ts +7 -0
- package/dist/shims/error-boundary-navigation.js +44 -0
- package/dist/shims/error-boundary.js +10 -8
- package/dist/shims/error.js +2 -1
- package/dist/shims/fetch-cache.js +1 -1
- package/dist/shims/form.js +1 -1
- package/dist/shims/image.js +74 -9
- package/dist/shims/internal/app-page-props-cache-key.d.ts +5 -0
- package/dist/shims/internal/app-page-props-cache-key.js +16 -0
- package/dist/shims/internal/navigation-untracked.js +2 -1
- package/dist/shims/internal/pages-data-fetch-dedup.d.ts +6 -7
- package/dist/shims/internal/pages-data-fetch-dedup.js +67 -14
- package/dist/shims/internal/pages-data-target.js +1 -1
- package/dist/shims/layout-segment-context.d.ts +1 -1
- package/dist/shims/layout-segment-context.js +2 -1
- package/dist/shims/link.js +38 -17
- package/dist/shims/metadata.js +4 -4
- package/dist/shims/navigation-context-state.d.ts +40 -0
- package/dist/shims/navigation-context-state.js +116 -0
- package/dist/shims/navigation-errors.d.ts +55 -0
- package/dist/shims/navigation-errors.js +110 -0
- package/dist/shims/navigation-server.d.ts +3 -0
- package/dist/shims/navigation-server.js +3 -0
- package/dist/shims/navigation-state.d.ts +1 -2
- package/dist/shims/navigation-state.js +2 -1
- package/dist/shims/navigation.d.ts +3 -291
- package/dist/shims/navigation.js +16 -445
- package/dist/shims/navigation.react-server.d.ts +2 -2
- package/dist/shims/navigation.react-server.js +3 -1
- package/dist/shims/request-state-types.d.ts +3 -3
- package/dist/shims/router.d.ts +6 -2
- package/dist/shims/router.js +99 -20
- package/dist/shims/script.js +9 -5
- package/dist/shims/slot.js +3 -1
- package/dist/shims/unified-request-context.d.ts +2 -2
- package/dist/utils/has-trailing-comma.d.ts +24 -0
- package/dist/utils/has-trailing-comma.js +62 -0
- package/dist/utils/text-stream.d.ts +1 -1
- package/dist/utils/text-stream.js +2 -2
- package/dist/utils/virtual-module.d.ts +5 -0
- package/dist/utils/virtual-module.js +0 -0
- package/dist/utils/vite-version.d.ts +12 -1
- package/dist/utils/vite-version.js +9 -1
- package/package.json +5 -1
|
@@ -36,33 +36,37 @@ function dataUrlCssPlugin() {
|
|
|
36
36
|
return {
|
|
37
37
|
name: "vinext:css-data-url",
|
|
38
38
|
enforce: "pre",
|
|
39
|
-
transform
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
css
|
|
56
|
-
|
|
39
|
+
transform: {
|
|
40
|
+
filter: {
|
|
41
|
+
id: { exclude: new RegExp(`^${VIRTUAL_PREFIX}`) },
|
|
42
|
+
code: DATA_URL_HINT
|
|
43
|
+
},
|
|
44
|
+
handler(code, id) {
|
|
45
|
+
let mutated = false;
|
|
46
|
+
const rewritten = code.replace(DATA_URL_IMPORT_RE, (_match, quote, moduleFlag, base64Flag, payload) => {
|
|
47
|
+
const isModule = moduleFlag === "+module";
|
|
48
|
+
const isBase64 = base64Flag === ";base64";
|
|
49
|
+
let css;
|
|
50
|
+
try {
|
|
51
|
+
css = decode(payload, isBase64);
|
|
52
|
+
} catch (err) {
|
|
53
|
+
throw new Error(`[vinext] Failed to decode CSS data URL import in ${id}: ${err.message}`);
|
|
54
|
+
}
|
|
55
|
+
const ext = isModule ? ".module.css" : ".css";
|
|
56
|
+
const syntheticId = `${VIRTUAL_PREFIX}${hash(css + ext)}${ext}`;
|
|
57
|
+
entries.set(syntheticId, {
|
|
58
|
+
css,
|
|
59
|
+
isModule
|
|
60
|
+
});
|
|
61
|
+
mutated = true;
|
|
62
|
+
return `${quote}${syntheticId}${quote}`;
|
|
57
63
|
});
|
|
58
|
-
mutated
|
|
59
|
-
return
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
map: null
|
|
65
|
-
};
|
|
64
|
+
if (!mutated) return null;
|
|
65
|
+
return {
|
|
66
|
+
code: rewritten,
|
|
67
|
+
map: null
|
|
68
|
+
};
|
|
69
|
+
}
|
|
66
70
|
},
|
|
67
71
|
resolveId(id) {
|
|
68
72
|
if (id.startsWith(VIRTUAL_PREFIX)) return id;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { isUnknownRecord } from "../utils/record.js";
|
|
2
|
+
import { hasTrailingComma } from "../utils/has-trailing-comma.js";
|
|
2
3
|
import { relativeWithinRoot, tryRealpathSync } from "../build/ssr-manifest.js";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
import { parseAst } from "vite";
|
|
@@ -281,15 +282,12 @@ function appendObjectProperty(output, objectNode, property) {
|
|
|
281
282
|
output.appendLeft(propertyEnd, `, ${property}`);
|
|
282
283
|
return true;
|
|
283
284
|
}
|
|
284
|
-
function stripComments(source) {
|
|
285
|
-
return source.replace(/\/\*[\s\S]*?\*\//g, "").replace(/\/\/[^\n]*/g, "");
|
|
286
|
-
}
|
|
287
285
|
function insertSecondOptionsArgument(output, code, callNode, firstArg, optionsLiteral) {
|
|
288
286
|
const callEnd = getNumber(callNode, "end");
|
|
289
287
|
const firstArgEnd = getNumber(firstArg, "end");
|
|
290
288
|
if (callEnd === null || firstArgEnd === null) return false;
|
|
291
289
|
const closeParen = callEnd - 1;
|
|
292
|
-
const separator =
|
|
290
|
+
const separator = hasTrailingComma(code.slice(firstArgEnd, closeParen)) ? " " : ", ";
|
|
293
291
|
output.appendLeft(closeParen, `${separator}${optionsLiteral}`);
|
|
294
292
|
return true;
|
|
295
293
|
}
|
|
@@ -29,28 +29,34 @@ function createExtensionlessDynamicImportPlugin() {
|
|
|
29
29
|
configResolved(config) {
|
|
30
30
|
moduleExtensions = getModuleExtensions(config);
|
|
31
31
|
},
|
|
32
|
-
transform
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
32
|
+
transform: {
|
|
33
|
+
filter: {
|
|
34
|
+
id: {
|
|
35
|
+
include: /\.(?:[cm]?[jt]s|[jt]sx)(?:\?.*)?$/i,
|
|
36
|
+
exclude: /[\\/]node_modules[\\/]/
|
|
37
|
+
},
|
|
38
|
+
code: /\bimport\s*\(/
|
|
39
|
+
},
|
|
40
|
+
handler(code, id) {
|
|
41
|
+
const lang = langForId(id);
|
|
42
|
+
let ast;
|
|
43
|
+
try {
|
|
44
|
+
ast = parseAst(code, { lang });
|
|
45
|
+
} catch {
|
|
46
|
+
return null;
|
|
47
|
+
}
|
|
48
|
+
const imports = collectExtensionlessImports(ast, code, moduleExtensions);
|
|
49
|
+
if (imports.length === 0) return null;
|
|
50
|
+
const output = new MagicString(code);
|
|
51
|
+
for (const dynamicImport of imports) {
|
|
52
|
+
const source = code.slice(dynamicImport.sourceStart, dynamicImport.sourceEnd);
|
|
53
|
+
output.overwrite(dynamicImport.start, dynamicImport.end, buildReplacement(source, dynamicImport.globPattern, dynamicImport.moduleExtensions));
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
code: output.toString(),
|
|
57
|
+
map: output.generateMap({ hires: "boundary" })
|
|
58
|
+
};
|
|
42
59
|
}
|
|
43
|
-
const imports = collectExtensionlessImports(ast, code, moduleExtensions);
|
|
44
|
-
if (imports.length === 0) return null;
|
|
45
|
-
const output = new MagicString(code);
|
|
46
|
-
for (const dynamicImport of imports) {
|
|
47
|
-
const source = code.slice(dynamicImport.sourceStart, dynamicImport.sourceEnd);
|
|
48
|
-
output.overwrite(dynamicImport.start, dynamicImport.end, buildReplacement(source, dynamicImport.globPattern, dynamicImport.moduleExtensions));
|
|
49
|
-
}
|
|
50
|
-
return {
|
|
51
|
-
code: output.toString(),
|
|
52
|
-
map: output.generateMap({ hires: "boundary" })
|
|
53
|
-
};
|
|
54
60
|
}
|
|
55
61
|
};
|
|
56
62
|
}
|
|
@@ -64,9 +70,6 @@ function langForId(id) {
|
|
|
64
70
|
if (ext === ".tsx") return "tsx";
|
|
65
71
|
return "jsx";
|
|
66
72
|
}
|
|
67
|
-
function isDependencyId(id) {
|
|
68
|
-
return id.split("?", 1)[0].replaceAll("\\", "/").includes("/node_modules/");
|
|
69
|
-
}
|
|
70
73
|
function collectExtensionlessImports(ast, code, moduleExtensions) {
|
|
71
74
|
const imports = [];
|
|
72
75
|
function visit(value) {
|
package/dist/plugins/fonts.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { escapeRegExp } from "../utils/regex.js";
|
|
2
|
+
import { lastSignificantChar } from "../utils/has-trailing-comma.js";
|
|
2
3
|
import { buildFallbackFontFace, getFallbackFontOverrideMetrics } from "../build/google-fonts/fallback-metrics.js";
|
|
3
4
|
import { validateGoogleFontOptions } from "../build/google-fonts/validate.js";
|
|
4
5
|
import { getFontAxes } from "../build/google-fonts/get-axes.js";
|
|
@@ -552,8 +553,8 @@ function createGoogleFontsPlugin(fontGoogleShimPath, shimsDir) {
|
|
|
552
553
|
if (validatedFontStyle) internalFontProperties.push(`fontStyle: ${JSON.stringify(validatedFontStyle)}`);
|
|
553
554
|
const injectedProperties = [`_vinext: { font: { ${internalFontProperties.join(", ")} } }`];
|
|
554
555
|
const closingBrace = optionsStr.lastIndexOf("}");
|
|
555
|
-
const
|
|
556
|
-
const separator =
|
|
556
|
+
const lastChar = lastSignificantChar(optionsStr.slice(0, closingBrace));
|
|
557
|
+
const separator = lastChar === "{" || lastChar === "," ? "" : ", ";
|
|
557
558
|
const replacement = `${calleeSource}(${optionsStr.slice(0, closingBrace) + separator + injectedProperties.join(", ") + optionsStr.slice(closingBrace)})`;
|
|
558
559
|
s.overwrite(callStart, callEnd, replacement);
|
|
559
560
|
overwrittenRanges.push([callStart, callEnd]);
|
|
@@ -688,8 +689,8 @@ function createLocalFontsPlugin(shimsDir) {
|
|
|
688
689
|
if (familyPayloadInsertions.has(insertAt)) continue;
|
|
689
690
|
const optionsStr = code.slice(objRange[0], objRange[1]);
|
|
690
691
|
if (/(?:^|[,{])\s*_vinext\s*:/.test(optionsStr)) continue;
|
|
691
|
-
const
|
|
692
|
-
const separator =
|
|
692
|
+
const lastChar = lastSignificantChar(optionsStr.slice(0, -1));
|
|
693
|
+
const separator = lastChar === "{" || lastChar === "," ? "" : ", ";
|
|
693
694
|
s.appendLeft(insertAt, `${separator}_vinext: { font: { family: ${JSON.stringify(bindingName)} } }`);
|
|
694
695
|
familyPayloadInsertions.add(insertAt);
|
|
695
696
|
hasChanges = true;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { VIRTUAL_MODULE_ID_RE } from "../utils/virtual-module.js";
|
|
1
2
|
import { collectBindingNames, forEachAstChild, hasRange, isAstRecord, isIdentifierNamed, nodeArray } from "./ast-utils.js";
|
|
2
3
|
import { tryRealpathSync } from "../build/ssr-manifest.js";
|
|
3
4
|
import path from "node:path";
|
|
@@ -32,18 +33,26 @@ function createImportMetaUrlPlugin(options) {
|
|
|
32
33
|
outputDirs = [config.build.outDir];
|
|
33
34
|
rootPaths = createRootPaths(root, { outputDirs });
|
|
34
35
|
},
|
|
35
|
-
transform
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
36
|
+
transform: {
|
|
37
|
+
filter: {
|
|
38
|
+
id: {
|
|
39
|
+
include: /\.(?:[cm]?[jt]s|[jt]sx)(?:\?.*)?$/,
|
|
40
|
+
exclude: [/[\\/]node_modules[\\/]/, VIRTUAL_MODULE_ID_RE]
|
|
41
|
+
},
|
|
42
|
+
code: /import\.meta(?:\.|\?\.)url|__filename|__dirname/
|
|
43
|
+
},
|
|
44
|
+
handler(code, id) {
|
|
45
|
+
const paths = getRootPaths();
|
|
46
|
+
if (!paths) return null;
|
|
47
|
+
const canonicalId = transformableModuleCanonicalId(cleanModuleId(id), paths);
|
|
48
|
+
if (!canonicalId) return null;
|
|
49
|
+
const rewritten = rewriteCanonicalSourceIdentity(code, canonicalId, paths, this.environment?.name === "client" ? "client" : "server");
|
|
50
|
+
if (!rewritten) return null;
|
|
51
|
+
return {
|
|
52
|
+
code: rewritten.code,
|
|
53
|
+
map: rewritten.map
|
|
54
|
+
};
|
|
55
|
+
}
|
|
47
56
|
}
|
|
48
57
|
};
|
|
49
58
|
}
|
|
@@ -120,9 +129,6 @@ function mayContainImportMetaUrl(code) {
|
|
|
120
129
|
function mayContainServerCjsGlobal(code) {
|
|
121
130
|
return code.includes("__filename") || code.includes("__dirname");
|
|
122
131
|
}
|
|
123
|
-
function mayContainSourceIdentityToken(code) {
|
|
124
|
-
return mayContainImportMetaUrl(code) || mayContainServerCjsGlobal(code);
|
|
125
|
-
}
|
|
126
132
|
function excludedRelativePrefixes(canonicalRoot, normalizedRoot, options) {
|
|
127
133
|
const prefixes = new Set([
|
|
128
134
|
".next",
|
|
@@ -8,7 +8,7 @@ function createInstrumentationClientTransformPlugin(getInstrumentationClientPath
|
|
|
8
8
|
transform(code, id) {
|
|
9
9
|
const instrumentationClientPath = getInstrumentationClientPath();
|
|
10
10
|
if (!instrumentationClientPath) return null;
|
|
11
|
-
if (normalizePath(id.split("?", 1)[0]) !==
|
|
11
|
+
if (normalizePath(id.split("?", 1)[0]) !== instrumentationClientPath) return null;
|
|
12
12
|
if (code.includes("__vinextInstrumentationClientStart")) return null;
|
|
13
13
|
const ast = parseAst(code);
|
|
14
14
|
let insertPos = 0;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { normalizePathSeparators } from "../utils/path.js";
|
|
1
2
|
//#region src/plugins/middleware-server-only.ts
|
|
2
3
|
/**
|
|
3
4
|
* Allow `import 'server-only'` from middleware (and any module reachable
|
|
@@ -44,16 +45,16 @@
|
|
|
44
45
|
*/
|
|
45
46
|
function createMiddlewareServerOnlyPlugin(options) {
|
|
46
47
|
const tainted = /* @__PURE__ */ new Set();
|
|
48
|
+
function canonicalizeId(id) {
|
|
49
|
+
const queryIndex = id.indexOf("?");
|
|
50
|
+
return normalizePathSeparators(queryIndex === -1 ? id : id.slice(0, queryIndex));
|
|
51
|
+
}
|
|
47
52
|
function isTainted(id) {
|
|
48
53
|
if (!id) return false;
|
|
49
|
-
|
|
50
|
-
const queryIndex = id.indexOf("?");
|
|
51
|
-
if (queryIndex !== -1) return tainted.has(id.slice(0, queryIndex));
|
|
52
|
-
return false;
|
|
54
|
+
return tainted.has(canonicalizeId(id));
|
|
53
55
|
}
|
|
54
56
|
function addTainted(id) {
|
|
55
|
-
|
|
56
|
-
tainted.add(queryIndex === -1 ? id : id.slice(0, queryIndex));
|
|
57
|
+
tainted.add(canonicalizeId(id));
|
|
57
58
|
}
|
|
58
59
|
return {
|
|
59
60
|
name: "vinext:middleware-server-only",
|
|
@@ -25,54 +25,56 @@ function createOgInlineFetchAssetsPlugin() {
|
|
|
25
25
|
buildStart() {
|
|
26
26
|
if (isBuild) cache.clear();
|
|
27
27
|
},
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
const
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
28
|
+
transform: {
|
|
29
|
+
filter: { code: "import.meta.url" },
|
|
30
|
+
async handler(code, id) {
|
|
31
|
+
const useCache = isBuild;
|
|
32
|
+
const moduleDir = path.dirname(id);
|
|
33
|
+
let newCode = code;
|
|
34
|
+
let didReplace = false;
|
|
35
|
+
const readAsBase64 = async (absPath) => {
|
|
36
|
+
const cached = useCache ? cache.get(absPath) : void 0;
|
|
37
|
+
if (cached !== void 0) return cached;
|
|
38
|
+
try {
|
|
39
|
+
const b64 = (await fs.promises.readFile(absPath)).toString("base64");
|
|
40
|
+
if (useCache) cache.set(absPath, b64);
|
|
41
|
+
return b64;
|
|
42
|
+
} catch {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
if (code.includes("fetch(")) for (const match of code.matchAll(/fetch\(\s*new URL\(\s*(["'])(\.[^"']+)\1\s*,\s*import\.meta\.url\s*\)\s*\)(?:\.then\(\s*(?:function\s*\([^)]*\)|\([^)]*\)\s*=>)\s*\{?\s*return\s+[^.]+\.arrayBuffer\(\)\s*;?\s*\}?\s*,?\s*\)|\.then\(\s*\([^)]*\)\s*=>\s*[^.]+\.arrayBuffer\(\)\s*,?\s*\))/g)) {
|
|
47
|
+
const fullMatch = match[0];
|
|
48
|
+
const relPath = match[2];
|
|
49
|
+
const fileBase64 = await readAsBase64(path.resolve(moduleDir, relPath));
|
|
50
|
+
if (fileBase64 === null) continue;
|
|
51
|
+
const inlined = [
|
|
52
|
+
`(function(){`,
|
|
53
|
+
`var b=${JSON.stringify(fileBase64)};`,
|
|
54
|
+
`var r=atob(b);`,
|
|
55
|
+
`var a=new Uint8Array(r.length);`,
|
|
56
|
+
`for(var i=0;i<r.length;i++)a[i]=r.charCodeAt(i);`,
|
|
57
|
+
`return Promise.resolve(a.buffer);`,
|
|
58
|
+
`})()`
|
|
59
|
+
].join("");
|
|
60
|
+
newCode = newCode.replaceAll(fullMatch, inlined);
|
|
61
|
+
didReplace = true;
|
|
43
62
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
].join("");
|
|
59
|
-
newCode = newCode.replaceAll(fullMatch, inlined);
|
|
60
|
-
didReplace = true;
|
|
61
|
-
}
|
|
62
|
-
if (code.includes("readFileSync(")) for (const match of newCode.matchAll(/[a-zA-Z_$][a-zA-Z0-9_$]*\.readFileSync\(\s*(?:[a-zA-Z_$][a-zA-Z0-9_$]*\.)?fileURLToPath\(\s*new URL\(\s*(["'])(\.[^"']+)\1\s*,\s*import\.meta\.url\s*\)\s*\)\s*\)/g)) {
|
|
63
|
-
const fullMatch = match[0];
|
|
64
|
-
const relPath = match[2];
|
|
65
|
-
const fileBase64 = await readAsBase64(path.resolve(moduleDir, relPath));
|
|
66
|
-
if (fileBase64 === null) continue;
|
|
67
|
-
const inlined = `Buffer.from(${JSON.stringify(fileBase64)},"base64")`;
|
|
68
|
-
newCode = newCode.replaceAll(fullMatch, inlined);
|
|
69
|
-
didReplace = true;
|
|
63
|
+
if (code.includes("readFileSync(")) for (const match of newCode.matchAll(/[a-zA-Z_$][a-zA-Z0-9_$]*\.readFileSync\(\s*(?:[a-zA-Z_$][a-zA-Z0-9_$]*\.)?fileURLToPath\(\s*new URL\(\s*(["'])(\.[^"']+)\1\s*,\s*import\.meta\.url\s*\)\s*\)\s*\)/g)) {
|
|
64
|
+
const fullMatch = match[0];
|
|
65
|
+
const relPath = match[2];
|
|
66
|
+
const fileBase64 = await readAsBase64(path.resolve(moduleDir, relPath));
|
|
67
|
+
if (fileBase64 === null) continue;
|
|
68
|
+
const inlined = `Buffer.from(${JSON.stringify(fileBase64)},"base64")`;
|
|
69
|
+
newCode = newCode.replaceAll(fullMatch, inlined);
|
|
70
|
+
didReplace = true;
|
|
71
|
+
}
|
|
72
|
+
if (!didReplace) return null;
|
|
73
|
+
return {
|
|
74
|
+
code: newCode,
|
|
75
|
+
map: null
|
|
76
|
+
};
|
|
70
77
|
}
|
|
71
|
-
if (!didReplace) return null;
|
|
72
|
-
return {
|
|
73
|
-
code: newCode,
|
|
74
|
-
map: null
|
|
75
|
-
};
|
|
76
78
|
}
|
|
77
79
|
};
|
|
78
80
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { normalizePathSeparators } from "../utils/path.js";
|
|
2
2
|
import { escapeRegExp } from "../utils/regex.js";
|
|
3
|
+
import { VIRTUAL_MODULE_ID_RE } from "../utils/virtual-module.js";
|
|
3
4
|
import { getAstName } from "./ast-utils.js";
|
|
4
5
|
import { createRequire } from "node:module";
|
|
5
6
|
import path from "node:path";
|
|
@@ -445,12 +446,17 @@ function createOptimizeImportsPlugin(getNextConfig, getRoot) {
|
|
|
445
446
|
return await this.resolve(source, barrelEntry, { skipSelf: true }) ?? void 0;
|
|
446
447
|
},
|
|
447
448
|
transform: {
|
|
448
|
-
filter: {
|
|
449
|
-
|
|
449
|
+
filter: {
|
|
450
|
+
id: {
|
|
451
|
+
include: /\.(tsx?|jsx?|mjs)$/,
|
|
452
|
+
exclude: VIRTUAL_MODULE_ID_RE
|
|
453
|
+
},
|
|
454
|
+
code: /\bimport\b[\s\S]*\bfrom\b/
|
|
455
|
+
},
|
|
456
|
+
async handler(code) {
|
|
450
457
|
const env = this.environment;
|
|
451
458
|
if (env?.name === "client") return null;
|
|
452
459
|
const preferReactServer = env?.name === "rsc";
|
|
453
|
-
if (id.startsWith("\0")) return null;
|
|
454
460
|
const packages = optimizedPackages;
|
|
455
461
|
if (!hasOptimizedImportSource(code)) return null;
|
|
456
462
|
let ast;
|
|
@@ -1,7 +1,13 @@
|
|
|
1
|
+
import MagicString from "magic-string";
|
|
2
|
+
|
|
1
3
|
//#region src/plugins/remove-console.d.ts
|
|
2
4
|
type RemoveConsoleConfig = boolean | {
|
|
3
5
|
exclude: string[];
|
|
4
6
|
};
|
|
7
|
+
type RemoveConsoleResult = {
|
|
8
|
+
code: string;
|
|
9
|
+
map: ReturnType<MagicString["generateMap"]>;
|
|
10
|
+
};
|
|
5
11
|
/**
|
|
6
12
|
* Walk the AST body looking for expression statements whose expression is a
|
|
7
13
|
* CallExpression with a callee of `console.<identifier>`. When found, check
|
|
@@ -10,6 +16,6 @@ type RemoveConsoleConfig = boolean | {
|
|
|
10
16
|
*
|
|
11
17
|
* Returns `null` if no console calls are removed.
|
|
12
18
|
*/
|
|
13
|
-
declare function removeConsoleCalls(code: string, config: RemoveConsoleConfig):
|
|
19
|
+
declare function removeConsoleCalls(code: string, config: RemoveConsoleConfig): RemoveConsoleResult | null;
|
|
14
20
|
//#endregion
|
|
15
21
|
export { removeConsoleCalls };
|
|
@@ -168,7 +168,10 @@ function removeConsoleCalls(code, config) {
|
|
|
168
168
|
}
|
|
169
169
|
for (const node of ast.body) walk(node);
|
|
170
170
|
if (!changed) return null;
|
|
171
|
-
return
|
|
171
|
+
return {
|
|
172
|
+
code: s.toString(),
|
|
173
|
+
map: s.generateMap({ hires: "boundary" })
|
|
174
|
+
};
|
|
172
175
|
}
|
|
173
176
|
//#endregion
|
|
174
177
|
export { removeConsoleCalls };
|
|
@@ -16,30 +16,31 @@ function createRequireContextPlugin() {
|
|
|
16
16
|
return {
|
|
17
17
|
name: "vinext:require-context",
|
|
18
18
|
enforce: "pre",
|
|
19
|
-
transform
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
transform: {
|
|
20
|
+
filter: {
|
|
21
|
+
id: /\.(?:[cm]?[jt]s|[jt]sx)(?:\?.*)?$/i,
|
|
22
|
+
code: /\brequire\b[\s\S]*\.context/
|
|
23
|
+
},
|
|
24
|
+
handler(code, id) {
|
|
25
|
+
const lang = langForId(id);
|
|
26
|
+
let ast;
|
|
27
|
+
try {
|
|
28
|
+
ast = parseAst(code, { lang });
|
|
29
|
+
} catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
const calls = collectRequireContextCalls(ast);
|
|
33
|
+
if (calls.length === 0) return null;
|
|
34
|
+
const output = new MagicString(code);
|
|
35
|
+
for (const call of calls) output.overwrite(call.range.start, call.range.end, buildReplacement(call));
|
|
36
|
+
return {
|
|
37
|
+
code: output.toString(),
|
|
38
|
+
map: output.generateMap({ hires: "boundary" })
|
|
39
|
+
};
|
|
28
40
|
}
|
|
29
|
-
const calls = collectRequireContextCalls(ast);
|
|
30
|
-
if (calls.length === 0) return null;
|
|
31
|
-
const output = new MagicString(code);
|
|
32
|
-
for (const call of calls) output.overwrite(call.range.start, call.range.end, buildReplacement(call));
|
|
33
|
-
return {
|
|
34
|
-
code: output.toString(),
|
|
35
|
-
map: output.generateMap({ hires: "boundary" })
|
|
36
|
-
};
|
|
37
41
|
}
|
|
38
42
|
};
|
|
39
43
|
}
|
|
40
|
-
function mayContainRequireContext(code) {
|
|
41
|
-
return code.includes("require") && code.includes(".context");
|
|
42
|
-
}
|
|
43
44
|
function langForId(id) {
|
|
44
45
|
const clean = id.split("?", 1)[0];
|
|
45
46
|
const dot = clean.lastIndexOf(".");
|
|
@@ -1,13 +1,21 @@
|
|
|
1
|
+
import MagicString from "magic-string";
|
|
2
|
+
|
|
1
3
|
//#region src/plugins/strip-server-exports.d.ts
|
|
4
|
+
declare function hasServerExportCandidate(code: string): boolean;
|
|
5
|
+
declare function hasExportAllCandidate(code: string): boolean;
|
|
6
|
+
declare function validatePageExports(code: string): void;
|
|
7
|
+
type StripServerExportsResult = {
|
|
8
|
+
code: string;
|
|
9
|
+
map: ReturnType<MagicString["generateMap"]>;
|
|
10
|
+
};
|
|
2
11
|
/**
|
|
3
|
-
* Strip server-only data-fetching exports
|
|
4
|
-
*
|
|
5
|
-
* bundle. Uses Vite's parseAst (Rollup/acorn) for correct handling
|
|
6
|
-
* of all export patterns including function expressions, arrow
|
|
7
|
-
* functions with TS return types, and re-exports.
|
|
12
|
+
* Strip server-only Pages Router data-fetching exports and their unique
|
|
13
|
+
* dependency graph from browser bundles.
|
|
8
14
|
*
|
|
9
|
-
*
|
|
15
|
+
* Ported from Next.js:
|
|
16
|
+
* - test/unit/babel-plugin-next-ssg-transform.test.ts
|
|
17
|
+
* - crates/next-custom-transforms/src/transforms/strip_page_exports.rs
|
|
10
18
|
*/
|
|
11
|
-
declare function stripServerExports(code: string):
|
|
19
|
+
declare function stripServerExports(code: string): StripServerExportsResult | null;
|
|
12
20
|
//#endregion
|
|
13
|
-
export { stripServerExports };
|
|
21
|
+
export { hasExportAllCandidate, hasServerExportCandidate, stripServerExports, validatePageExports };
|