Skip to content

Commit 32fdb6e

Browse files
committed
docs: use vite.define for template variables
1 parent 354df27 commit 32fdb6e

4 files changed

Lines changed: 9 additions & 20 deletions

File tree

docs/src/layouts/ComponentLayout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import ComponentApi from "../components/ComponentApi.svelte";
2828
import { theme } from "../store";
2929
30-
const REPO_URL = "REPO_URL";
30+
const REPO_URL = __REPO_URL__;
3131
3232
export let component = $activeRoute?.leaf?.node?.name ?? "";
3333
export let components = [component];

docs/src/pages/_module.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@
9898

9999
<span slot="platform" class="platform-name" class:hidden={active}>
100100
Carbon<span class="platform-name-full">&nbsp;Components</span>&nbsp;Svelte
101-
&nbsp;<code class="code-01">v{process.env.VERSION || ""}</code>
101+
&nbsp;<code class="code-01">v{__PKG_VERSION__}</code>
102102
</span>
103103
<HeaderUtilities>
104104
<HeaderSearch

docs/svelte.config.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import Prism from "prismjs";
1010
import rehypeSlug from "rehype-slug";
1111
import { parse } from "svelte/compiler";
1212
import visit from "unist-util-visit";
13-
import pkg from "../package.json" with { type: "json" };
1413
import componentApi from "./src/COMPONENT_API.json" with { type: "json" };
1514
import "prismjs/components/prism-markup.js";
1615
import "prismjs/components/prism-css.js";
@@ -28,8 +27,6 @@ const componentApiByName = new Set(
2827
const ICON_NAME_REGEX = /[A-Z][a-z]*/;
2928
const NODE_MODULES_REGEX = /node_modules/;
3029
const PAGES_COMPONENTS_REGEX = /pages\/(components)/;
31-
const GIT_PREFIX_REGEX = /^git\+/;
32-
const GIT_SUFFIX_REGEX = /\.git$/;
3330
const SCRIPT_TAG_REGEX = /(<script[^>]*>)/i;
3431
const FILE_SOURCE_SRC_REGEX = /src="([^"]+)"/;
3532

@@ -351,21 +348,6 @@ function carbonify() {
351348
export default {
352349
extensions: [".svelte", ".svx"],
353350
preprocess: [
354-
{
355-
markup: ({ filename, content }) => {
356-
if (NODE_MODULES_REGEX.test(filename) || !filename.endsWith(".svelte"))
357-
return;
358-
const repoUrl = pkg.repository?.url ?? "";
359-
const normalizedRepoUrl = repoUrl
360-
.replace(GIT_PREFIX_REGEX, "")
361-
.replace(GIT_SUFFIX_REGEX, "");
362-
return {
363-
code: content
364-
.replace(/process.env.VERSION/g, JSON.stringify(pkg.version))
365-
.replace(/"REPO_URL"/g, JSON.stringify(normalizedRepoUrl)),
366-
};
367-
},
368-
},
369351
mdsvex({
370352
smartypants: false,
371353
highlight: { highlighter: mdsvexPrismHighlighter },

docs/vite.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { fileURLToPath } from "node:url";
33
import routify from "@roxi/routify/vite-plugin";
44
import { svelte } from "@sveltejs/vite-plugin-svelte";
55
import { defineConfig } from "vite";
6+
import pkg from "../package.json" with { type: "json" };
67

78
const __dirname = path.dirname(fileURLToPath(import.meta.url));
89
const carbonRoot = path.resolve(__dirname, "..");
@@ -31,6 +32,12 @@ export default defineConfig({
3132
},
3233
},
3334
],
35+
define: {
36+
__PKG_VERSION__: JSON.stringify(pkg.version),
37+
__REPO_URL__: JSON.stringify(
38+
(pkg.repository?.url ?? "").replace(/^git\+/, "").replace(/\.git$/, ""),
39+
),
40+
},
3441
optimizeDeps: {
3542
include: ["clipboard-copy", "flatpickr/dist/plugins/rangePlugin"],
3643
exclude: [

0 commit comments

Comments
 (0)