Skip to content

Commit 1283bc8

Browse files
committed
chore: port to typescript
1 parent 5653636 commit 1283bc8

File tree

15 files changed

+841
-127
lines changed

15 files changed

+841
-127
lines changed

.prettierignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
**Fixes**
1111

12-
- only add heading to TOC if text is defined
12+
- Add heading to TOC if text is defined
1313

1414
## [1.3.0](https://github.com/metonym/svelte-readme/releases/tag/v1.3.0) - 2020-12-21
1515

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ import svelte from "rollup-plugin-svelte";
3838
import svelteReadme from "svelte-readme";
3939
import pkg from "./package.json";
4040

41-
const DEV = process.env.ROLLUP_WATCH;
42-
const BUNDLE = process.env.BUNDLE === "true";
43-
4441
export default () => {
45-
if (!BUNDLE) {
42+
if (process.env.BUNDLE !== "true") {
4643
return svelteReadme({
47-
minify: !DEV,
48-
prefixUrl: "https://github.com/metonym/svelte-readme/tree/master/",
44+
minify: !process.env.ROLLUP_WATCH,
45+
svelte: {
46+
compilerOptions: {
47+
immutable: true,
48+
},
49+
},
4950
});
5051
}
5152

package.json

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,49 @@
55
"description": "Use your README.md file for Svelte component documentation",
66
"author": "Eric Liu (https://github.com/metonym)",
77
"main": "./dist/index.js",
8+
"types": "./dist/index.d.ts",
89
"scripts": {
9-
"dev": "node scripts/preprocessStyles && rollup -cw",
10-
"prepack": "rollup -c"
11-
},
12-
"peerDependencies": {
13-
"svelte": "^3.20.x"
10+
"preprocess": "node scripts/preprocessStyles",
11+
"dev": "yarn preprocess && tsc -w",
12+
"prepack": "yarn preprocess && tsc"
1413
},
1514
"dependencies": {
16-
"@rollup/plugin-node-resolve": "^10.0.0",
15+
"@rollup/plugin-node-resolve": "^11.0.1",
1716
"@rollup/plugin-virtual": "^2.0.3",
1817
"fs-extra": "^9.0.1",
1918
"html-minifier": "^4.0.0",
2019
"is-relative-url": "^3.0.0",
21-
"markdown-it": "^12.0.2",
20+
"markdown-it": "^12.0.4",
2221
"markdown-it-anchor": "^6.0.1",
2322
"prettier": "^2.2.1",
24-
"prettier-plugin-svelte": "^1.4.1",
23+
"prettier-plugin-svelte": "^1.4.2",
2524
"prism-svelte": "^0.4.7",
2625
"prismjs": "^1.22.0",
27-
"rollup": "^2.33.3",
28-
"rollup-plugin-svelte": "^6.1.1",
26+
"rollup": "^2.35.1",
27+
"rollup-plugin-svelte": "^7.0.0",
2928
"rollup-plugin-terser": "^7.0.2"
3029
},
3130
"devDependencies": {
31+
"@types/fs-extra": "^9.0.6",
32+
"@types/html-minifier": "^4.0.0",
33+
"@types/markdown-it": "^12.0.0",
34+
"@types/markdown-it-anchor": "^4.0.4",
35+
"@types/prettier": "^2.1.6",
36+
"@types/prismjs": "^1.16.2",
37+
"@types/rollup__plugin-virtual": "^2.0.1",
3238
"github-markdown-css": "4.0.0",
33-
"postcss": "^8.1.10",
34-
"svelte": "^3.30.0"
39+
"postcss": "^8.2.2",
40+
"svelte": "^3.31.0",
41+
"ts-node-dev": "^1.1.1",
42+
"typescript": "^4.1.3"
3543
},
3644
"repository": {
3745
"type": "git",
3846
"url": "https://github.com/metonym/svelte-readme"
3947
},
4048
"homepage": "https://github.com/metonym/svelte-readme",
4149
"bugs": "https://github.com/metonym/svelte-readme/issues",
42-
"keywords": [],
50+
"keywords": ["svelte", "svelte-component", "documentation"],
4351
"files": [
4452
"dist"
4553
],

rollup.config.js

Lines changed: 0 additions & 18 deletions
This file was deleted.

scripts/preprocessStyles.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ const plugin = postcss.plugin("postcss-plugin", () => {
2525
postcss(plugin)
2626
.process(github_css, { from: undefined })
2727
.then((result) => {
28-
fs.writeFileSync("src/style.js", `export const css = \`${result.css}\`;`);
28+
fs.writeFileSync("src/style.ts", `export const css = \`${result.css}\`;`);
2929
});

src/createConfig.js renamed to src/createConfig.ts

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import resolve from "@rollup/plugin-node-resolve";
2-
import rollupPluginSvelte from "rollup-plugin-svelte";
2+
import rollupPluginSvelte, { Options as RollupPluginSvelteOptions } from "rollup-plugin-svelte";
33
import virtual from "@rollup/plugin-virtual";
44
import { terser } from "rollup-plugin-terser";
55
import { preprocessReadme } from "./preprocessReadme";
@@ -8,6 +8,7 @@ import path from "path";
88
import { createHash } from "crypto";
99
import htmlminifier from "html-minifier";
1010
import { css } from "./style";
11+
import { Plugin, OutputOptions, InputOptions } from "rollup";
1112

1213
function hashREADME() {
1314
try {
@@ -24,6 +25,7 @@ function getPackageJSON() {
2425
try {
2526
const path_pkg = path.join(process.cwd(), "package.json");
2627
const pkg = JSON.parse(fs.readFileSync(path_pkg, "utf-8"));
28+
2729
if (!pkg.name) throw Error(`Package name is required as "name".`);
2830
if (!pkg.svelte) throw Error(`Svelte code entry is required as "svelte".`);
2931

@@ -75,19 +77,57 @@ const custom_css = `
7577
}
7678
`;
7779

78-
/**
79-
* createConfig
80-
* @param {Object} opts
81-
* @param {boolean} opts.minify - set to `true` to minify the HTML/JS
82-
* @param {string} opts.outDir - set the folder to emit the files
83-
* @param {string} opts.style - custom CSS appended to the <style> block
84-
* @param {boolean} opts.disableDefaultCSS - set to `true` to omit the default GitHub styles
85-
* @param {string} opts.prefixUrl - Value to prepend to relative URLs (i.e. GitHub repo URL)
86-
* @param {object} opts.svelte - `rollup-plugin-svelte` options
87-
* @param {Array} opts.plugins - Rollup plugins
88-
* @param {object} opts.output - Rollup output options
89-
*/
90-
export default function createConfig(opts) {
80+
interface CreateConfigOptions {
81+
/**
82+
* set to `true` to minify the HTML/JS
83+
* @default false
84+
*/
85+
minify: boolean;
86+
87+
/**
88+
* set the folder to emit the files
89+
* @default "dist"
90+
*/
91+
outDir: string;
92+
93+
/**
94+
* custom CSS appended to the <style> block
95+
* @default ""
96+
*/
97+
style: string;
98+
99+
/**
100+
* set to `true` to omit the default GitHub styles
101+
* @default false
102+
*/
103+
disableDefaultCSS: boolean;
104+
105+
/**
106+
* value to prepend to relative URLs (i.e. GitHub repo URL)
107+
* @default undefined
108+
*/
109+
prefixUrl: string;
110+
111+
/**
112+
* `rollup-plugin-svelte` options
113+
* @default {}
114+
*/
115+
svelte: RollupPluginSvelteOptions;
116+
117+
/**
118+
* Rollup plugins
119+
* @default {[]}
120+
*/
121+
plugins: Plugin[];
122+
123+
/**
124+
* Rollup output options
125+
* @default {{}}
126+
*/
127+
output: OutputOptions;
128+
}
129+
130+
export default function createConfig(opts: Partial<CreateConfigOptions>): InputOptions {
91131
const minify = opts.minify === true;
92132
const pkg = getPackageJSON();
93133
const hash = minify ? hashREADME() : "";
@@ -131,6 +171,7 @@ export default function createConfig(opts) {
131171
return {
132172
watch: { clearScreen: false },
133173
input: "entry",
174+
// @ts-ignore
134175
output: {
135176
format: "iife",
136177
name: "app",
@@ -151,6 +192,6 @@ export default function createConfig(opts) {
151192
resolve(),
152193
...(opts.plugins || []),
153194
minify && terser(),
154-
].filter(Boolean),
195+
].filter(Boolean) as Plugin[],
155196
};
156197
}

src/global.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
declare module "prismjs/components/*";
2+
3+
declare module "prism-svelte";
File renamed without changes.

src/preprocessReadme.js renamed to src/preprocessReadme.ts

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,24 @@ import Prism from "prismjs";
66
import "prismjs/components/prism-bash";
77
import "prism-svelte";
88
import isRelativeUrl from "is-relative-url";
9+
import { PreprocessorGroup } from "svelte/types/compiler/preprocess";
10+
import { Node } from "estree-walker";
911

10-
const aliases = {
12+
const aliases: Record<string, string> = {
1113
sh: "bash",
1214
js: "javascript",
1315
};
1416

15-
let md;
17+
let md: Markdown;
1618

17-
export function preprocessReadme(opts) {
19+
interface PreprocessReadmeOptions {
20+
name: string;
21+
svelte: string;
22+
prefixUrl: string;
23+
homepage: string;
24+
}
25+
26+
export function preprocessReadme(opts: Partial<PreprocessReadmeOptions>): Pick<PreprocessorGroup, "markup"> {
1827
const prefixUrl = opts.prefixUrl || `${opts.homepage}/tree/master/`;
1928

2029
if (!md) {
@@ -53,6 +62,7 @@ export function preprocessReadme(opts) {
5362
}
5463

5564
return {
65+
// @ts-ignore
5666
markup: ({ content, filename }) => {
5767
if (/node_modules/.test(filename) || !filename.endsWith(".md")) return null;
5868

@@ -68,10 +78,10 @@ export function preprocessReadme(opts) {
6878
let result = md.render(content);
6979
let cursor = 0;
7080

71-
const ast = parse(result);
81+
const ast = (parse(result) as unknown) as Node;
7282

7383
let headings = [];
74-
let prev;
84+
let prev: undefined | "h2" | "h3" = undefined;
7585

7686
walk(ast, {
7787
enter(node, parent) {
@@ -93,6 +103,7 @@ export function preprocessReadme(opts) {
93103
}
94104

95105
if (node.type === "Element" && node.name === "h2") {
106+
// @ts-ignore
96107
const id = node.attributes.find((attr) => attr.name === "id").value[0].raw;
97108

98109
if (id === "table-of-contents") return;
@@ -111,6 +122,7 @@ export function preprocessReadme(opts) {
111122
}
112123

113124
if (node.type === "Element" && node.name === "h3") {
125+
// @ts-ignore
114126
const id = node.attributes.find((attr) => attr.name === "id").value[0].raw;
115127
const text = node.children[0].raw;
116128

@@ -128,10 +140,10 @@ export function preprocessReadme(opts) {
128140
if (node.type === "Attribute" && node.name === "data-svelte") {
129141
const raw_value = node.value[0].raw;
130142
const value = decodeURI(raw_value);
131-
const value_ast = parse(value);
143+
const value_ast = (parse(value) as unknown) as Node;
132144
const markup =
133145
`<div class="code-fence">` + value.slice(value_ast.html.start, value_ast.html.end) + "</div>";
134-
const replace = result.slice(parent.start + cursor, parent.end + cursor);
146+
const replace = result.slice(parent!.start + cursor, parent!.end + cursor);
135147
result = result.replace(replace, markup + replace.replace(raw_value, ""));
136148
cursor += markup.length - raw_value.length;
137149

@@ -156,10 +168,9 @@ export function preprocessReadme(opts) {
156168
);
157169

158170
return {
159-
code: `
160-
<script>${script_content}</script>
161-
<style>${style_content}</style>
162-
<main class="markdown-body">${result}</main>`,
171+
code: `<script>${script_content}</script>
172+
<style>${style_content}</style>
173+
<main class="markdown-body">${result}</main>`,
163174
};
164175
},
165176
};

0 commit comments

Comments
 (0)