Skip to content

Commit

Permalink
perf: use util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jan 4, 2023
1 parent f853aa4 commit 2e364a9
Show file tree
Hide file tree
Showing 64 changed files with 308 additions and 242 deletions.
1 change: 1 addition & 0 deletions packages/auto-catalog/rollup.config.ts
Expand Up @@ -7,6 +7,7 @@ export default [
"@vuepress/shared",
"@vuepress/utils",
"vuepress-shared/node",
"vuepress-plugin-components",
"vuepress-plugin-sass-palette",
],
}),
Expand Down
19 changes: 9 additions & 10 deletions packages/blog2/src/node/category.ts
@@ -1,5 +1,5 @@
import { createPage } from "@vuepress/core";
import { removeLeadingSlash } from "@vuepress/shared";
import { isFunction, isString, removeLeadingSlash } from "@vuepress/shared";
import { logger } from "./utils.js";

import type { App, Page } from "@vuepress/core";
Expand Down Expand Up @@ -52,13 +52,13 @@ export const prepareCategory = (
},
index
) => {
if (typeof key !== "string" || !key) {
if (!isString(key) || !key) {
logger.error(`Invalid 'key' option ${key} in 'category[${index}]'`);

return null;
}

if (typeof getter !== "function") {
if (!isFunction(getter)) {
logger.error(
`Invalid 'getter' option in 'category[${index}]', it should be a function!`
);
Expand All @@ -70,13 +70,12 @@ export const prepareCategory = (

const categoryMap: CategoryMap = {};
const pageKeys: string[] = [];
const getItemPath =
typeof itemPath === "function"
? itemPath
: (name: string): string =>
(itemPath || "")
.replace(/:key/g, slugify(key))
.replace(/:name/g, slugify(name));
const getItemPath = isFunction(itemPath)
? itemPath
: (name: string): string =>
(itemPath || "")
.replace(/:key/g, slugify(key))
.replace(/:name/g, slugify(name));

for (const localePath in pageMap) {
if (path) {
Expand Down
4 changes: 2 additions & 2 deletions packages/blog2/src/node/type.ts
@@ -1,5 +1,5 @@
import { createPage } from "@vuepress/core";
import { removeLeadingSlash } from "@vuepress/shared";
import { isString, removeLeadingSlash } from "@vuepress/shared";
import { logger } from "./utils.js";

import type { App } from "@vuepress/core";
Expand Down Expand Up @@ -48,7 +48,7 @@ export const prepareType = (
},
index
) => {
if (typeof key !== "string" || !key) {
if (!isString(key) || !key) {
logger.error(`Invalid 'key' option ${key} in 'category[${index}]'`);

return null;
Expand Down
18 changes: 15 additions & 3 deletions packages/components/rollup.config.ts
Expand Up @@ -27,7 +27,13 @@ export default [
dtsExternal: [/\.scss$/],
}),
...rollupTypescript("client/components/BiliBili", {
external: ["@vueuse/core", "vue", "vuepress-shared/client", /\.scss$/],
external: [
"@vueuse/core",
"@vuepress/shared",
"vue",
"vuepress-shared/client",
/\.scss$/,
],
dtsExternal: [/\.scss$/],
}),
...rollupTypescript("client/components/BackToTop", {
Expand Down Expand Up @@ -74,11 +80,17 @@ export default [
dtsExternal: [/\.scss$/],
}),
...rollupTypescript("client/components/StackBlitz", {
external: ["@vueuse/core", "vue", /\.scss$/],
external: ["@vueuse/core", "@vuepress/shared", "vue", /\.scss$/],
dtsExternal: [/\.scss$/],
}),
...rollupTypescript("client/components/YouTube", {
external: ["@vueuse/core", "@vuepress/client", "vue", /\.scss$/],
external: [
"@vueuse/core",
"@vuepress/shared",
"@vuepress/client",
"vue",
/\.scss$/,
],
dtsExternal: [/\.scss$/],
}),
...rollupTypescript("client/components/VideoPlayer", {
Expand Down
6 changes: 4 additions & 2 deletions packages/components/src/client/composables/size.ts
@@ -1,10 +1,12 @@
import { isString } from "@vuepress/shared";
import { useEventListener } from "@vueuse/core";
import { computed, onMounted, isRef, ref, unref, watch } from "vue";

import type { MaybeRef } from "@vueuse/core";
import type { Ref } from "vue";

const getValue = (value: string | number): string =>
typeof value === "string" ? value : `${value}px`;
isString(value) ? value : `${value}px`;

export interface SizeOptions {
width: string | number | undefined;
Expand All @@ -27,7 +29,7 @@ export const useSize = <E extends HTMLElement>(
const height = ref("auto");

const getRadio = (radio: number | string | undefined): number => {
if (typeof radio === "string") {
if (isString(radio)) {
const [width, height] = radio.split(":");
const parsedRadio = Number(width) / Number(height);

Expand Down
10 changes: 5 additions & 5 deletions packages/components/src/client/utils/pdf.ts
@@ -1,7 +1,3 @@
import { withBase } from "@vuepress/client";
import { ensureEndingSlash } from "@vuepress/shared";
import { checkIsMobile, checkIsSafari } from "vuepress-shared/client";

/**
* Fork and edited from https://github.com/pipwerks/PDFObject/blob/master/pdfobject.js
*
Expand All @@ -16,6 +12,10 @@ import { checkIsMobile, checkIsSafari } from "vuepress-shared/client";
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

import { withBase } from "@vuepress/client";
import { ensureEndingSlash, isString } from "@vuepress/shared";
import { checkIsMobile, checkIsSafari } from "vuepress-shared/client";

declare const PDFJS_URL: string | null;

export interface ViewPDFOptions {
Expand Down Expand Up @@ -164,7 +164,7 @@ export const viewPDF = (
// Modern versions of Firefox come bundled with PDFJS
isFirefoxWithPDFJS);

if (typeof url !== "string") {
if (!isString(url)) {
logError("URL is not valid");

return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/node/define.ts
@@ -1,3 +1,4 @@
import { isString } from "@vuepress/shared";
import { getLocales } from "vuepress-shared/node";
import {
backToTopLocales,
Expand All @@ -24,8 +25,7 @@ export const getDefine =
});

if (options.components?.includes("FontIcon"))
result["ICON_PREFIX"] =
typeof prefix === "string" ? prefix : getIconPrefix(assets);
result["ICON_PREFIX"] = isString(prefix) ? prefix : getIconPrefix(assets);

if (options.components?.includes("PDF")) {
result["PDF_LOCALES"] = getLocales({
Expand Down
5 changes: 3 additions & 2 deletions packages/components/src/node/prepare.ts
@@ -1,3 +1,4 @@
import { isPlainObject, isString } from "@vuepress/shared";
import { CLIENT_FOLDER, logger } from "./utils.js";

import type { App } from "@vuepress/core";
Expand Down Expand Up @@ -101,7 +102,7 @@ useStyleTag(\`${content}\`, { id: "icon-assets" });
}
});

if (typeof rootComponents.addThis === "string") {
if (isString(rootComponents.addThis)) {
shouldImportUseScriptTag = true;
setup += `\
useScriptTag(\`//s7.addthis.com/js/300/addthis_widget.js#pubid=${rootComponents.addThis}\`);
Expand All @@ -122,7 +123,7 @@ import BackToTop from "${CLIENT_FOLDER}components/BackToTop.js";
`;
}

if (typeof rootComponents.notice === "object") {
if (isPlainObject(rootComponents.notice)) {
shouldImportH = true;
configImport += `\
import Notice from "${CLIENT_FOLDER}components/Notice.js";
Expand Down
1 change: 1 addition & 0 deletions packages/copy-code2/package.json
Expand Up @@ -50,6 +50,7 @@
},
"dependencies": {
"@vuepress/client": "2.0.0-beta.60",
"@vuepress/shared": "2.0.0-beta.60",
"@vuepress/utils": "2.0.0-beta.60",
"balloon-css": "^1.2.0",
"vue": "^3.2.45",
Expand Down
1 change: 1 addition & 0 deletions packages/copy-code2/rollup.config.ts
Expand Up @@ -14,6 +14,7 @@ export default [
...rollupTypescript("client/config", {
external: [
"@vuepress/client",
"@vuepress/shared",
"balloon-css/balloon.css",
"vue",
"vue-router",
Expand Down
5 changes: 3 additions & 2 deletions packages/copy-code2/src/client/composables/setup.ts
Expand Up @@ -25,6 +25,7 @@
* © 2019 GitHub, Inc.
*/

import { isArray, isString } from "@vuepress/shared";
import { onMounted, watch } from "vue";
import { useRoute } from "vue-router";
import { Message, useLocaleConfig } from "vuepress-shared/client";
Expand Down Expand Up @@ -79,11 +80,11 @@ export const setupCopyCode = (): void => {

const generateCopyButton = (): void => {
setTimeout(() => {
if (typeof copyCodeSelector === "string")
if (isString(copyCodeSelector))
document
.querySelectorAll<HTMLElement>(copyCodeSelector)
.forEach(insertCopyButton);
else if (Array.isArray(copyCodeSelector))
else if (isArray(copyCodeSelector))
copyCodeSelector.forEach((item) => {
document
.querySelectorAll<HTMLElement>(item)
Expand Down
7 changes: 6 additions & 1 deletion packages/copyright2/rollup.config.ts
Expand Up @@ -2,7 +2,12 @@ import { rollupTypescript } from "../../scripts/rollup.js";

export default [
...rollupTypescript("node/index", {
external: ["@vuepress/core", "@vuepress/utils", "vuepress-shared/node"],
external: [
"@vuepress/core",
"@vuepress/shared",
"@vuepress/utils",
"vuepress-shared/node",
],
dtsExternal: ["vuepress-shared"],
}),
...rollupTypescript("client/config", {
Expand Down
6 changes: 3 additions & 3 deletions packages/copyright2/src/client/composables/setup.ts
@@ -1,5 +1,5 @@
import { usePageFrontmatter, usePageData } from "@vuepress/client";
import { isLinkHttp, removeEndingSlash } from "@vuepress/shared";
import { isLinkHttp, isPlainObject, removeEndingSlash } from "@vuepress/shared";
import { useEventListener } from "@vueuse/core";
import { computed, onMounted, watchEffect } from "vue";
import { useRoute } from "vue-router";
Expand Down Expand Up @@ -39,7 +39,7 @@ export const setupCopyright = (): void => {
if (!enabled.value) return false;

if (
typeof frontmatterOptions === "object" &&
isPlainObject(frontmatterOptions) &&
"disableCopy" in frontmatterOptions
)
return frontmatterOptions.disableCopy;
Expand All @@ -53,7 +53,7 @@ export const setupCopyright = (): void => {
if (!enabled.value) return false;

if (
typeof frontmatterOptions === "object" &&
isPlainObject(frontmatterOptions) &&
"disableSelection" in frontmatterOptions
)
return frontmatterOptions.disableSelection;
Expand Down
6 changes: 3 additions & 3 deletions packages/copyright2/src/node/plugins.ts
@@ -1,3 +1,4 @@
import { isFunction } from "@vuepress/shared";
import { getDirname, path } from "@vuepress/utils";
import { getLocales } from "vuepress-shared/node";

Expand Down Expand Up @@ -45,10 +46,9 @@ export const copyrightPlugin =
}),

extendsPage: (page: Page<Partial<CopyrightPluginPageData>>): void => {
const authorText = typeof author === "function" ? author(page) : author;
const authorText = isFunction(author) ? author(page) : author;

const licenseText =
typeof license === "function" ? license(page) : license;
const licenseText = isFunction(license) ? license(page) : license;

page.data.copyright = {
...(authorText ? { author: authorText } : {}),
Expand Down
3 changes: 2 additions & 1 deletion packages/feed2/src/node/json/index.ts
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { isArray } from "@vuepress/shared";
import { stripTags } from "vuepress-shared/node";

import type { Feed } from "../feed.js";
Expand Down Expand Up @@ -65,7 +66,7 @@ export const renderJSON = (feed: Feed): string => {
feedItem.date_modified = item.lastUpdated.toISOString();

// author
if (Array.isArray(item.author))
if (isArray(item.author))
feedItem.authors = item.author
.filter((author) => author.name)
.map((author) => formatAuthor(author));
Expand Down

0 comments on commit 2e364a9

Please sign in to comment.