Skip to content

Commit

Permalink
feat: add helper package
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jan 26, 2024
1 parent e7887aa commit 4395f27
Show file tree
Hide file tree
Showing 24 changed files with 1,188 additions and 0 deletions.
120 changes: 120 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 71 additions & 0 deletions tools/helper/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{
"name": "@vuepress/helper",
"version": "2.0.0-rc.2",
"description": "VuePress helper",
"keywords": [
"vuepress",
"helper",
"bundler-helper",
"excerpt"
],
"homepage": "https://github.com/vuepress",
"bugs": {
"url": "https://github.com/vuepress/ecosystem/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/vuepress/ecosystem.git",
"directory": "tools/helper"
},
"license": "MIT",
"author": {
"name": "Mr.Hope",
"email": "[email protected]",
"url": "https://mister-hope.com"
},
"type": "module",
"exports": {
".": "./lib/node/index.js",
"./client": "./lib/client/index.js",
"./client/*": "./lib/client/*",
"./node": "./lib/node/index.js",
"./noopComponent": "./lib/client/noopComponent.js",
"./noopModule": "./lib/client/noopModule.js",
"./package.json": "./package.json"
},
"main": "./lib/node/index.js",
"types": "./lib/node/index.d.ts",
"files": [
"lib"
],
"scripts": {
"build": "tsc -b tsconfig.build.json",
"clean": "rimraf lib *.tsbuildinfo"
},
"dependencies": {
"@vue/shared": "^3.4.15",
"cheerio": "1.0.0-rc.12",
"fflate": "^0.8.1",
"gray-matter": "^4.0.3",
"vue": "^3.4.15"
},
"devDependencies": {
"@types/connect": "3.4.38",
"@vuepress/bundler-vite": "2.0.0-rc.2",
"@vuepress/bundler-webpack": "2.0.0-rc.2",
"@vuepress/plugin-git": "2.0.0-rc.1",
"vite": "5.0.12"
},
"peerDependencies": {
"vuepress": "2.0.0-rc.2"
},
"engines": {
"node": ">=18.16.0",
"npm": ">=8",
"pnpm": ">=7",
"yarn": ">=2"
},
"publishConfig": {
"access": "public"
}
}
1 change: 1 addition & 0 deletions tools/helper/src/client/composables/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useLocaleConfig.js'
18 changes: 18 additions & 0 deletions tools/helper/src/client/composables/useLocaleConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { computed } from 'vue'
import type { ComputedRef } from 'vue'
import { useRouteLocale } from 'vuepress/client'
import type { LocaleConfig, LocaleData } from 'vuepress/shared'

/**
* Get current locale config
*
* @param localesConfig client locale Config
* @returns current locale config
*/
export const useLocaleConfig = <T extends LocaleData>(
localesConfig: LocaleConfig<T>,
): ComputedRef<Partial<T>> => {
const routeLocale = useRouteLocale()

return computed(() => localesConfig[routeLocale.value] ?? {})
}
2 changes: 2 additions & 0 deletions tools/helper/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './composables/index.js'
export * from '../shared/index.js'
5 changes: 5 additions & 0 deletions tools/helper/src/client/noopComponent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { FunctionalComponent } from 'vue'

const noopComponent: FunctionalComponent = () => null

export default noopComponent
1 change: 1 addition & 0 deletions tools/helper/src/client/noopModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {}
64 changes: 64 additions & 0 deletions tools/helper/src/node/bundler/addCustomElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { ViteBundlerOptions } from '@vuepress/bundler-vite'
import type { WebpackBundlerOptions } from '@vuepress/bundler-webpack'
import type { App } from 'vuepress/core'
import { isString } from '../../shared/index.js'
import { getBundlerName } from './getBundlerName.js'

/**
* Add tags as customElement
*
* @param bundlerOptions VuePress Bundler config
* @param app VuePress Node App
* @param customElements tags recognized as custom element
*/
export const addCustomElement = (
bundlerOptions: unknown,
app: App,
customElement: string[] | string | RegExp,
): void => {
const customElements = isString(customElement)
? [customElement]
: customElement
const bundlerName = getBundlerName(app)

// for vite
if (bundlerName === 'vite') {
const viteBundlerConfig = bundlerOptions as ViteBundlerOptions

const { isCustomElement } = (((viteBundlerConfig.vuePluginOptions ??=
{}).template ??= {}).compilerOptions ??= {})

viteBundlerConfig.vuePluginOptions.template.compilerOptions.isCustomElement =
(tag: string): boolean | void => {
if (
customElements instanceof RegExp
? customElements.test(tag)
: customElements.includes(tag)
)
return true

return isCustomElement?.(tag)
}
}

// for webpack
else if (bundlerName === 'webpack') {
const webpackBundlerConfig = bundlerOptions as WebpackBundlerOptions

const { isCustomElement } = ((webpackBundlerConfig.vue ??=
{}).compilerOptions ??= {})

webpackBundlerConfig.vue.compilerOptions.isCustomElement = (
tag: string,
): boolean | void => {
if (
customElements instanceof RegExp
? customElements.test(tag)
: customElements.includes(tag)
)
return true

return isCustomElement?.(tag)
}
}
}
Loading

0 comments on commit 4395f27

Please sign in to comment.