Skip to content

Commit

Permalink
Merge branch 'main' into rollup
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Sep 30, 2024
2 parents e1f1bc8 + e58c755 commit 2f9e049
Show file tree
Hide file tree
Showing 28 changed files with 292 additions and 123 deletions.
3 changes: 3 additions & 0 deletions tools/helper/src/client/components/LoadingIcon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { FunctionalComponent, VNode } from 'vue'
import { h } from 'vue'

/**
* Loading icon
*/
export const LoadingIcon: FunctionalComponent<{
size?: number
stroke?: number
Expand Down
5 changes: 5 additions & 0 deletions tools/helper/src/client/composables/useDarkmode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ const _useDarkmode = (): Readonly<Ref<boolean>> => {
return readonly(isDarkmode)
}

/**
* Get darkmode status
*
* @returns readonly darkmode ref
*/
// eslint-disable-next-line no-return-assign
export const useDarkmode = (): Readonly<Ref<boolean>> =>
(darkmode ??= _useDarkmode())
3 changes: 3 additions & 0 deletions tools/helper/src/client/composables/useRoutePaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import type { ComputedRef } from 'vue'
import { computed } from 'vue'
import { useRoutes } from 'vuepress/client'

/**
* get all route paths
*/
export const useRoutePaths = (): ComputedRef<string[]> => {
const routes = useRoutes()

Expand Down
3 changes: 3 additions & 0 deletions tools/helper/src/client/noopComponent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { FunctionalComponent } from 'vue'

/**
* A noop component that renders nothing
*/
const noopComponent: FunctionalComponent = () => null

export default noopComponent
3 changes: 3 additions & 0 deletions tools/helper/src/client/noopModule.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
/**
* Noop module
*/
export default {}
6 changes: 6 additions & 0 deletions tools/helper/src/client/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { strFromU8, strToU8, unzlibSync, zlibSync } from 'fflate/browser'

declare const __VUEPRESS_SSR__: boolean

/**
* Encode and compress data
*/
export const encodeData = (
data: string,
level: DeflateOptions['level'] = 6,
Expand All @@ -17,6 +20,9 @@ export const encodeData = (
: btoa(binary)
}

/**
* Decode and unzip data
*/
export const decodeData = (base64: string): string => {
const binary = __VUEPRESS_SSR__
? Buffer.from(base64, 'base64').toString('binary')
Expand Down
5 changes: 5 additions & 0 deletions tools/helper/src/client/utils/getDarkmode.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/**
* Get darkmode status from DOM
*
* @returns darkmode status
*/
export const getDarkmode = (): boolean =>
document.documentElement.getAttribute('data-theme') === 'dark'
3 changes: 3 additions & 0 deletions tools/helper/src/client/utils/getHeaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ export interface GetHeadersOptions {
levels?: HeaderLevels
}

/**
* Get headers of current page.
*/
export const getHeaders = ({
selector = [...new Array<undefined>(6)]
.map((_, i) => `[vp-content] h${i + 1}`)
Expand Down
7 changes: 7 additions & 0 deletions tools/helper/src/client/utils/hasGlobalComponent.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { App } from 'vue'
import { camelize, capitalize, getCurrentInstance } from 'vue'

/**
* Check if a global component with the given name exists
*
* @param name component name
* @param app Vue app instance
* @returns whether the global component with the given name exists
*/
export const hasGlobalComponent = (name: string, app?: App): boolean => {
const globalComponents = (app?._instance ?? getCurrentInstance())?.appContext
.components
Expand Down
6 changes: 6 additions & 0 deletions tools/helper/src/client/utils/wait.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* Wait for a given time
*
* @param ms wait time in milliseconds
* @returns a promise that resolves after the given time
*/
export const wait = (ms: number): Promise<void> =>
new Promise<void>((resolve) => {
setTimeout(resolve, ms)
Expand Down
7 changes: 5 additions & 2 deletions tools/helper/src/node/bundler/customizeDevServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import { removeLeadingSlash } from 'vuepress/shared'
import { getBundlerName } from './getBundlerName.js'
import { mergeViteConfig } from './vite/index.js'

/**
* Options for customizing VuePress Dev Server
*/
export interface DevServerOptions {
/**
* Path to be responded
*/
path: string
/**
* Respond function
* Respond handler
*/
response: (
request: IncomingMessage,
Expand All @@ -33,7 +36,7 @@ export interface DevServerOptions {
/**
* Handle specific path when running VuePress Dev Server
*
* @param config VuePress Bundler config
* @param bundlerOptions VuePress Bundler config
* @param app VuePress Node App
* @param path Path to be responded
* @param response respond function
Expand Down
5 changes: 5 additions & 0 deletions tools/helper/src/node/bundler/getBundlerName.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { App } from 'vuepress/core'

/**
* Get current bundler name
*
* @param app VuePress Node App
*/
export const getBundlerName = (app: App): string => {
const { name } = app.options.bundler

Expand Down
2 changes: 1 addition & 1 deletion tools/helper/src/node/bundler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export * from './addCustomElement.js'
export * from './customizeDevServer.js'
export * from './getBundlerName.js'
export * from './vite/index.js'
export * from './webpack.js'
export * from './webpack/index.js'
44 changes: 35 additions & 9 deletions tools/helper/src/node/bundler/vite/mergeViteConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,24 @@ const normalizeSingleAlias = ({
replacement,
}

if (customResolver) alias.customResolver = customResolver
if (customResolver) {
alias.customResolver = customResolver
}

return alias
}

const normalizeAlias = (aliasOption: AliasOptions): Alias[] =>
const normalizeAlias = (aliasOption: AliasOptions = []): Alias[] =>
isArray(aliasOption)
? aliasOption.map(normalizeSingleAlias)
: keys(aliasOption).map((find) =>
normalizeSingleAlias({
find,
replacement: (aliasOption as Record<string, string>)[find],
replacement: aliasOption[find],
}),
)

export const mergeAlias = (
const mergeAlias = (
defaults?: AliasOptions,
overrides?: AliasOptions,
): AliasOptions | undefined => {
Expand All @@ -124,17 +126,30 @@ export const mergeAlias = (
return [...normalizeAlias(overrides), ...normalizeAlias(defaults)]
}

const backwardCompatibleWorkerPlugins = (plugins: any): any[] => {
if (Array.isArray(plugins)) {
return plugins
}

if (typeof plugins === 'function') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
return plugins() as any[]
}

return []
}

const mergeConfigRecursively = (
defaults: Record<string, any>,
{ ...merged }: Record<string, any>,
overrides: Record<string, any>,
rootPath: string,
): Record<string, any> => {
const merged: Record<string, any> = { ...defaults }

for (const key in overrides) {
const value = overrides[key]

if (value == null) continue
if (value == null) {
continue
}

const existing = merged[key]

Expand All @@ -152,17 +167,28 @@ const mergeConfigRecursively = (
continue
} else if (
key === 'noExternal' &&
rootPath === 'ssr' &&
(rootPath === 'ssr' || rootPath === 'resolve') &&
(existing === true || value === true)
) {
merged[key] = true
continue
} else if (key === 'plugins' && rootPath === 'worker') {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
merged[key] = () => [
...backwardCompatibleWorkerPlugins(existing),
...backwardCompatibleWorkerPlugins(value),
]
continue
} else if (key === 'server' && rootPath === 'server.hmr') {
merged[key] = value
continue
}

if (isArray(existing) || isArray(value)) {
merged[key] = [...arraify(existing), ...arraify(value)]
continue
}

if (isObject(existing) && isObject(value)) {
merged[key] = mergeConfigRecursively(
existing,
Expand Down
33 changes: 33 additions & 0 deletions tools/helper/src/node/bundler/webpack/chainWebpack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type {
WebpackBundlerOptions,
WebpackChainConfig,
} from '@vuepress/bundler-webpack'
import type { App } from 'vuepress/core'
import { getBundlerName } from '../getBundlerName.js'

/**
* Chain webpack
*
* @param bundlerOptions VuePress Bundler config
* @param app VuePress Node App
* @param chain chain function
*/
export const chainWebpack = (
bundlerOptions: unknown,
app: App,
chain: (
config: WebpackChainConfig,
isServer: boolean,
isBuild: boolean,
) => void,
): void => {
if (getBundlerName(app) === 'webpack') {
const webpackBundlerOptions = bundlerOptions as WebpackBundlerOptions
const { chainWebpack: originalChainWebpack } = webpackBundlerOptions

webpackBundlerOptions.chainWebpack = (config, isServer, isBuild): void => {
originalChainWebpack?.(config, isServer, isBuild)
chain(config, isServer, isBuild)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,17 @@
import type {
WebpackBundlerOptions,
WebpackChainConfig,
WebpackConfiguration,
} from '@vuepress/bundler-webpack'
import type { App } from 'vuepress/core'
import { getBundlerName } from './getBundlerName.js'

export const chainWebpack = (
bundlerOptions: unknown,
app: App,
chain: (
config: WebpackChainConfig,
isServer: boolean,
isBuild: boolean,
) => void,
): void => {
if (getBundlerName(app) === 'webpack') {
const webpackBundlerOptions = bundlerOptions as WebpackBundlerOptions
const { chainWebpack: originalChainWebpack } = webpackBundlerOptions

webpackBundlerOptions.chainWebpack = (config, isServer, isBuild): void => {
originalChainWebpack?.(config, isServer, isBuild)
chain(config, isServer, isBuild)
}
}
}
import { getBundlerName } from '../getBundlerName.js'

/**
* Configure webpack options
*
* @param bundlerOptions VuePress Bundler config
* @param app VuePress Node App
* @param configureWebpack function to configure webpack
*/
export const configWebpack = (
bundlerOptions: unknown,
app: App,
Expand Down
2 changes: 2 additions & 0 deletions tools/helper/src/node/bundler/webpack/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './chainWebpack.js'
export * from './configWebpack.js'
25 changes: 14 additions & 11 deletions tools/helper/src/node/locales/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ import { Logger } from '../utils/index.js'
import { lang2PathConfig, path2langConfig } from './config.js'
import type { KnownLangCode } from './types.js'

/** Get language from path */
export const path2Lang = (path = '', debug = false): KnownLangCode => {
if (path in path2langConfig) return path2langConfig[path]
/** Infer language from locale path */
export const inferLocaleLang = (
localePath = '',
debug = false,
): KnownLangCode => {
if (localePath in path2langConfig) return path2langConfig[localePath]

if (debug)
// eslint-disable-next-line no-console
console.warn(
`${path} isn’t assign with a lang, and will return "en-US" instead.`,
`${localePath} isn’t assign with a lang, and will return "en-US" instead.`,
)

return 'en-US'
}

/** Get path from language */
export const lang2Path = (lang = '', debug = false): string => {
/** Infer locale path from language */
export const inferLocalePath = (lang = '', debug = false): string => {
if (lang in lang2PathConfig) return lang2PathConfig[lang as KnownLangCode]

if (debug)
Expand All @@ -47,13 +50,13 @@ export const getRootLang = (app: App): string => {
}

/**
* Get the infer language path from root directory language
* Infer language path from root directory language
*
* @param app VuePress Node App
* @returns infer language
* @returns inferred locale path of root directory
*/
export const getRootLangPath = (app: App): string =>
lang2Path(getRootLang(app), app.env.isDebug)
inferLocalePath(getRootLang(app), app.env.isDebug)

/**
* Get locale paths
Expand Down Expand Up @@ -96,10 +99,10 @@ export const getLocaleConfig = <T extends LocaleData>({
const defaultLocaleData =
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
defaultLocalesConfig[localePath] ||
(lang2Path(app.options.locales[localePath].lang) === '/'
(inferLocalePath(app.options.locales[localePath].lang) === '/'
? null
: defaultLocalesConfig[
lang2Path(app.options.locales[localePath].lang)
inferLocalePath(app.options.locales[localePath].lang)
])

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
Expand Down
Loading

0 comments on commit 2f9e049

Please sign in to comment.