Skip to content

Commit

Permalink
refactor: fix bad requires
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Sep 21, 2021
1 parent 37d4e44 commit b3a4c52
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 24 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
"@docsearch/js": "^1.0.0-alpha.28",
"@vitejs/plugin-vue": "^1.9.0",
"vite": "^2.5.0",
"vue": "^3.2.13"
"vue": "^3.2.13",
"prismjs": "^1.23.0"
},
"devDependencies": {
"@microsoft/api-extractor": "^7.18.9",
Expand All @@ -81,13 +82,15 @@
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@types/compression": "^1.7.0",
"@types/debug": "^4.1.7",
"@types/fs-extra": "^9.0.11",
"@types/jest": "^26.0.23",
"@types/markdown-it": "^12.0.1",
"@types/koa": "^2.13.1",
"@types/koa-static": "^4.0.1",
"@types/lru-cache": "^5.1.0",
"@types/markdown-it": "^12.0.1",
"@types/node": "^15.6.1",
"@types/polka": "^0.5.3",
"@types/postcss-load-config": "^3.0.1",
"chalk": "^4.1.1",
"chokidar": "^3.5.1",
Expand Down Expand Up @@ -116,7 +119,6 @@
"ora": "^5.4.0",
"polka": "^0.5.2",
"prettier": "^2.3.0",
"prismjs": "^1.23.0",
"rimraf": "^3.0.2",
"rollup": "^2.56.3",
"rollup-plugin-esbuild": "^4.5.0",
Expand Down
3 changes: 1 addition & 2 deletions src/node/build/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { HeadConfig } from '../shared'
import { normalizePath } from 'vite'
import { RollupOutput, OutputChunk, OutputAsset } from 'rollup'
import { slash } from '../utils/slash'

const escape = require('escape-html')
import escape from 'escape-html'

export async function renderPage(
config: SiteConfig,
Expand Down
3 changes: 2 additions & 1 deletion src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import {
} from './shared'
import { resolveAliases, APP_PATH, DEFAULT_THEME_PATH } from './alias'
import { MarkdownOptions } from './markdown/markdown'
import _debug from 'debug'

export { resolveSiteDataByRoute } from './shared'

const debug = require('debug')('vitepress:config')
const debug = _debug('vitepress:config')

export type { MarkdownOptions }

Expand Down
7 changes: 3 additions & 4 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ import { linkPlugin } from './plugins/link'
import { extractHeaderPlugin } from './plugins/header'
import { Header } from '../shared'
import anchor, { AnchorOptions } from 'markdown-it-anchor'

const attrs = require('markdown-it-attrs')
const emoji = require('markdown-it-emoji')
const toc = require('markdown-it-table-of-contents')
import attrs from 'markdown-it-attrs'
import emoji from 'markdown-it-emoji'
import toc from 'markdown-it-table-of-contents'

export interface MarkdownOptions extends MarkdownIt.Options {
lineNumbers?: boolean
Expand Down
6 changes: 2 additions & 4 deletions src/node/markdown/plugins/component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import MarkdownIt from 'markdown-it'
import { RuleBlock } from 'markdown-it/lib/parser_block'
import blockNames from 'markdown-it/lib/common/html_blocks'
import { HTML_OPEN_CLOSE_TAG_RE } from 'markdown-it/lib/common/html_re'

// Replacing the default htmlBlock rule to allow using custom components at
// root level

const blockNames: string[] = require('markdown-it/lib/common/html_blocks')
const HTML_OPEN_CLOSE_TAG_RE: RegExp =
require('markdown-it/lib/common/html_re').HTML_OPEN_CLOSE_TAG_RE

// An array of opening and corresponding closing sequences for html tags,
// last argument defines whether it can terminate a paragraph or not
const HTML_SEQUENCES: [RegExp, RegExp, boolean][] = [
Expand Down
3 changes: 1 addition & 2 deletions src/node/markdown/plugins/containers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import MarkdownIt from 'markdown-it'
import Token from 'markdown-it/lib/token'

const container = require('markdown-it-container')
import container from 'markdown-it-container'

export const containerPlugin = (md: MarkdownIt) => {
md.use(...createContainer('tip', 'TIP'))
Expand Down
8 changes: 5 additions & 3 deletions src/node/markdown/plugins/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const chalk = require('chalk')
const prism = require('prismjs')
import chalk from 'chalk'
import escapeHtml from 'escape-html'
import prism from 'prismjs'

// prism is listed as actual dep so it's ok to require
const loadLanguages = require('prismjs/components/index')
const escapeHtml = require('escape-html')

// required to make embedded highlighting work...
loadLanguages(['markup', 'css', 'javascript'])
Expand Down
2 changes: 1 addition & 1 deletion src/node/markdown/plugins/slugify.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// string.js slugify drops non ascii chars so we have to
// use a custom implementation here
const removeDiacritics = require('diacritics').remove
import { remove as removeDiacritics } from 'diacritics'
// eslint-disable-next-line no-control-regex
const rControl = /[\u0000-\u001f]/g
const rSpecial = /[\s~`!@#$%^&*()\-_+=[\]{}|\\;:"'<>,.?/]+/g
Expand Down
3 changes: 2 additions & 1 deletion src/node/markdownToVue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { deeplyParseHeader } from './utils/parseHeader'
import { PageData, HeadConfig } from './shared'
import { slash } from './utils/slash'
import chalk from 'chalk'
import _debug from 'debug'

const debug = require('debug')('vitepress:md')
const debug = _debug('vitepress:md')
const cache = new LRUCache<string, MarkdownCompileResult>({ max: 1024 })
const includesRE = /<!--\s*@include:\s*(.*?)\s*-->/g

Expand Down
3 changes: 2 additions & 1 deletion src/node/serve/serve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sirv from 'sirv'
import compression from 'compression'
import { resolveConfig } from '../config'
import polka from 'polka'

export interface ServeOptions {
root?: string
Expand All @@ -26,7 +27,7 @@ export async function serve(options: ServeOptions = {}) {
}
})

require('polka')()
polka()
.use(compress, serve)
.listen(port, (err: any) => {
if (err) throw err
Expand Down
43 changes: 43 additions & 0 deletions src/node/shims.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
declare module 'markdown-it-attrs' {
const def: any
export default def
}

declare module 'markdown-it-emoji' {
const def: any
export default def
}

declare module 'markdown-it-table-of-contents' {
const def: any
export default def
}

declare module 'markdown-it-container' {
const def: any
export default def
}

declare module 'escape-html' {
const def: (str: string) => string
export default def
}

declare module 'prismjs' {
const def: any
export default def
}

declare module 'prismjs/components/index' {
const def: any
export default def
}

declare module 'diacritics' {
export const remove: (str: string) => string
}

declare module '*.json' {
const def: any
export default def
}
2 changes: 1 addition & 1 deletion src/node/utils/parseHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
// loader, the raw HTML in headers will finally be parsed by Vue-loader.
// so that we can write HTML/Vue in the header. One exception is the HTML
// wrapped by <code>(markdown token: '`') tag.
import emojiData from 'markdown-it-emoji/lib/data/full.json'

const parseEmojis = (str: string) => {
const emojiData = require('markdown-it-emoji/lib/data/full.json')
return String(str).replace(
/:(.+?):/g,
(placeholder, key) => emojiData[key] || placeholder
Expand Down
29 changes: 28 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,13 @@
"@types/keygrip" "*"
"@types/node" "*"

"@types/debug@^4.1.7":
version "4.1.7"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.7.tgz#7cc0ea761509124709b8b2d1090d8f6c17aadb82"
integrity sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg==
dependencies:
"@types/ms" "*"

"@types/estree@*":
version "0.0.50"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.50.tgz#1e0caa9364d3fccd2931c3ed96fdbeaa5d4cca83"
Expand All @@ -918,7 +925,7 @@
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==

"@types/express-serve-static-core@^4.17.18":
"@types/express-serve-static-core@*", "@types/express-serve-static-core@^4.17.18":
version "4.17.24"
resolved "https://registry.yarnpkg.com/@types/express-serve-static-core/-/express-serve-static-core-4.17.24.tgz#ea41f93bf7e0d59cd5a76665068ed6aab6815c07"
integrity sha512-3UJuW+Qxhzwjq3xhwXm2onQcFHn76frIYVbTu+kn24LFxI+dEhdfISDFovPB8VpEgW8oQCTpRuCe+0zJxB7NEA==
Expand Down Expand Up @@ -1063,6 +1070,11 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
integrity sha512-jhuKLIRrhvCPLqwPcx6INqmKeiA5EWrsCOPhrlFSrbrmU4ZMPjj5Ul/oLCMDO98XRUIwVm78xICz4EPCektzeQ==

"@types/ms@*":
version "0.7.31"
resolved "https://registry.yarnpkg.com/@types/ms/-/ms-0.7.31.tgz#31b7ca6407128a3d2bbc27fe2d21b345397f6197"
integrity sha512-iiUgKzV9AuaEkZqkOLDIvlQiL6ltuZd9tGcW3gwpnX8JbuiuhFlEGmmFXEXkN50Cvq7Os88IY2v0dkDqXYWVgA==

"@types/node@*":
version "16.6.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.6.2.tgz#331b7b9f8621c638284787c5559423822fdffc50"
Expand All @@ -1088,6 +1100,16 @@
resolved "https://registry.yarnpkg.com/@types/parse-json/-/parse-json-4.0.0.tgz#2f8bb441434d163b35fb8ffdccd7138927ffb8c0"
integrity sha512-//oorEZjL6sbPcKUaCdIGlIUeH26mgzimjBB77G6XRgnDl/L5wOnpyBGRe/Mmf5CVW3PwEBE1NjiMZ/ssFh4wA==

"@types/polka@^0.5.3":
version "0.5.3"
resolved "https://registry.yarnpkg.com/@types/polka/-/polka-0.5.3.tgz#c2f76b9a5b7d5f5a9771320cc6164bde10c07819"
integrity sha512-Myj8ztBAH63xXhff5OMGiHIj37O4RG7GTDmFL/Amb05BAdG8IYFC90vp9IpdN7hjpZ5tkepsSvQkPvWwB8IRwQ==
dependencies:
"@types/express" "*"
"@types/express-serve-static-core" "*"
"@types/node" "*"
"@types/trouter" "*"

"@types/postcss-load-config@^3.0.1":
version "3.0.1"
resolved "https://registry.yarnpkg.com/@types/postcss-load-config/-/postcss-load-config-3.0.1.tgz#457c8b55b3271e11aa1c27baf5e58419e1a9ab9a"
Expand Down Expand Up @@ -1130,6 +1152,11 @@
resolved "https://registry.yarnpkg.com/@types/stack-utils/-/stack-utils-2.0.1.tgz#20f18294f797f2209b5f65c8e3b5c8e8261d127c"
integrity sha512-Hl219/BT5fLAaz6NDkSuhzasy49dwQS/DSdu4MdggFB8zcXv7vflBI3xp7FEmkmdDkBUI2bPUNeMttp2knYdxw==

"@types/trouter@*":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@types/trouter/-/trouter-3.1.0.tgz#8a3c7b34352b1cb454217a194e8bf03e5cdf7cc4"
integrity sha512-4LPNrqSJknLzILMVXn2P/mh0djNgFvom4T9Y1hmhaB8OBm1cY71bMMSrGRu1q5qF4JZzY6iaGT11BHmCMY/NZg==

"@types/yargs-parser@*":
version "20.2.1"
resolved "https://registry.yarnpkg.com/@types/yargs-parser/-/yargs-parser-20.2.1.tgz#3b9ce2489919d9e4fea439b76916abc34b2df129"
Expand Down

0 comments on commit b3a4c52

Please sign in to comment.