Skip to content

Commit 3b82d95

Browse files
authored
Chore: upgrade vite to v3 (#183)
* Upgrade vite * fix lint errors
1 parent e939e67 commit 3b82d95

20 files changed

+140
-208
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = {
5252
},
5353
},
5454
{
55-
files: ["*.ts"],
55+
files: ["*.ts", "*.mts"],
5656
parser: "@typescript-eslint/parser",
5757
parserOptions: {
5858
sourceType: "module",

docs-svelte-kit/.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// eslint-disable-next-line no-undef -- ignore
21
module.exports = {
32
extends: ["plugin:svelte/recommended"],
43
env: {
@@ -15,5 +14,6 @@ module.exports = {
1514
"svelte/prefer-class-directive": "error",
1615
"svelte/prefer-style-directive": "error",
1716
"svelte/spaced-html-comment": "error",
17+
"node/file-extension-in-import": "off",
1818
},
1919
}

docs-svelte-kit/build-system/build.js renamed to docs-svelte-kit/build-system/build.mts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1-
const esbuild = require("esbuild")
2-
const path = require("path")
3-
const fs = require("fs")
1+
import esbuild from "esbuild"
2+
import path from "path"
3+
import fs from "fs"
44
// const babelCore = require("@babel/core")
55
// const t = require("@babel/types")
66

7+
const dirname = path.dirname(
8+
new URL(
9+
// @ts-expect-error -- Cannot change `module` option
10+
import.meta.url,
11+
).pathname,
12+
)
13+
714
build(
8-
require.resolve("./src/eslint.mjs"),
9-
path.join(__dirname, "../shim/eslint.mjs"),
15+
path.join(dirname, "./src/eslint.mjs"),
16+
path.join(dirname, "../shim/eslint.mjs"),
1017
["assert", "util"],
1118
)
1219
build(
13-
require.resolve("../../node_modules/assert"),
14-
path.join(__dirname, "../shim/assert.mjs"),
20+
path.join(dirname, "../../node_modules/assert"),
21+
path.join(dirname, "../shim/assert.mjs"),
1522
)
1623

1724
/** build */
18-
function build(input, out, injects = []) {
25+
function build(input: string, out: string, injects: string[] = []) {
1926
// eslint-disable-next-line no-console -- ignore
2027
console.log(`build@ ${input}`)
2128
let code = bundle(input, ["path", ...injects])
@@ -24,21 +31,21 @@ function build(input, out, injects = []) {
2431
}
2532

2633
/** bundle */
27-
function bundle(entryPoint, externals) {
34+
function bundle(entryPoint: string, externals: string[]) {
2835
const result = esbuild.buildSync({
2936
entryPoints: [entryPoint],
3037
format: "esm",
3138
bundle: true,
3239
external: externals,
3340
write: false,
34-
inject: [require.resolve("./src/process-shim.mjs")],
41+
inject: [path.join(dirname, "./src/process-shim.mjs")],
3542
})
3643

3744
return `${result.outputFiles[0].text}`
3845
}
3946

4047
/** transform code */
41-
function transform(code, injects) {
48+
function transform(code: string, injects: string[]) {
4249
const newCode = code.replace(/"[a-z]+" = "[a-z]+";/, "")
4350
// const newCode = babelCore.transformSync(code, {
4451
// babelrc: false,

docs-svelte-kit/shim/module.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint require-jsdoc:0 -- shim */
2-
function createRequire() {
2+
function _createRequire() {
33
return () => null
44
}
55

6-
export { createRequire }
7-
export default { createRequire }
6+
export { _createRequire as createRequire }
7+
export default { createRequire: _createRequire }

docs-svelte-kit/src/lib/eslint/scripts/linter.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// eslint-disable-next-line node/file-extension-in-import -- ignore
21
import { rules as pluginRules } from "../../../../../src/utils/rules.ts"
32
import { Linter } from "eslint"
43
import * as svelteEslintParser from "svelte-eslint-parser"
5-
// eslint-disable-next-line node/file-extension-in-import -- ignore
4+
65
export { preprocess, postprocess } from "../../../../../src/processor/index.ts"
76

87
const linter = new Linter()

docs-svelte-kit/src/lib/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// eslint-disable-next-line node/file-extension-in-import -- ignore
21
import { rules } from "../../../src/utils/rules.ts"
32
import { readable, writable } from "svelte/store"
43
// eslint-disable-next-line node/no-missing-import -- ignore

docs-svelte-kit/tools/highlight.mjs renamed to docs-svelte-kit/tools/highlight.mts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import "prism-svelte"
77
loadLanguages(["markup", "css", "javascript"])
88

99
/** Wrap pre tag */
10-
function wrapPre(code, lang) {
10+
function wrapPre(code: string, lang: string) {
1111
const htmlCode = lang === "text" ? escapeHtml(code) : code
1212
return `<pre class="language-${lang}"><code>${htmlCode}</code></pre>`
1313
}
1414

15-
const EXTENSION_MAPPINGS = {
15+
const EXTENSION_MAPPINGS: Record<string, string | undefined> = {
1616
vue: "markup",
1717
html: "markup",
1818
svelte: "svelte",
@@ -28,7 +28,7 @@ const EXTENSION_MAPPINGS = {
2828
rs: "rust",
2929
}
3030

31-
export default (str, lang) => {
31+
export default (str: string, lang: string): string => {
3232
if (!lang) {
3333
return wrapPre(str, "text")
3434
}

docs-svelte-kit/tools/markdown-it-auto-inject-components.mjs renamed to docs-svelte-kit/tools/markdown-it-auto-inject-components.mts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
import type Md from "markdown-it"
2+
import type Token from "markdown-it/lib/token"
13
/**
24
* @param {import('markdown-it')} md
35
*/
4-
export default (md) => {
6+
export default (md: Md): void => {
57
md.core.ruler.push("auto_inject_components", (state) => {
68
const injected = new Set(extractInjectedComponents(state.tokens))
79
for (const component of new Set(
810
extractComponents(state.tokens, injected),
911
)) {
10-
const newToken = new state.Token("auto_inject_component")
12+
const newToken = new state.Token("auto_inject_component", "", 0)
1113
newToken.content = `<script>
1214
import ${component} from '$lib/components/${component}.svelte'
1315
</script>`
1416
state.tokens.unshift(newToken)
1517
}
1618

1719
/** Extract imported components */
18-
function* extractInjectedComponents(tokens) {
20+
function* extractInjectedComponents(tokens: Token[]): Iterable<string> {
1921
for (const token of tokens) {
2022
if (
2123
(token.type === "html_inline" || token.type === "html_block") &&
@@ -33,7 +35,10 @@ import ${component} from '$lib/components/${component}.svelte'
3335
}
3436

3537
/** Extract inject components */
36-
function* extractComponents(tokens, injected) {
38+
function* extractComponents(
39+
tokens: Token[],
40+
injected: Set<string>,
41+
): Iterable<string> {
3742
for (const token of tokens) {
3843
if (token.type === "html_inline" || token.type === "html_block") {
3944
const match = /<([A-Z][\w$]*)/u.exec(token.content)
@@ -47,7 +52,7 @@ import ${component} from '$lib/components/${component}.svelte'
4752
}
4853
}
4954
})
50-
// eslint-disable-next-line camelcase -- ignore
55+
5156
md.renderer.rules.auto_inject_component = (tokens, idx, _options) => {
5257
return tokens[idx].content
5358
}

docs-svelte-kit/tools/markdown-it-container-option.mjs renamed to docs-svelte-kit/tools/markdown-it-container-option.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import type containerPlugin from "markdown-it-container"
2+
type ContainerPluginOption = Parameters<typeof containerPlugin>[2]
13
/**
24
* Generate markdown-it-container option
35
* @see https://github.com/markdown-it/markdown-it-container
46
*/
5-
export default (type, defaultTitle = type.toUpperCase()) => {
7+
export default (
8+
type: string,
9+
defaultTitle = type.toUpperCase(),
10+
): ContainerPluginOption => {
611
return {
712
render(tokens, index) {
813
const token = tokens[index]

docs-svelte-kit/tools/markdown-it-markdown.mjs renamed to docs-svelte-kit/tools/markdown-it-markdown.mts

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import path from "path"
22
import spawn from "cross-spawn"
3+
import type Md from "markdown-it"
4+
5+
type TreeItem = {
6+
children: TreeItem[]
7+
}
8+
type TreeStack = { item: TreeItem; level: number; upper: TreeStack | null }
39

410
class TOCRenderer {
5-
constructor(name) {
6-
const item = { children: [] }
11+
private readonly tree: TreeItem
12+
13+
private stack: TreeStack
14+
15+
public constructor() {
16+
const item: TreeItem = { children: [] }
717
this.tree = item
818
this.stack = { item, level: 1, upper: null }
9-
this.name = name
1019
}
1120

12-
addMenu(level, id, title) {
21+
public addMenu(level: number, id: string, title: string) {
1322
if (this.stack.level < level) {
1423
const parent = this.stack.item
1524
const item = parent.children[parent.children.length - 1]
@@ -24,53 +33,52 @@ class TOCRenderer {
2433
this.stack.item.children.push(item)
2534
}
2635

27-
toc() {
36+
public toc() {
2837
return this.tree
2938
}
3039
}
3140
/**
3241
* @param {import('markdown-it')} md
3342
*/
34-
export default (md) => {
43+
export default (md: Md): void => {
3544
md.core.ruler.push("custom_markdown", (state) => {
3645
const tokens = state.tokens
37-
tokens.unshift(new state.Token("custom_markdown_data"))
46+
tokens.unshift(new state.Token("custom_markdown_data", "", 0))
3847
})
39-
// eslint-disable-next-line camelcase -- ignore
48+
4049
md.renderer.rules.custom_markdown_data = (
4150
tokens,
4251
_idx,
4352
_options,
4453
env,
4554
_self,
4655
) => {
47-
const name = path.basename(env.id).replace(/\.md$/, "")
48-
const renderer = new TOCRenderer(name)
56+
const renderer = new TOCRenderer()
4957
for (let idx = 0; idx < tokens.length; idx++) {
5058
const token = tokens[idx]
5159

5260
if (token.type !== "heading_open") {
5361
continue
5462
}
55-
let level = Number(token.tag.substr(1))
63+
const level = Number(token.tag.slice(1))
5664
if (level > 3) {
5765
continue
5866
}
5967
// Aggregate the next token children text.
60-
const title = tokens[idx + 1].children
61-
.filter(
68+
const title = tokens[idx + 1]
69+
.children!.filter(
6270
(token) =>
6371
token.type === "text" ||
6472
token.type === "emoji" ||
6573
token.type === "code_inline",
6674
)
6775
.reduce((acc, t) => acc + t.content, "")
6876

69-
let slug = token.attrGet("id")
77+
const slug = token.attrGet("id")!
7078
renderer.addMenu(level, slug, title)
7179
}
7280

73-
const fileInfo = {}
81+
const fileInfo: { timestamp?: number; lastUpdated?: string } = {}
7482
const timestamp = getGitLastUpdatedTimestamp(env.id)
7583
if (timestamp) {
7684
fileInfo.timestamp = timestamp
@@ -85,7 +93,7 @@ export default (md) => {
8593
}
8694

8795
/** Get last updated timestamp */
88-
function getGitLastUpdatedTimestamp(filePath) {
96+
function getGitLastUpdatedTimestamp(filePath: string) {
8997
let lastUpdated
9098
try {
9199
lastUpdated =

0 commit comments

Comments
 (0)