@tanstack/start-plugin-core 1.131.7 → 1.132.0-alpha.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/esm/{nitro-plugin/build-sitemap.d.ts → build-sitemap.d.ts} +3 -3
- package/dist/esm/{nitro-plugin/build-sitemap.js → build-sitemap.js} +19 -24
- package/dist/esm/build-sitemap.js.map +1 -0
- package/dist/esm/compilers.js +7 -10
- package/dist/esm/compilers.js.map +1 -1
- package/dist/esm/constants.d.ts +6 -2
- package/dist/esm/constants.js +7 -10
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/debug.js.map +1 -1
- package/dist/esm/dev-server-plugin/extract-html-scripts.js.map +1 -1
- package/dist/esm/dev-server-plugin/plugin.d.ts +5 -5
- package/dist/esm/dev-server-plugin/plugin.js +117 -79
- package/dist/esm/dev-server-plugin/plugin.js.map +1 -1
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +0 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/load-env-plugin/plugin.d.ts +2 -3
- package/dist/esm/load-env-plugin/plugin.js +5 -8
- package/dist/esm/load-env-plugin/plugin.js.map +1 -1
- package/dist/esm/output-directory.d.ts +3 -0
- package/dist/esm/output-directory.js +14 -0
- package/dist/esm/output-directory.js.map +1 -0
- package/dist/esm/plugin.d.ts +6 -290
- package/dist/esm/plugin.js +123 -76
- package/dist/esm/plugin.js.map +1 -1
- package/dist/esm/post-server-build.d.ts +7 -0
- package/dist/esm/post-server-build.js +55 -0
- package/dist/esm/post-server-build.js.map +1 -0
- package/dist/esm/prerender.d.ts +11 -0
- package/dist/esm/{nitro-plugin/prerender.js → prerender.js} +85 -73
- package/dist/esm/prerender.js.map +1 -0
- package/dist/esm/{nitro-plugin/queue.js → queue.js} +7 -10
- package/dist/esm/queue.js.map +1 -0
- package/dist/esm/resolve-entries.d.ts +8 -0
- package/dist/esm/resolve-entries.js +37 -0
- package/dist/esm/resolve-entries.js.map +1 -0
- package/dist/esm/schema.d.ts +1369 -6719
- package/dist/esm/schema.js +52 -85
- package/dist/esm/schema.js.map +1 -1
- package/dist/esm/start-compiler-plugin.js +2 -2
- package/dist/esm/start-compiler-plugin.js.map +1 -1
- package/dist/esm/start-manifest-plugin/plugin.d.ts +1 -1
- package/dist/esm/start-manifest-plugin/plugin.js +8 -13
- package/dist/esm/start-manifest-plugin/plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js +2 -3
- package/dist/esm/start-router-plugin/generator-plugins/routes-manifest-plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.js +2 -4
- package/dist/esm/start-router-plugin/generator-plugins/server-routes-plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/route-tree-client-plugin.js.map +1 -1
- package/dist/esm/start-router-plugin/virtual-route-tree-plugin.js.map +1 -1
- package/dist/esm/utils.js.map +1 -1
- package/package.json +13 -18
- package/src/{nitro-plugin/build-sitemap.ts → build-sitemap.ts} +8 -8
- package/src/constants.ts +12 -9
- package/src/dev-server-plugin/plugin.ts +140 -99
- package/src/global.d.ts +0 -2
- package/src/index.ts +1 -5
- package/src/load-env-plugin/plugin.ts +6 -11
- package/src/output-directory.ts +18 -0
- package/src/plugin.ts +160 -98
- package/src/post-server-build.ts +73 -0
- package/src/{nitro-plugin/prerender.ts → prerender.ts} +93 -86
- package/src/resolve-entries.ts +52 -0
- package/src/schema.ts +88 -121
- package/src/start-manifest-plugin/plugin.ts +8 -14
- package/dist/cjs/compilers.cjs +0 -416
- package/dist/cjs/compilers.cjs.map +0 -1
- package/dist/cjs/compilers.d.cts +0 -21
- package/dist/cjs/constants.cjs +0 -20
- package/dist/cjs/constants.cjs.map +0 -1
- package/dist/cjs/constants.d.cts +0 -6
- package/dist/cjs/debug.cjs +0 -5
- package/dist/cjs/debug.cjs.map +0 -1
- package/dist/cjs/debug.d.cts +0 -1
- package/dist/cjs/dev-server-plugin/extract-html-scripts.cjs +0 -35
- package/dist/cjs/dev-server-plugin/extract-html-scripts.cjs.map +0 -1
- package/dist/cjs/dev-server-plugin/extract-html-scripts.d.cts +0 -4
- package/dist/cjs/dev-server-plugin/plugin.cjs +0 -121
- package/dist/cjs/dev-server-plugin/plugin.cjs.map +0 -1
- package/dist/cjs/dev-server-plugin/plugin.d.cts +0 -5
- package/dist/cjs/index.cjs +0 -11
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/cjs/index.d.cts +0 -3
- package/dist/cjs/load-env-plugin/plugin.cjs +0 -34
- package/dist/cjs/load-env-plugin/plugin.cjs.map +0 -1
- package/dist/cjs/load-env-plugin/plugin.d.cts +0 -3
- package/dist/cjs/nitro-plugin/build-sitemap.cjs +0 -138
- package/dist/cjs/nitro-plugin/build-sitemap.cjs.map +0 -1
- package/dist/cjs/nitro-plugin/build-sitemap.d.cts +0 -31
- package/dist/cjs/nitro-plugin/plugin.cjs +0 -187
- package/dist/cjs/nitro-plugin/plugin.cjs.map +0 -1
- package/dist/cjs/nitro-plugin/plugin.d.cts +0 -3
- package/dist/cjs/nitro-plugin/prerender.cjs +0 -178
- package/dist/cjs/nitro-plugin/prerender.cjs.map +0 -1
- package/dist/cjs/nitro-plugin/prerender.d.cts +0 -8
- package/dist/cjs/nitro-plugin/queue.cjs +0 -131
- package/dist/cjs/nitro-plugin/queue.cjs.map +0 -1
- package/dist/cjs/nitro-plugin/queue.d.cts +0 -32
- package/dist/cjs/plugin.cjs +0 -227
- package/dist/cjs/plugin.cjs.map +0 -1
- package/dist/cjs/plugin.d.cts +0 -300
- package/dist/cjs/resolve-virtual-entries-plugin/plugin.cjs +0 -80
- package/dist/cjs/resolve-virtual-entries-plugin/plugin.cjs.map +0 -1
- package/dist/cjs/resolve-virtual-entries-plugin/plugin.d.cts +0 -3
- package/dist/cjs/schema.cjs +0 -158
- package/dist/cjs/schema.cjs.map +0 -1
- package/dist/cjs/schema.d.cts +0 -8878
- package/dist/cjs/start-compiler-plugin.cjs +0 -78
- package/dist/cjs/start-compiler-plugin.cjs.map +0 -1
- package/dist/cjs/start-compiler-plugin.d.cts +0 -13
- package/dist/cjs/start-manifest-plugin/plugin.cjs +0 -182
- package/dist/cjs/start-manifest-plugin/plugin.cjs.map +0 -1
- package/dist/cjs/start-manifest-plugin/plugin.d.cts +0 -6
- package/dist/cjs/start-router-plugin/generator-plugins/routes-manifest-plugin.cjs +0 -39
- package/dist/cjs/start-router-plugin/generator-plugins/routes-manifest-plugin.cjs.map +0 -1
- package/dist/cjs/start-router-plugin/generator-plugins/routes-manifest-plugin.d.cts +0 -6
- package/dist/cjs/start-router-plugin/generator-plugins/server-routes-plugin.cjs +0 -121
- package/dist/cjs/start-router-plugin/generator-plugins/server-routes-plugin.cjs.map +0 -1
- package/dist/cjs/start-router-plugin/generator-plugins/server-routes-plugin.d.cts +0 -2
- package/dist/cjs/start-router-plugin/plugin.cjs +0 -45
- package/dist/cjs/start-router-plugin/plugin.cjs.map +0 -1
- package/dist/cjs/start-router-plugin/plugin.d.cts +0 -3
- package/dist/cjs/start-router-plugin/route-tree-client-plugin.cjs +0 -73
- package/dist/cjs/start-router-plugin/route-tree-client-plugin.cjs.map +0 -1
- package/dist/cjs/start-router-plugin/route-tree-client-plugin.d.cts +0 -6
- package/dist/cjs/start-router-plugin/virtual-route-tree-plugin.cjs +0 -29
- package/dist/cjs/start-router-plugin/virtual-route-tree-plugin.cjs.map +0 -1
- package/dist/cjs/start-router-plugin/virtual-route-tree-plugin.d.cts +0 -3
- package/dist/cjs/utils.cjs +0 -18
- package/dist/cjs/utils.cjs.map +0 -1
- package/dist/cjs/utils.d.cts +0 -8
- package/dist/esm/nitro-plugin/build-sitemap.js.map +0 -1
- package/dist/esm/nitro-plugin/plugin.d.ts +0 -3
- package/dist/esm/nitro-plugin/plugin.js +0 -187
- package/dist/esm/nitro-plugin/plugin.js.map +0 -1
- package/dist/esm/nitro-plugin/prerender.d.ts +0 -8
- package/dist/esm/nitro-plugin/prerender.js.map +0 -1
- package/dist/esm/nitro-plugin/queue.js.map +0 -1
- package/dist/esm/resolve-virtual-entries-plugin/plugin.d.ts +0 -3
- package/dist/esm/resolve-virtual-entries-plugin/plugin.js +0 -63
- package/dist/esm/resolve-virtual-entries-plugin/plugin.js.map +0 -1
- package/src/nitro-plugin/plugin.ts +0 -252
- package/src/resolve-virtual-entries-plugin/plugin.ts +0 -77
- /package/dist/esm/{nitro-plugin/queue.d.ts → queue.d.ts} +0 -0
- /package/src/{nitro-plugin/queue.ts → queue.ts} +0 -0
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { TanStackStartOutputConfig } from '
|
|
1
|
+
import { TanStackStartOutputConfig } from './schema.js';
|
|
2
2
|
export type SitemapUrl = {
|
|
3
3
|
loc: string;
|
|
4
4
|
lastmod: string;
|
|
@@ -25,7 +25,7 @@ export type SitemapUrl = {
|
|
|
25
25
|
export type SitemapData = {
|
|
26
26
|
urls: Array<SitemapUrl>;
|
|
27
27
|
};
|
|
28
|
-
export declare function buildSitemap({
|
|
29
|
-
|
|
28
|
+
export declare function buildSitemap({ startConfig, publicDir, }: {
|
|
29
|
+
startConfig: TanStackStartOutputConfig;
|
|
30
30
|
publicDir: string;
|
|
31
31
|
}): void;
|
|
@@ -1,28 +1,23 @@
|
|
|
1
1
|
import { writeFileSync } from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { create } from "xmlbuilder2";
|
|
4
|
-
import { createLogger } from "
|
|
4
|
+
import { createLogger } from "./utils.js";
|
|
5
5
|
function buildSitemapJson(pages, host) {
|
|
6
6
|
const slash = checkSlash(host);
|
|
7
7
|
const urls = pages.filter((page) => {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
images: (_e = page.sitemap) == null ? void 0 : _e.images,
|
|
19
|
-
news: (_f = page.sitemap) == null ? void 0 : _f.news
|
|
20
|
-
};
|
|
21
|
-
});
|
|
8
|
+
return page.sitemap?.exclude !== true;
|
|
9
|
+
}).map((page) => ({
|
|
10
|
+
loc: `${host}${slash}${page.path.replace(/^\/+/g, "")}`,
|
|
11
|
+
lastmod: page.sitemap?.lastmod ? new Date(page.sitemap.lastmod).toISOString().split("T")[0] : (/* @__PURE__ */ new Date()).toISOString().split("T")[0],
|
|
12
|
+
priority: page.sitemap?.priority,
|
|
13
|
+
changefreq: page.sitemap?.changefreq,
|
|
14
|
+
alternateRefs: page.sitemap?.alternateRefs,
|
|
15
|
+
images: page.sitemap?.images,
|
|
16
|
+
news: page.sitemap?.news
|
|
17
|
+
}));
|
|
22
18
|
return { urls };
|
|
23
19
|
}
|
|
24
20
|
function jsonToXml(sitemapData) {
|
|
25
|
-
var _a, _b;
|
|
26
21
|
const sitemap = createXml("urlset");
|
|
27
22
|
for (const item of sitemapData.urls) {
|
|
28
23
|
const page = sitemap.ele("url");
|
|
@@ -34,7 +29,7 @@ function jsonToXml(sitemapData) {
|
|
|
34
29
|
if (item.changefreq) {
|
|
35
30
|
page.ele("changefreq").txt(item.changefreq);
|
|
36
31
|
}
|
|
37
|
-
if (
|
|
32
|
+
if (item.alternateRefs?.length) {
|
|
38
33
|
for (const ref of item.alternateRefs) {
|
|
39
34
|
const alternateRef = page.ele("xhtml:link");
|
|
40
35
|
alternateRef.att("rel", "alternate");
|
|
@@ -44,7 +39,7 @@ function jsonToXml(sitemapData) {
|
|
|
44
39
|
}
|
|
45
40
|
}
|
|
46
41
|
}
|
|
47
|
-
if (
|
|
42
|
+
if (item.images?.length) {
|
|
48
43
|
for (const image of item.images) {
|
|
49
44
|
const imageElement = page.ele("image:image");
|
|
50
45
|
imageElement.ele("image:loc").txt(image.loc);
|
|
@@ -68,20 +63,20 @@ function jsonToXml(sitemapData) {
|
|
|
68
63
|
return sitemap.end({ prettyPrint: true });
|
|
69
64
|
}
|
|
70
65
|
function buildSitemap({
|
|
71
|
-
|
|
66
|
+
startConfig,
|
|
72
67
|
publicDir
|
|
73
68
|
}) {
|
|
74
69
|
const logger = createLogger("sitemap");
|
|
75
|
-
let sitemapOptions =
|
|
76
|
-
if (!sitemapOptions &&
|
|
70
|
+
let sitemapOptions = startConfig.sitemap;
|
|
71
|
+
if (!sitemapOptions && startConfig.pages.length) {
|
|
77
72
|
sitemapOptions = { enabled: true, outputPath: "sitemap.xml" };
|
|
78
73
|
}
|
|
79
|
-
if (!
|
|
74
|
+
if (!sitemapOptions?.enabled) {
|
|
80
75
|
throw new Error("Sitemap is not enabled");
|
|
81
76
|
}
|
|
82
77
|
const { host, outputPath } = sitemapOptions;
|
|
83
78
|
if (!host) {
|
|
84
|
-
if (!
|
|
79
|
+
if (!startConfig.sitemap) {
|
|
85
80
|
logger.info(
|
|
86
81
|
"Hint: Pages found, but no sitemap host has been set. To enable sitemap generation, set the `sitemap.host` option."
|
|
87
82
|
);
|
|
@@ -94,7 +89,7 @@ function buildSitemap({
|
|
|
94
89
|
if (!outputPath) {
|
|
95
90
|
throw new Error("Sitemap output path is not set");
|
|
96
91
|
}
|
|
97
|
-
const { pages } =
|
|
92
|
+
const { pages } = startConfig;
|
|
98
93
|
if (!pages.length) {
|
|
99
94
|
logger.info("No pages were found to build the sitemap. Skipping...");
|
|
100
95
|
return;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"build-sitemap.js","sources":["../../src/build-sitemap.ts"],"sourcesContent":["import { writeFileSync } from 'node:fs'\nimport path from 'node:path'\nimport { create } from 'xmlbuilder2'\nimport { createLogger } from './utils'\nimport type { TanStackStartOutputConfig } from './schema'\nimport type { XMLBuilder } from 'xmlbuilder2/lib/interfaces'\n\nexport type SitemapUrl = {\n loc: string\n lastmod: string\n priority?: number\n changefreq?:\n | 'always'\n | 'hourly'\n | 'daily'\n | 'weekly'\n | 'monthly'\n | 'yearly'\n | 'never'\n alternateRefs?: Array<{\n href: string\n hreflang?: string\n }>\n images?: Array<{\n loc: string\n title?: string\n caption?: string\n }>\n news?: {\n publication: {\n name: string\n language: string\n }\n publicationDate: string | Date\n title: string\n }\n}\n\nexport type SitemapData = {\n urls: Array<SitemapUrl>\n}\n\nfunction buildSitemapJson(\n pages: TanStackStartOutputConfig['pages'],\n host: string,\n): SitemapData {\n const slash = checkSlash(host)\n\n const urls: Array<SitemapUrl> = pages\n .filter((page) => {\n return page.sitemap?.exclude !== true\n })\n .map((page) => ({\n loc: `${host}${slash}${page.path.replace(/^\\/+/g, '')}`,\n lastmod: page.sitemap?.lastmod\n ? new Date(page.sitemap.lastmod).toISOString().split('T')[0]!\n : new Date().toISOString().split('T')[0]!,\n priority: page.sitemap?.priority,\n changefreq: page.sitemap?.changefreq,\n alternateRefs: page.sitemap?.alternateRefs,\n images: page.sitemap?.images,\n news: page.sitemap?.news,\n }))\n\n return { urls }\n}\n\nfunction jsonToXml(sitemapData: SitemapData): string {\n const sitemap = createXml('urlset')\n\n for (const item of sitemapData.urls) {\n const page = sitemap.ele('url')\n page.ele('loc').txt(item.loc)\n page.ele('lastmod').txt(item.lastmod)\n\n if (item.priority !== undefined) {\n page.ele('priority').txt(item.priority.toString())\n }\n if (item.changefreq) {\n page.ele('changefreq').txt(item.changefreq)\n }\n\n // Add alternate references\n if (item.alternateRefs?.length) {\n for (const ref of item.alternateRefs) {\n const alternateRef = page.ele('xhtml:link')\n alternateRef.att('rel', 'alternate')\n alternateRef.att('href', ref.href)\n if (ref.hreflang) {\n alternateRef.att('hreflang', ref.hreflang)\n }\n }\n }\n\n // Add images\n if (item.images?.length) {\n for (const image of item.images) {\n const imageElement = page.ele('image:image')\n imageElement.ele('image:loc').txt(image.loc)\n if (image.title) {\n imageElement.ele('image:title').txt(image.title)\n }\n if (image.caption) {\n imageElement.ele('image:caption').txt(image.caption)\n }\n }\n }\n\n // Add news\n if (item.news) {\n const newsElement = page.ele('news:news')\n const publication = newsElement.ele('news:publication')\n publication.ele('news:name').txt(item.news.publication.name)\n publication.ele('news:language').txt(item.news.publication.language)\n newsElement\n .ele('news:publication_date')\n .txt(new Date(item.news.publicationDate).toISOString().split('T')[0]!)\n newsElement.ele('news:title').txt(item.news.title)\n }\n }\n\n return sitemap.end({ prettyPrint: true })\n}\n\nexport function buildSitemap({\n startConfig,\n publicDir,\n}: {\n startConfig: TanStackStartOutputConfig\n publicDir: string\n}) {\n const logger = createLogger('sitemap')\n\n let sitemapOptions = startConfig.sitemap\n\n if (!sitemapOptions && startConfig.pages.length) {\n sitemapOptions = { enabled: true, outputPath: 'sitemap.xml' }\n }\n\n if (!sitemapOptions?.enabled) {\n throw new Error('Sitemap is not enabled')\n }\n\n const { host, outputPath } = sitemapOptions\n\n if (!host) {\n if (!startConfig.sitemap) {\n logger.info(\n 'Hint: Pages found, but no sitemap host has been set. To enable sitemap generation, set the `sitemap.host` option.',\n )\n return\n }\n throw new Error(\n 'Sitemap host is not set and required to build the sitemap.',\n )\n }\n\n if (!outputPath) {\n throw new Error('Sitemap output path is not set')\n }\n\n const { pages } = startConfig\n\n if (!pages.length) {\n logger.info('No pages were found to build the sitemap. Skipping...')\n return\n }\n\n logger.info('Building Sitemap...')\n\n // Build the sitemap data\n const sitemapData = buildSitemapJson(pages, host)\n\n // Generate output paths\n const xmlOutputPath = path.join(publicDir, outputPath)\n const pagesOutputPath = path.join(publicDir, 'pages.json')\n\n try {\n // Write XML sitemap\n logger.info(`Writing sitemap XML at ${xmlOutputPath}`)\n writeFileSync(xmlOutputPath, jsonToXml(sitemapData))\n\n // Write pages data for runtime use\n logger.info(`Writing pages data at ${pagesOutputPath}`)\n writeFileSync(\n pagesOutputPath,\n JSON.stringify(\n {\n pages,\n host,\n lastBuilt: new Date().toISOString(),\n },\n null,\n 2,\n ),\n )\n } catch (e) {\n logger.error(`Unable to write sitemap files`, e)\n }\n}\n\nfunction createXml(elementName: 'urlset' | 'sitemapindex'): XMLBuilder {\n return create({ version: '1.0', encoding: 'UTF-8' })\n .ele(elementName, {\n xmlns: 'https://www.sitemaps.org/schemas/sitemap/0.9',\n })\n .com(`This file was automatically generated by TanStack Start.`)\n}\n\nfunction checkSlash(host: string): string {\n const finalChar = host.slice(-1)\n return finalChar === '/' ? '' : '/'\n}\n"],"names":[],"mappings":";;;;AA0CA,SAAS,iBACP,OACA,MACa;AACb,QAAM,QAAQ,WAAW,IAAI;AAE7B,QAAM,OAA0B,MAC7B,OAAO,CAAC,SAAS;AAChB,WAAO,KAAK,SAAS,YAAY;AAAA,EACnC,CAAC,EACA,IAAI,CAAC,UAAU;AAAA,IACd,KAAK,GAAG,IAAI,GAAG,KAAK,GAAG,KAAK,KAAK,QAAQ,SAAS,EAAE,CAAC;AAAA,IACrD,SAAS,KAAK,SAAS,UACnB,IAAI,KAAK,KAAK,QAAQ,OAAO,EAAE,YAAA,EAAc,MAAM,GAAG,EAAE,CAAC,KACzD,oBAAI,KAAA,GAAO,YAAA,EAAc,MAAM,GAAG,EAAE,CAAC;AAAA,IACzC,UAAU,KAAK,SAAS;AAAA,IACxB,YAAY,KAAK,SAAS;AAAA,IAC1B,eAAe,KAAK,SAAS;AAAA,IAC7B,QAAQ,KAAK,SAAS;AAAA,IACtB,MAAM,KAAK,SAAS;AAAA,EAAA,EACpB;AAEJ,SAAO,EAAE,KAAA;AACX;AAEA,SAAS,UAAU,aAAkC;AACnD,QAAM,UAAU,UAAU,QAAQ;AAElC,aAAW,QAAQ,YAAY,MAAM;AACnC,UAAM,OAAO,QAAQ,IAAI,KAAK;AAC9B,SAAK,IAAI,KAAK,EAAE,IAAI,KAAK,GAAG;AAC5B,SAAK,IAAI,SAAS,EAAE,IAAI,KAAK,OAAO;AAEpC,QAAI,KAAK,aAAa,QAAW;AAC/B,WAAK,IAAI,UAAU,EAAE,IAAI,KAAK,SAAS,UAAU;AAAA,IACnD;AACA,QAAI,KAAK,YAAY;AACnB,WAAK,IAAI,YAAY,EAAE,IAAI,KAAK,UAAU;AAAA,IAC5C;AAGA,QAAI,KAAK,eAAe,QAAQ;AAC9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AAK;AAClC;"}
|
package/dist/esm/compilers.js
CHANGED
|
@@ -84,9 +84,8 @@ function compileStartOutputFactory(framework) {
|
|
|
84
84
|
},
|
|
85
85
|
CallExpression: (path) => {
|
|
86
86
|
identifierKeys.forEach((identifierKey) => {
|
|
87
|
-
var _a;
|
|
88
87
|
if (t.isIdentifier(path.node.callee) && path.node.callee.name === identifiers[identifierKey].name) {
|
|
89
|
-
if (
|
|
88
|
+
if (path.scope.getBinding(identifiers[identifierKey].name)?.path.node.type === "FunctionDeclaration") {
|
|
90
89
|
return;
|
|
91
90
|
}
|
|
92
91
|
return identifiers[identifierKey].paths.push(path);
|
|
@@ -145,9 +144,9 @@ function handleCreateServerFileRouteCallExpressionFactory(factory, method) {
|
|
|
145
144
|
const importSource = importPath.node.source.value;
|
|
146
145
|
if (importSource === PACKAGES.start) {
|
|
147
146
|
const specifiers = importPath.node.specifiers;
|
|
148
|
-
isCreateServerFileRouteImported
|
|
147
|
+
isCreateServerFileRouteImported ||= specifiers.some((specifier) => {
|
|
149
148
|
return t.isImportSpecifier(specifier) && t.isIdentifier(specifier.imported) && specifier.imported.name === method;
|
|
150
|
-
})
|
|
149
|
+
});
|
|
151
150
|
}
|
|
152
151
|
}
|
|
153
152
|
});
|
|
@@ -163,11 +162,10 @@ function handleCreateServerFileRouteCallExpressionFactory(factory, method) {
|
|
|
163
162
|
const handleServerOnlyCallExpression = buildEnvOnlyCallExpressionHandler("server");
|
|
164
163
|
const handleClientOnlyCallExpression = buildEnvOnlyCallExpressionHandler("client");
|
|
165
164
|
function handleCreateServerFnCallExpression(path, opts) {
|
|
166
|
-
var _a;
|
|
167
165
|
const calledOptions = path.node.arguments[0] ? path.get("arguments.0") : null;
|
|
168
|
-
const shouldValidateClient = !!
|
|
166
|
+
const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {
|
|
169
167
|
return t.isObjectProperty(prop) && t.isIdentifier(prop.key) && prop.key.name === "validateClient" && t.isBooleanLiteral(prop.value) && prop.value.value === true;
|
|
170
|
-
})
|
|
168
|
+
});
|
|
171
169
|
const callExpressionPaths = {
|
|
172
170
|
middleware: null,
|
|
173
171
|
validator: null,
|
|
@@ -203,7 +201,7 @@ function handleCreateServerFnCallExpression(path, opts) {
|
|
|
203
201
|
);
|
|
204
202
|
}
|
|
205
203
|
}
|
|
206
|
-
const handlerFnPath =
|
|
204
|
+
const handlerFnPath = callExpressionPaths.handler?.get(
|
|
207
205
|
"arguments.0"
|
|
208
206
|
);
|
|
209
207
|
if (!callExpressionPaths.handler || !handlerFnPath.node) {
|
|
@@ -245,7 +243,6 @@ function handleCreateServerFnCallExpression(path, opts) {
|
|
|
245
243
|
}
|
|
246
244
|
}
|
|
247
245
|
function handleCreateMiddlewareCallExpression(path, opts) {
|
|
248
|
-
var _a;
|
|
249
246
|
const rootCallExpression = getRootCallExpression(path);
|
|
250
247
|
const callExpressionPaths = {
|
|
251
248
|
middleware: null,
|
|
@@ -279,7 +276,7 @@ function handleCreateMiddlewareCallExpression(path, opts) {
|
|
|
279
276
|
}
|
|
280
277
|
}
|
|
281
278
|
}
|
|
282
|
-
const serverFnPath =
|
|
279
|
+
const serverFnPath = callExpressionPaths.server?.get(
|
|
283
280
|
"arguments.0"
|
|
284
281
|
);
|
|
285
282
|
if (callExpressionPaths.server && serverFnPath.node && opts.env === "client") {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compilers.js","sources":["../../src/compilers.ts"],"sourcesContent":["import * as babel from '@babel/core'\nimport * as t from '@babel/types'\nimport { codeFrameColumns } from '@babel/code-frame'\n\nimport {\n deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport { generateFromAst, parseAst } from '@tanstack/router-utils'\nimport type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'\n\nexport type CompileStartFrameworkOptions = 'react' | 'solid'\n\nexport function compileStartOutputFactory(\n framework: CompileStartFrameworkOptions,\n) {\n return function compileStartOutput(opts: CompileOptions): GeneratorResult {\n const ast = parseAst(opts)\n\n const doDce = opts.dce ?? true\n // find referenced identifiers *before* we transform anything\n const refIdents = doDce ? findReferencedIdentifiers(ast) : undefined\n\n babel.traverse(ast, {\n Program: {\n enter(programPath) {\n const identifiers: {\n createServerRoute: IdentifierConfig\n createServerFileRoute: IdentifierConfig\n createServerFn: IdentifierConfig\n createMiddleware: IdentifierConfig\n serverOnly: IdentifierConfig\n clientOnly: IdentifierConfig\n createIsomorphicFn: IdentifierConfig\n createServerRootRoute: IdentifierConfig\n } = {\n createServerRootRoute: {\n name: 'createServerRootRoute',\n handleCallExpression:\n handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRootRoute',\n ),\n paths: [],\n },\n createServerRoute: {\n name: 'createServerRoute',\n handleCallExpression:\n handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRoute',\n ),\n paths: [],\n },\n createServerFileRoute: {\n name: 'createServerFileRoute',\n handleCallExpression:\n handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerFileRoute',\n ),\n paths: [],\n },\n createServerFn: {\n name: 'createServerFn',\n handleCallExpression: handleCreateServerFnCallExpression,\n paths: [],\n },\n createMiddleware: {\n name: 'createMiddleware',\n handleCallExpression: handleCreateMiddlewareCallExpression,\n paths: [],\n },\n serverOnly: {\n name: 'serverOnly',\n handleCallExpression: handleServerOnlyCallExpression,\n paths: [],\n },\n clientOnly: {\n name: 'clientOnly',\n handleCallExpression: handleClientOnlyCallExpression,\n paths: [],\n },\n createIsomorphicFn: {\n name: 'createIsomorphicFn',\n handleCallExpression: handleCreateIsomorphicFnCallExpression,\n paths: [],\n },\n }\n\n const identifierKeys = Object.keys(identifiers) as Array<\n keyof typeof identifiers\n >\n\n programPath.traverse({\n ImportDeclaration: (path) => {\n if (path.node.source.value !== `@tanstack/${framework}-start`) {\n return\n }\n\n // handle a destructured imports being renamed like \"import { createServerFn as myCreateServerFn } from '@tanstack/react-start';\"\n path.node.specifiers.forEach((specifier) => {\n identifierKeys.forEach((identifierKey) => {\n const identifier = identifiers[identifierKey]\n\n if (\n specifier.type === 'ImportSpecifier' &&\n specifier.imported.type === 'Identifier'\n ) {\n if (specifier.imported.name === identifierKey) {\n identifier.name = specifier.local.name\n }\n }\n\n // handle namespace imports like \"import * as TanStackStart from '@tanstack/react-start';\"\n if (specifier.type === 'ImportNamespaceSpecifier') {\n identifier.name = `${specifier.local.name}.${identifierKey}`\n }\n })\n })\n },\n CallExpression: (path) => {\n identifierKeys.forEach((identifierKey) => {\n // Check to see if the call expression is a call to the\n // identifiers[identifierKey].name\n if (\n t.isIdentifier(path.node.callee) &&\n path.node.callee.name === identifiers[identifierKey].name\n ) {\n // The identifier could be a call to the original function\n // in the source code. If this is case, we need to ignore it.\n // Check the scope to see if the identifier is a function declaration.\n // if it is, then we can ignore it.\n\n if (\n path.scope.getBinding(identifiers[identifierKey].name)?.path\n .node.type === 'FunctionDeclaration'\n ) {\n return\n }\n\n return identifiers[identifierKey].paths.push(path)\n }\n\n if (t.isMemberExpression(path.node.callee)) {\n if (\n t.isIdentifier(path.node.callee.object) &&\n t.isIdentifier(path.node.callee.property)\n ) {\n const callname = [\n path.node.callee.object.name,\n path.node.callee.property.name,\n ].join('.')\n\n if (callname === identifiers[identifierKey].name) {\n identifiers[identifierKey].paths.push(path)\n }\n }\n }\n\n return\n })\n },\n })\n\n identifierKeys.forEach((identifierKey) => {\n identifiers[identifierKey].paths.forEach((path) => {\n identifiers[identifierKey].handleCallExpression(\n path as babel.NodePath<t.CallExpression>,\n opts,\n )\n })\n })\n },\n },\n })\n\n if (doDce) {\n deadCodeElimination(ast, refIdents)\n }\n\n return generateFromAst(ast, {\n sourceMaps: true,\n sourceFileName: opts.filename,\n filename: opts.filename,\n })\n }\n}\n\nfunction handleCreateServerFileRouteCallExpressionFactory(\n factory: CompileStartFrameworkOptions,\n method:\n | 'createServerFileRoute'\n | 'createServerRoute'\n | 'createServerRootRoute',\n) {\n return function handleCreateServerFileRouteCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n const PACKAGES = { start: `@tanstack/${factory}-start/server` }\n\n let highestParent: babel.NodePath<any> = path\n\n while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {\n highestParent = highestParent.parentPath\n }\n\n const programPath = highestParent.parentPath as babel.NodePath<t.Program>\n\n // If we're on the client, remove the entire variable\n if (opts.env === 'client') {\n // console.debug('createServerFileRoute -> manifest:\\n', manifest)\n highestParent.remove()\n return\n }\n\n let isCreateServerFileRouteImported = false as boolean\n\n programPath.traverse({\n ImportDeclaration(importPath) {\n const importSource = importPath.node.source.value\n if (importSource === PACKAGES.start) {\n const specifiers = importPath.node.specifiers\n isCreateServerFileRouteImported ||= specifiers.some((specifier) => {\n return (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported) &&\n specifier.imported.name === method\n )\n })\n }\n },\n })\n\n if (!isCreateServerFileRouteImported) {\n const importDeclaration = t.importDeclaration(\n [t.importSpecifier(t.identifier(method), t.identifier(method))],\n t.stringLiteral(PACKAGES.start),\n )\n programPath.node.body.unshift(importDeclaration)\n }\n }\n}\n\n// build these once and reuse them\nexport const handleServerOnlyCallExpression =\n buildEnvOnlyCallExpressionHandler('server')\nexport const handleClientOnlyCallExpression =\n buildEnvOnlyCallExpressionHandler('client')\n\nexport type CompileOptions = ParseAstOptions & {\n env: 'server' | 'client'\n dce?: boolean\n filename: string\n}\n\nexport type IdentifierConfig = {\n name: string\n handleCallExpression: (\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) => void\n paths: Array<babel.NodePath>\n}\n\nexport function handleCreateServerFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n // The function is the 'fn' property of the object passed to createServerFn\n\n // const firstArg = path.node.arguments[0]\n // if (t.isObjectExpression(firstArg)) {\n // // Was called with some options\n // }\n\n // Traverse the member expression and find the call expressions for\n // the validator, handler, and middleware methods. Check to make sure they\n // are children of the createServerFn call expression.\n\n const calledOptions = path.node.arguments[0]\n ? (path.get('arguments.0') as babel.NodePath<t.ObjectExpression>)\n : null\n\n const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {\n return (\n t.isObjectProperty(prop) &&\n t.isIdentifier(prop.key) &&\n prop.key.name === 'validateClient' &&\n t.isBooleanLiteral(prop.value) &&\n prop.value.value === true\n )\n })\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n handler: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createServerFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n // Check if the call is assigned to a variable\n if (!rootCallExpression.parentPath.isVariableDeclarator()) {\n throw new Error('createServerFn must be assigned to a variable!')\n }\n\n // Get the identifier name of the variable\n const variableDeclarator = rootCallExpression.parentPath.node\n const existingVariableName = (variableDeclarator.id as t.Identifier).name\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createServerFn().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, and we're not validating the client, remove the validator call expression\n if (\n opts.env === 'client' &&\n !shouldValidateClient &&\n t.isMemberExpression(callExpressionPaths.validator.node.callee)\n ) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n\n // First, we need to move the handler function to a nested function call\n // that is applied to the arguments passed to the server function.\n\n const handlerFnPath = callExpressionPaths.handler?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (!callExpressionPaths.handler || !handlerFnPath.node) {\n throw codeFrameError(\n opts.code,\n path.node.callee.loc!,\n `createServerFn must be called with a \"handler\" property!`,\n )\n }\n\n const handlerFn = handlerFnPath.node\n\n // So, the way we do this is we give the handler function a way\n // to access the serverFn ctx on the server via function scope.\n // The 'use server' extracted function will be called with the\n // payload from the client, then use the scoped serverFn ctx\n // to execute the handler function.\n // This way, we can do things like data and middleware validation\n // in the __execute function without having to AST transform the\n // handler function too much itself.\n\n // .handler((optsOut, ctx) => {\n // return ((optsIn) => {\n // 'use server'\n // ctx.__execute(handlerFn, optsIn)\n // })(optsOut)\n // })\n\n // If the handler function is an identifier and we're on the client, we need to\n // remove the bound function from the file.\n // If we're on the server, you can leave it, since it will get referenced\n // as a second argument.\n\n if (t.isIdentifier(handlerFn)) {\n if (opts.env === 'client') {\n // Find the binding for the handler function\n const binding = handlerFnPath.scope.getBinding(handlerFn.name)\n // Remove it\n if (binding) {\n binding.path.remove()\n }\n }\n // If the env is server, just leave it alone\n }\n\n handlerFnPath.replaceWith(\n t.arrowFunctionExpression(\n [t.identifier('opts'), t.identifier('signal')],\n t.blockStatement(\n // Everything in here is server-only, since the client\n // will strip out anything in the 'use server' directive.\n [\n t.returnStatement(\n t.callExpression(\n t.identifier(`${existingVariableName}.__executeServer`),\n [t.identifier('opts'), t.identifier('signal')],\n ),\n ),\n ],\n [t.directive(t.directiveLiteral('use server'))],\n ),\n ),\n )\n\n if (opts.env === 'server') {\n callExpressionPaths.handler.node.arguments.push(handlerFn)\n }\n}\n\nexport function handleCreateMiddlewareCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createMiddleware call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createMiddleware().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, remove the validator call expression\n if (opts.env === 'client') {\n if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n }\n\n const serverFnPath = callExpressionPaths.server?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (\n callExpressionPaths.server &&\n serverFnPath.node &&\n opts.env === 'client'\n ) {\n // If we're on the client, remove the server call expression\n if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {\n callExpressionPaths.server.replaceWith(\n callExpressionPaths.server.node.callee.object,\n )\n }\n }\n}\n\nfunction buildEnvOnlyCallExpressionHandler(env: 'client' | 'server') {\n return function envOnlyCallExpressionHandler(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n // if (debug)\n // console.info(`Handling ${env}Only call expression:`, path.toString())\n\n const isEnvMatch =\n env === 'client' ? opts.env === 'client' : opts.env === 'server'\n\n if (isEnvMatch) {\n // extract the inner function from the call expression\n const innerInputExpression = path.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `${env}Only() functions must be called with a function!`,\n )\n }\n\n path.replaceWith(innerInputExpression)\n return\n }\n\n // If we're on the wrong environment, replace the call expression\n // with a function that always throws an error.\n path.replaceWith(\n t.arrowFunctionExpression(\n [],\n t.blockStatement([\n t.throwStatement(\n t.newExpression(t.identifier('Error'), [\n t.stringLiteral(\n `${env}Only() functions can only be called on the ${env}!`,\n ),\n ]),\n ),\n ]),\n ),\n )\n }\n}\n\nexport function handleCreateIsomorphicFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createIsomorphicFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (\n validMethods.every(\n (method) =>\n !callExpressionPaths[method as keyof typeof callExpressionPaths],\n )\n ) {\n const variableId = rootCallExpression.parentPath.isVariableDeclarator()\n ? rootCallExpression.parentPath.node.id\n : null\n console.warn(\n 'createIsomorphicFn called without a client or server implementation!',\n 'This will result in a no-op function.',\n 'Variable name:',\n t.isIdentifier(variableId) ? variableId.name : 'unknown',\n )\n }\n\n const envCallExpression = callExpressionPaths[opts.env]\n\n if (!envCallExpression) {\n // if we don't have an implementation for this environment, default to a no-op\n rootCallExpression.replaceWith(\n t.arrowFunctionExpression([], t.blockStatement([])),\n )\n return\n }\n\n const innerInputExpression = envCallExpression.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `createIsomorphicFn().${opts.env}(func) must be called with a function!`,\n )\n }\n\n rootCallExpression.replaceWith(innerInputExpression)\n}\n\nexport function getRootCallExpression(path: babel.NodePath<t.CallExpression>) {\n // Find the highest callExpression parent\n let rootCallExpression: babel.NodePath<t.CallExpression> = path\n\n // Traverse up the chain of CallExpressions\n while (rootCallExpression.parentPath.isMemberExpression()) {\n const parent = rootCallExpression.parentPath\n if (parent.parentPath.isCallExpression()) {\n rootCallExpression = parent.parentPath\n }\n }\n\n return rootCallExpression\n}\n\nfunction codeFrameError(\n code: string,\n loc: {\n start: { line: number; column: number }\n end: { line: number; column: number }\n },\n message: string,\n) {\n const frame = codeFrameColumns(\n code,\n {\n start: loc.start,\n end: loc.end,\n },\n {\n highlightCode: true,\n message,\n },\n )\n\n return new Error(frame)\n}\n"],"names":[],"mappings":";;;;;AAaO,SAAS,0BACd,WACA;AACO,SAAA,SAAS,mBAAmB,MAAuC;AAClE,UAAA,MAAM,SAAS,IAAI;AAEnB,UAAA,QAAQ,KAAK,OAAO;AAE1B,UAAM,YAAY,QAAQ,0BAA0B,GAAG,IAAI;AAE3D,UAAM,SAAS,KAAK;AAAA,MAClB,SAAS;AAAA,QACP,MAAM,aAAa;AACjB,gBAAM,cASF;AAAA,YACF,uBAAuB;AAAA,cACrB,MAAM;AAAA,cACN,sBACE;AAAA,gBACE;AAAA,gBACA;AAAA,cACF;AAAA,cACF,OAAO,CAAA;AAAA,YACT;AAAA,YACA,mBAAmB;AAAA,cACjB,MAAM;AAAA,cACN,sBACE;AAAA,gBACE;AAAA,gBACA;AAAA,cACF;AAAA,cACF,OAAO,CAAA;AAAA,YACT;AAAA,YACA,uBAAuB;AAAA,cACrB,MAAM;AAAA,cACN,sBACE;AAAA,gBACE;AAAA,gBACA;AAAA,cACF;AAAA,cACF,OAAO,CAAA;AAAA,YACT;AAAA,YACA,gBAAgB;AAAA,cACd,MAAM;AAAA,cACN,sBAAsB;AAAA,cACtB,OAAO,CAAA;AAAA,YACT;AAAA,YACA,kBAAkB;AAAA,cAChB,MAAM;AAAA,cACN,sBAAsB;AAAA,cACtB,OAAO,CAAA;AAAA,YACT;AAAA,YACA,YAAY;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* as babel from '@babel/core'\nimport * as t from '@babel/types'\nimport { codeFrameColumns } from '@babel/code-frame'\n\nimport {\n deadCodeElimination,\n findReferencedIdentifiers,\n} from 'babel-dead-code-elimination'\nimport { generateFromAst, parseAst } from '@tanstack/router-utils'\nimport type { GeneratorResult, ParseAstOptions } from '@tanstack/router-utils'\n\nexport type CompileStartFrameworkOptions = 'react' | 'solid'\n\nexport function compileStartOutputFactory(\n framework: CompileStartFrameworkOptions,\n) {\n return function compileStartOutput(opts: CompileOptions): GeneratorResult {\n const ast = parseAst(opts)\n\n const doDce = opts.dce ?? true\n // find referenced identifiers *before* we transform anything\n const refIdents = doDce ? findReferencedIdentifiers(ast) : undefined\n\n babel.traverse(ast, {\n Program: {\n enter(programPath) {\n const identifiers: {\n createServerRoute: IdentifierConfig\n createServerFileRoute: IdentifierConfig\n createServerFn: IdentifierConfig\n createMiddleware: IdentifierConfig\n serverOnly: IdentifierConfig\n clientOnly: IdentifierConfig\n createIsomorphicFn: IdentifierConfig\n createServerRootRoute: IdentifierConfig\n } = {\n createServerRootRoute: {\n name: 'createServerRootRoute',\n handleCallExpression:\n handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRootRoute',\n ),\n paths: [],\n },\n createServerRoute: {\n name: 'createServerRoute',\n handleCallExpression:\n handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerRoute',\n ),\n paths: [],\n },\n createServerFileRoute: {\n name: 'createServerFileRoute',\n handleCallExpression:\n handleCreateServerFileRouteCallExpressionFactory(\n framework,\n 'createServerFileRoute',\n ),\n paths: [],\n },\n createServerFn: {\n name: 'createServerFn',\n handleCallExpression: handleCreateServerFnCallExpression,\n paths: [],\n },\n createMiddleware: {\n name: 'createMiddleware',\n handleCallExpression: handleCreateMiddlewareCallExpression,\n paths: [],\n },\n serverOnly: {\n name: 'serverOnly',\n handleCallExpression: handleServerOnlyCallExpression,\n paths: [],\n },\n clientOnly: {\n name: 'clientOnly',\n handleCallExpression: handleClientOnlyCallExpression,\n paths: [],\n },\n createIsomorphicFn: {\n name: 'createIsomorphicFn',\n handleCallExpression: handleCreateIsomorphicFnCallExpression,\n paths: [],\n },\n }\n\n const identifierKeys = Object.keys(identifiers) as Array<\n keyof typeof identifiers\n >\n\n programPath.traverse({\n ImportDeclaration: (path) => {\n if (path.node.source.value !== `@tanstack/${framework}-start`) {\n return\n }\n\n // handle a destructured imports being renamed like \"import { createServerFn as myCreateServerFn } from '@tanstack/react-start';\"\n path.node.specifiers.forEach((specifier) => {\n identifierKeys.forEach((identifierKey) => {\n const identifier = identifiers[identifierKey]\n\n if (\n specifier.type === 'ImportSpecifier' &&\n specifier.imported.type === 'Identifier'\n ) {\n if (specifier.imported.name === identifierKey) {\n identifier.name = specifier.local.name\n }\n }\n\n // handle namespace imports like \"import * as TanStackStart from '@tanstack/react-start';\"\n if (specifier.type === 'ImportNamespaceSpecifier') {\n identifier.name = `${specifier.local.name}.${identifierKey}`\n }\n })\n })\n },\n CallExpression: (path) => {\n identifierKeys.forEach((identifierKey) => {\n // Check to see if the call expression is a call to the\n // identifiers[identifierKey].name\n if (\n t.isIdentifier(path.node.callee) &&\n path.node.callee.name === identifiers[identifierKey].name\n ) {\n // The identifier could be a call to the original function\n // in the source code. If this is case, we need to ignore it.\n // Check the scope to see if the identifier is a function declaration.\n // if it is, then we can ignore it.\n\n if (\n path.scope.getBinding(identifiers[identifierKey].name)?.path\n .node.type === 'FunctionDeclaration'\n ) {\n return\n }\n\n return identifiers[identifierKey].paths.push(path)\n }\n\n if (t.isMemberExpression(path.node.callee)) {\n if (\n t.isIdentifier(path.node.callee.object) &&\n t.isIdentifier(path.node.callee.property)\n ) {\n const callname = [\n path.node.callee.object.name,\n path.node.callee.property.name,\n ].join('.')\n\n if (callname === identifiers[identifierKey].name) {\n identifiers[identifierKey].paths.push(path)\n }\n }\n }\n\n return\n })\n },\n })\n\n identifierKeys.forEach((identifierKey) => {\n identifiers[identifierKey].paths.forEach((path) => {\n identifiers[identifierKey].handleCallExpression(\n path as babel.NodePath<t.CallExpression>,\n opts,\n )\n })\n })\n },\n },\n })\n\n if (doDce) {\n deadCodeElimination(ast, refIdents)\n }\n\n return generateFromAst(ast, {\n sourceMaps: true,\n sourceFileName: opts.filename,\n filename: opts.filename,\n })\n }\n}\n\nfunction handleCreateServerFileRouteCallExpressionFactory(\n factory: CompileStartFrameworkOptions,\n method:\n | 'createServerFileRoute'\n | 'createServerRoute'\n | 'createServerRootRoute',\n) {\n return function handleCreateServerFileRouteCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n const PACKAGES = { start: `@tanstack/${factory}-start/server` }\n\n let highestParent: babel.NodePath<any> = path\n\n while (highestParent.parentPath && !highestParent.parentPath.isProgram()) {\n highestParent = highestParent.parentPath\n }\n\n const programPath = highestParent.parentPath as babel.NodePath<t.Program>\n\n // If we're on the client, remove the entire variable\n if (opts.env === 'client') {\n // console.debug('createServerFileRoute -> manifest:\\n', manifest)\n highestParent.remove()\n return\n }\n\n let isCreateServerFileRouteImported = false as boolean\n\n programPath.traverse({\n ImportDeclaration(importPath) {\n const importSource = importPath.node.source.value\n if (importSource === PACKAGES.start) {\n const specifiers = importPath.node.specifiers\n isCreateServerFileRouteImported ||= specifiers.some((specifier) => {\n return (\n t.isImportSpecifier(specifier) &&\n t.isIdentifier(specifier.imported) &&\n specifier.imported.name === method\n )\n })\n }\n },\n })\n\n if (!isCreateServerFileRouteImported) {\n const importDeclaration = t.importDeclaration(\n [t.importSpecifier(t.identifier(method), t.identifier(method))],\n t.stringLiteral(PACKAGES.start),\n )\n programPath.node.body.unshift(importDeclaration)\n }\n }\n}\n\n// build these once and reuse them\nexport const handleServerOnlyCallExpression =\n buildEnvOnlyCallExpressionHandler('server')\nexport const handleClientOnlyCallExpression =\n buildEnvOnlyCallExpressionHandler('client')\n\nexport type CompileOptions = ParseAstOptions & {\n env: 'server' | 'client'\n dce?: boolean\n filename: string\n}\n\nexport type IdentifierConfig = {\n name: string\n handleCallExpression: (\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) => void\n paths: Array<babel.NodePath>\n}\n\nexport function handleCreateServerFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n // The function is the 'fn' property of the object passed to createServerFn\n\n // const firstArg = path.node.arguments[0]\n // if (t.isObjectExpression(firstArg)) {\n // // Was called with some options\n // }\n\n // Traverse the member expression and find the call expressions for\n // the validator, handler, and middleware methods. Check to make sure they\n // are children of the createServerFn call expression.\n\n const calledOptions = path.node.arguments[0]\n ? (path.get('arguments.0') as babel.NodePath<t.ObjectExpression>)\n : null\n\n const shouldValidateClient = !!calledOptions?.node.properties.find((prop) => {\n return (\n t.isObjectProperty(prop) &&\n t.isIdentifier(prop.key) &&\n prop.key.name === 'validateClient' &&\n t.isBooleanLiteral(prop.value) &&\n prop.value.value === true\n )\n })\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n handler: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createServerFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n // Check if the call is assigned to a variable\n if (!rootCallExpression.parentPath.isVariableDeclarator()) {\n throw new Error('createServerFn must be assigned to a variable!')\n }\n\n // Get the identifier name of the variable\n const variableDeclarator = rootCallExpression.parentPath.node\n const existingVariableName = (variableDeclarator.id as t.Identifier).name\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createServerFn().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, and we're not validating the client, remove the validator call expression\n if (\n opts.env === 'client' &&\n !shouldValidateClient &&\n t.isMemberExpression(callExpressionPaths.validator.node.callee)\n ) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n\n // First, we need to move the handler function to a nested function call\n // that is applied to the arguments passed to the server function.\n\n const handlerFnPath = callExpressionPaths.handler?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (!callExpressionPaths.handler || !handlerFnPath.node) {\n throw codeFrameError(\n opts.code,\n path.node.callee.loc!,\n `createServerFn must be called with a \"handler\" property!`,\n )\n }\n\n const handlerFn = handlerFnPath.node\n\n // So, the way we do this is we give the handler function a way\n // to access the serverFn ctx on the server via function scope.\n // The 'use server' extracted function will be called with the\n // payload from the client, then use the scoped serverFn ctx\n // to execute the handler function.\n // This way, we can do things like data and middleware validation\n // in the __execute function without having to AST transform the\n // handler function too much itself.\n\n // .handler((optsOut, ctx) => {\n // return ((optsIn) => {\n // 'use server'\n // ctx.__execute(handlerFn, optsIn)\n // })(optsOut)\n // })\n\n // If the handler function is an identifier and we're on the client, we need to\n // remove the bound function from the file.\n // If we're on the server, you can leave it, since it will get referenced\n // as a second argument.\n\n if (t.isIdentifier(handlerFn)) {\n if (opts.env === 'client') {\n // Find the binding for the handler function\n const binding = handlerFnPath.scope.getBinding(handlerFn.name)\n // Remove it\n if (binding) {\n binding.path.remove()\n }\n }\n // If the env is server, just leave it alone\n }\n\n handlerFnPath.replaceWith(\n t.arrowFunctionExpression(\n [t.identifier('opts'), t.identifier('signal')],\n t.blockStatement(\n // Everything in here is server-only, since the client\n // will strip out anything in the 'use server' directive.\n [\n t.returnStatement(\n t.callExpression(\n t.identifier(`${existingVariableName}.__executeServer`),\n [t.identifier('opts'), t.identifier('signal')],\n ),\n ),\n ],\n [t.directive(t.directiveLiteral('use server'))],\n ),\n ),\n )\n\n if (opts.env === 'server') {\n callExpressionPaths.handler.node.arguments.push(handlerFn)\n }\n}\n\nexport function handleCreateMiddlewareCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createMiddleware call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n middleware: null as babel.NodePath<t.CallExpression> | null,\n validator: null as babel.NodePath<t.CallExpression> | null,\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (callExpressionPaths.validator) {\n const innerInputExpression = callExpressionPaths.validator.node.arguments[0]\n\n if (!innerInputExpression) {\n throw new Error(\n 'createMiddleware().validator() must be called with a validator!',\n )\n }\n\n // If we're on the client, remove the validator call expression\n if (opts.env === 'client') {\n if (t.isMemberExpression(callExpressionPaths.validator.node.callee)) {\n callExpressionPaths.validator.replaceWith(\n callExpressionPaths.validator.node.callee.object,\n )\n }\n }\n }\n\n const serverFnPath = callExpressionPaths.server?.get(\n 'arguments.0',\n ) as babel.NodePath<any>\n\n if (\n callExpressionPaths.server &&\n serverFnPath.node &&\n opts.env === 'client'\n ) {\n // If we're on the client, remove the server call expression\n if (t.isMemberExpression(callExpressionPaths.server.node.callee)) {\n callExpressionPaths.server.replaceWith(\n callExpressionPaths.server.node.callee.object,\n )\n }\n }\n}\n\nfunction buildEnvOnlyCallExpressionHandler(env: 'client' | 'server') {\n return function envOnlyCallExpressionHandler(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n ) {\n // if (debug)\n // console.info(`Handling ${env}Only call expression:`, path.toString())\n\n const isEnvMatch =\n env === 'client' ? opts.env === 'client' : opts.env === 'server'\n\n if (isEnvMatch) {\n // extract the inner function from the call expression\n const innerInputExpression = path.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `${env}Only() functions must be called with a function!`,\n )\n }\n\n path.replaceWith(innerInputExpression)\n return\n }\n\n // If we're on the wrong environment, replace the call expression\n // with a function that always throws an error.\n path.replaceWith(\n t.arrowFunctionExpression(\n [],\n t.blockStatement([\n t.throwStatement(\n t.newExpression(t.identifier('Error'), [\n t.stringLiteral(\n `${env}Only() functions can only be called on the ${env}!`,\n ),\n ]),\n ),\n ]),\n ),\n )\n }\n}\n\nexport function handleCreateIsomorphicFnCallExpression(\n path: babel.NodePath<t.CallExpression>,\n opts: CompileOptions,\n) {\n const rootCallExpression = getRootCallExpression(path)\n\n // if (debug)\n // console.info(\n // 'Handling createIsomorphicFn call expression:',\n // rootCallExpression.toString(),\n // )\n\n const callExpressionPaths = {\n client: null as babel.NodePath<t.CallExpression> | null,\n server: null as babel.NodePath<t.CallExpression> | null,\n }\n\n const validMethods = Object.keys(callExpressionPaths)\n\n rootCallExpression.traverse({\n MemberExpression(memberExpressionPath) {\n if (t.isIdentifier(memberExpressionPath.node.property)) {\n const name = memberExpressionPath.node.property\n .name as keyof typeof callExpressionPaths\n\n if (\n validMethods.includes(name) &&\n memberExpressionPath.parentPath.isCallExpression()\n ) {\n callExpressionPaths[name] = memberExpressionPath.parentPath\n }\n }\n },\n })\n\n if (\n validMethods.every(\n (method) =>\n !callExpressionPaths[method as keyof typeof callExpressionPaths],\n )\n ) {\n const variableId = rootCallExpression.parentPath.isVariableDeclarator()\n ? rootCallExpression.parentPath.node.id\n : null\n console.warn(\n 'createIsomorphicFn called without a client or server implementation!',\n 'This will result in a no-op function.',\n 'Variable name:',\n t.isIdentifier(variableId) ? variableId.name : 'unknown',\n )\n }\n\n const envCallExpression = callExpressionPaths[opts.env]\n\n if (!envCallExpression) {\n // if we don't have an implementation for this environment, default to a no-op\n rootCallExpression.replaceWith(\n t.arrowFunctionExpression([], t.blockStatement([])),\n )\n return\n }\n\n const innerInputExpression = envCallExpression.node.arguments[0]\n\n if (!t.isExpression(innerInputExpression)) {\n throw new Error(\n `createIsomorphicFn().${opts.env}(func) must be called with a function!`,\n )\n }\n\n rootCallExpression.replaceWith(innerInputExpression)\n}\n\nexport function getRootCallExpression(path: babel.NodePath<t.CallExpression>) {\n // Find the highest callExpression parent\n let rootCallExpression: babel.NodePath<t.CallExpression> = path\n\n // Traverse up the chain of CallExpressions\n while (rootCallExpression.parentPath.isMemberExpression()) {\n const parent = rootCallExpression.parentPath\n if (parent.parentPath.isCallExpression()) {\n rootCallExpression = parent.parentPath\n }\n }\n\n return rootCallExpression\n}\n\nfunction codeFrameError(\n code: string,\n loc: {\n start: { line: number; column: number }\n end: { line: number; column: number }\n },\n message: string,\n) {\n const frame = codeFrameColumns(\n code,\n {\n start: loc.start,\n end: loc.end,\n },\n {\n highlightCode: true,\n message,\n },\n )\n\n return new Error(frame)\n}\n"],"names":[],"mappings":";;;;;AAaO,SAAS,0BACd,WACA;AACA,SAAO,SAAS,mBAAmB,MAAuC;AACxE,UAAM,MAAM,SAAS,IAAI;AAEzB,UAAM,QAAQ,KAAK,OAAO;AAE1B,UAAM,YAAY,QAAQ,0BAA0B,GAAG,IAAI;AAE3D,UAAM,SAAS,KAAK;AAAA,MAClB,SAAS;AAAA,QACP,MAAM,aAAa;AACjB,gBAAM,cASF;AAAA,YACF,uBAAuB;AAAA,cACrB,MAAM;AAAA,cACN,sBACE;AAAA,gBACE;AAAA,gBACA;AAAA,cAAA;AAAA,cAEJ,OAAO,CAAA;AAAA,YAAC;AAAA,YAEV,mBAAmB;AAAA,cACjB,MAAM;AAAA,cACN,sBACE;AAAA,gBACE;AAAA,gBACA;AAAA,cAAA;AAAA,cAEJ,OAA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AA,IAC9B;AAAA,EACF;AAEA,SAAO;AACT;AAEA,SAAS,eACP,MACA,KAIA,SACA;AACA,QAAM,QAAQ;AAAA,IACZ;AAAA,IACA;AAAA,MACE,OAAO,IAAI;AAAA,MACX,KAAK,IAAI;AAAA,IAAA;AAAA,IAEX;AAAA,MACE,eAAe;AAAA,MACf;AAAA,IAAA;AAAA,EACF;AAGF,SAAO,IAAI,MAAM,KAAK;AACxB;"}
|
package/dist/esm/constants.d.ts
CHANGED
|
@@ -2,5 +2,9 @@ export declare const VITE_ENVIRONMENT_NAMES: {
|
|
|
2
2
|
readonly server: "ssr";
|
|
3
3
|
readonly client: "client";
|
|
4
4
|
};
|
|
5
|
-
export
|
|
6
|
-
export declare const
|
|
5
|
+
export type ViteEnvironmentNames = (typeof VITE_ENVIRONMENT_NAMES)[keyof typeof VITE_ENVIRONMENT_NAMES];
|
|
6
|
+
export declare const ENTRY_POINTS: {
|
|
7
|
+
readonly client: "virtual:tanstack-start-client-entry";
|
|
8
|
+
readonly server: "virtual:tanstack-start-server-request-entry";
|
|
9
|
+
readonly router: "#tanstack-start-router-entry";
|
|
10
|
+
};
|
package/dist/esm/constants.js
CHANGED
|
@@ -1,20 +1,17 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
1
|
const VITE_ENVIRONMENT_NAMES = {
|
|
3
2
|
// 'ssr' is chosen as the name for the server environment to ensure backwards compatibility
|
|
4
3
|
// with vite plugins that are not compatible with the new vite environment API (e.g. tailwindcss)
|
|
5
4
|
server: "ssr",
|
|
6
5
|
client: "client"
|
|
7
6
|
};
|
|
8
|
-
const
|
|
9
|
-
"
|
|
10
|
-
"start",
|
|
11
|
-
|
|
12
|
-
"
|
|
13
|
-
|
|
14
|
-
const SSR_ENTRY_FILE = "ssr.mjs";
|
|
7
|
+
const ENTRY_POINTS = {
|
|
8
|
+
client: "virtual:tanstack-start-client-entry",
|
|
9
|
+
server: "virtual:tanstack-start-server-request-entry",
|
|
10
|
+
// the router entry point must always be provided by the user
|
|
11
|
+
router: "#tanstack-start-router-entry"
|
|
12
|
+
};
|
|
15
13
|
export {
|
|
16
|
-
|
|
17
|
-
SSR_ENTRY_FILE,
|
|
14
|
+
ENTRY_POINTS,
|
|
18
15
|
VITE_ENVIRONMENT_NAMES
|
|
19
16
|
};
|
|
20
17
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.js","sources":["../../src/constants.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"constants.js","sources":["../../src/constants.ts"],"sourcesContent":["export const VITE_ENVIRONMENT_NAMES = {\n // 'ssr' is chosen as the name for the server environment to ensure backwards compatibility\n // with vite plugins that are not compatible with the new vite environment API (e.g. tailwindcss)\n server: 'ssr',\n client: 'client',\n} as const\n\nexport type ViteEnvironmentNames =\n (typeof VITE_ENVIRONMENT_NAMES)[keyof typeof VITE_ENVIRONMENT_NAMES]\n\n// for client and router:\n// if a user has a custom server/client entry point file, resolve.alias will point to this\n// otherwise it will be aliased to the default entry point in the respective framework plugin\nexport const ENTRY_POINTS = {\n client: 'virtual:tanstack-start-client-entry',\n server: 'virtual:tanstack-start-server-request-entry',\n // the router entry point must always be provided by the user\n router: '#tanstack-start-router-entry',\n} as const\n"],"names":[],"mappings":"AAAO,MAAM,yBAAyB;AAAA;AAAA;AAAA,EAGpC,QAAQ;AAAA,EACR,QAAQ;AACV;AAQO,MAAM,eAAe;AAAA,EAC1B,QAAQ;AAAA,EACR,QAAQ;AAAA;AAAA,EAER,QAAQ;AACV;"}
|
package/dist/esm/debug.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"debug.js","sources":["../../src/debug.ts"],"sourcesContent":["export const debug =\n process.env.TSR_VITE_DEBUG &&\n ['true', 'start-plugin-core'].includes(process.env.TSR_VITE_DEBUG)\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"debug.js","sources":["../../src/debug.ts"],"sourcesContent":["export const debug =\n process.env.TSR_VITE_DEBUG &&\n ['true', 'start-plugin-core'].includes(process.env.TSR_VITE_DEBUG)\n"],"names":[],"mappings":"AAAO,MAAM,QACX,QAAQ,IAAI,kBACZ,CAAC,QAAQ,mBAAmB,EAAE,SAAS,QAAQ,IAAI,cAAc;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract-html-scripts.js","sources":["../../../src/dev-server-plugin/extract-html-scripts.ts"],"sourcesContent":["import * as cheerio from 'cheerio'\n\nexport function extractHtmlScripts(\n html: string,\n): Array<{ content?: string; src?: string }> {\n const $ = cheerio.load(html)\n const scripts: Array<{ content?: string; src?: string }> = []\n\n $('script').each((_, element) => {\n const src = $(element).attr('src')\n const content = $(element).html() ?? undefined\n scripts.push({\n src,\n content,\n })\n })\n\n return scripts\n}\n"],"names":[],"mappings":";AAEO,SAAS,mBACd,MAC2C;
|
|
1
|
+
{"version":3,"file":"extract-html-scripts.js","sources":["../../../src/dev-server-plugin/extract-html-scripts.ts"],"sourcesContent":["import * as cheerio from 'cheerio'\n\nexport function extractHtmlScripts(\n html: string,\n): Array<{ content?: string; src?: string }> {\n const $ = cheerio.load(html)\n const scripts: Array<{ content?: string; src?: string }> = []\n\n $('script').each((_, element) => {\n const src = $(element).attr('src')\n const content = $(element).html() ?? undefined\n scripts.push({\n src,\n content,\n })\n })\n\n return scripts\n}\n"],"names":[],"mappings":";AAEO,SAAS,mBACd,MAC2C;AAC3C,QAAM,IAAI,QAAQ,KAAK,IAAI;AAC3B,QAAM,UAAqD,CAAA;AAE3D,IAAE,QAAQ,EAAE,KAAK,CAAC,GAAG,YAAY;AAC/B,UAAM,MAAM,EAAE,OAAO,EAAE,KAAK,KAAK;AACjC,UAAM,UAAU,EAAE,OAAO,EAAE,UAAU;AACrC,YAAQ,KAAK;AAAA,MACX;AAAA,MACA;AAAA,IAAA,CACD;AAAA,EACH,CAAC;AAED,SAAO;AACT;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { PluginOption } from 'vite';
|
|
2
|
+
import { TanStackStartOutputConfig } from '../schema.js';
|
|
3
|
+
export declare function devServerPlugin({ startConfig, }: {
|
|
4
|
+
startConfig: TanStackStartOutputConfig;
|
|
5
|
+
}): PluginOption;
|