Skip to content

Commit 61172df

Browse files
committed
docs: use embedded build for preview examples
1 parent e6d27a3 commit 61172df

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ versions.json
8686
!/docs/api/index.md
8787
/docs/api/api-search-index.json
8888
/docs/public/api-diff-index.json
89+
/docs/public/faker.js
8990

9091
# Faker
9192
TAGS

docs/.vitepress/config.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import type { DefaultTheme } from 'vitepress/theme';
33
import { apiPages } from './api-pages';
44
import {
55
algoliaIndex,
6+
commitHash,
7+
isReleaseBranch,
68
version,
79
versionBannerInfix,
810
versionLabel,
@@ -13,7 +15,13 @@ type SidebarItem = DefaultTheme.SidebarItem;
1315

1416
const description =
1517
'Generate massive amounts of fake (but reasonable) data for testing and development.';
16-
const image = 'https://fakerjs.dev/social-image.png';
18+
const socialImage = 'https://fakerjs.dev/social-image.png';
19+
const consoleDownload = isReleaseBranch
20+
? `https://cdn.jsdelivr.net/npm/@faker-js/faker@${version}/+esm`
21+
: '/faker.js';
22+
const consoleVersion = isReleaseBranch
23+
? version
24+
: `${version.replace(/-.*$/, '')}-preview+${commitHash}`;
1725

1826
function getSideBarWithExpandedEntry(entryToExpand: string): SidebarItem[] {
1927
const links: SidebarItem[] = [
@@ -111,12 +119,12 @@ const config: UserConfig<DefaultTheme.Config> = {
111119
['meta', { name: 'theme-color', content: '#40af7c' }],
112120
['meta', { name: 'og:title', content: 'FakerJS' }],
113121
['meta', { name: 'og:description', content: description }],
114-
['meta', { name: 'og:image', content: image }],
122+
['meta', { name: 'og:image', content: socialImage }],
115123
['meta', { name: 'twitter:card', content: 'summary_large_image' }],
116124
['meta', { name: 'twitter:title', content: 'FakerJS' }],
117125
['meta', { name: 'twitter:description', content: description }],
118126
['meta', { name: 'twitter:site', content: '@faker_js' }],
119-
['meta', { name: 'twitter:image', content: image }],
127+
['meta', { name: 'twitter:image', content: socialImage }],
120128
['meta', { name: 'twitter:image:alt', content: 'The FakerJS logo' }],
121129
['link', { rel: 'me', href: 'https://fosstodon.org/@faker_js' }],
122130
[
@@ -127,9 +135,9 @@ const logStyle = 'background: rgba(16, 183, 127, 0.14); color: rgba(255, 255, 24
127135
console.log(\`%cIf you would like to test Faker in the browser console, you can do so using 'await enableFaker()'.
128136
If you would like to test Faker in a playground, visit https://new.fakerjs.dev.\`, logStyle);
129137
async function enableFaker() {
130-
const imported = await import('https://cdn.jsdelivr.net/npm/@faker-js/faker@${version}/+esm');
138+
const imported = await import('${consoleDownload}');
131139
Object.assign(globalThis, imported);
132-
console.log(\`%cYou can now start using Faker v${version}:
140+
console.log(\`%cYou can now start using Faker v${consoleVersion}:
133141
e.g. 'faker.food.description()' or 'fakerZH_CN.person.firstName()'
134142
For other languages please refer to https://fakerjs.dev/guide/localization.html#available-locales
135143
For a full list of all methods please refer to https://fakerjs.dev/api/\`, logStyle);

docs/.vitepress/versions.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ function readBranchName(): string {
88
);
99
}
1010

11+
function readCommitHash(): string {
12+
return execSync('git rev-parse HEAD').toString('utf8').trim() || 'unknown';
13+
}
14+
1115
function readOtherLatestReleaseTagNames(): string[] {
1216
const tags = execSync('git tag -l').toString('utf8').split('\n');
1317
const latestTagByMajor: Record<string, string> = {};
@@ -34,10 +38,11 @@ function readOtherLatestReleaseTagNames(): string[] {
3438
const {
3539
CONTEXT: deployContext = 'local',
3640
BRANCH: branchName = readBranchName(),
41+
COMMIT_REF: commitRef = readCommitHash(),
3742
} = process.env;
3843

3944
const otherVersions = readOtherLatestReleaseTagNames();
40-
const isReleaseBranch = /^v\d+$/.test(branchName);
45+
export const isReleaseBranch = /^v\d+$/.test(branchName);
4146

4247
/**
4348
* The text of the version banner describing the current version.
@@ -65,6 +70,11 @@ export const versionBannerInfix: string | null = (() => {
6570
*/
6671
export const version = version_;
6772

73+
/**
74+
* The commit hash of the current version.
75+
*/
76+
export const commitHash = commitRef.substring(0, 7);
77+
6878
/**
6979
* The version label to display in the top-right corner of the site.
7080
*/

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
"generate": "run-s generate:locales generate:api-docs",
1111
"generate:api-docs": "tsx ./scripts/apidocs.ts",
1212
"generate:locales": "tsx ./scripts/generate-locales.ts",
13-
"docs:build": "run-s generate:api-docs docs:build:run",
13+
"docs:build": "run-s generate:api-docs docs:build:embedded docs:build:run",
14+
"docs:build:embedded": "tsup-node --entry.faker src/index.ts --format esm --outDir docs/public --no-dts --no-splitting --no-clean",
1415
"docs:build:run": "vitepress build docs",
1516
"docs:build:ci": "run-s build docs:build",
16-
"docs:dev": "run-s generate:api-docs docs:dev:run",
17+
"docs:dev": "run-s generate:api-docs docs:build:embedded docs:dev:run",
1718
"docs:dev:run": "vitepress dev docs",
1819
"docs:serve": "vitepress serve docs --port 5173",
1920
"docs:diff": "tsx ./scripts/diff.ts",

tsup.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ export default defineConfig({
1414
minify: true,
1515
sourcemap: false,
1616
splitting: true,
17+
esbuildOptions(options) {
18+
options.charset = 'utf8'; // Prevent Unicode escaping
19+
},
1720
});

0 commit comments

Comments
 (0)