Skip to content

Commit 202e295

Browse files
committed
refactor: Switch to JS (#180)
* refactor: Switch to JS * ci: No more build script * ci: Add type-checking step * chore: Patch broken types in '@rollup/pluginutils'
1 parent b4c1638 commit 202e295

11 files changed

Lines changed: 217 additions & 174 deletions

File tree

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ jobs:
1818
cache: "npm"
1919
cache-dependency-path: "**/package-lock.json"
2020
- run: npm ci
21-
- run: npm run build
2221
- run: npm test
22+
- run: npm run test:types

jsconfig.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2024",
4+
"strict": true,
5+
"strictNullChecks": true,
6+
"noImplicitAny": true,
7+
"esModuleInterop": true,
8+
"module": "preserve",
9+
"noEmit": true,
10+
"allowJs": true,
11+
"checkJs": true,
12+
"skipLibCheck": false,
13+
"maxNodeModuleJsDepth": 0
14+
},
15+
"include": ["src/**/*"],
16+
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,19 @@
33
"version": "2.10.3",
44
"description": "Preact preset for the vite bundler",
55
"type": "module",
6-
"main": "./dist/index.js",
6+
"main": "./src/index.js",
77
"exports": {
8-
".": "./dist/index.js",
8+
".": "./src/index.js",
99
"./package.json": "./package.json"
1010
},
11-
"types": "dist/index.d.ts",
11+
"types": "src/index.d.ts",
1212
"scripts": {
1313
"prepare": "npx simple-git-hooks",
1414
"dev": "vite demo",
1515
"dev:build": "vite build demo",
1616
"dev:preview": "vite preview demo",
17-
"build": "premove dist && tsc",
1817
"test": "premove demo/node_modules && node --test test",
19-
"prepublishOnly": "npm run build"
18+
"test:types": "tsc -p jsconfig.json"
2019
},
2120
"keywords": [
2221
"preact",
@@ -31,7 +30,7 @@
3130
},
3231
"license": "MIT",
3332
"files": [
34-
"dist/"
33+
"src/"
3534
],
3635
"dependencies": {
3736
"@babel/plugin-transform-react-jsx": "^7.27.1",
@@ -53,7 +52,7 @@
5352
"@babel/core": "^7.28.5",
5453
"@types/babel__core": "^7.20.5",
5554
"@types/debug": "^4.1.12",
56-
"@types/node": "^20.19.24",
55+
"@types/node": "^20.19.37",
5756
"nano-staged": "^0.8.0",
5857
"preact": "^10.27.2",
5958
"preact-iso": "^2.11.0",

src/devtools.ts renamed to src/devtools.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
import { Plugin, ResolvedConfig, normalizePath } from "vite";
1+
import { normalizePath } from "vite";
22
import path from "path";
33
import debug from "debug";
44
import pc from "picocolors";
55

6-
import type { RollupFilter } from "./utils.js";
76
import { parseId } from "./utils.js";
87

9-
export interface PreactDevtoolsPluginOptions {
10-
devToolsEnabled?: boolean;
11-
shouldTransform: RollupFilter;
12-
}
8+
/** @import { Plugin, ResolvedConfig } from "vite"; */
9+
10+
/** @import { PreactDevtoolsPluginOptions } from "./index.d.ts" */
1311

14-
export function preactDevtoolsPlugin({
15-
devToolsEnabled,
16-
shouldTransform,
17-
}: PreactDevtoolsPluginOptions): Plugin {
12+
/**
13+
* @param {PreactDevtoolsPluginOptions} options
14+
* @returns {Plugin}
15+
*/
16+
export function preactDevtoolsPlugin({ devToolsEnabled, shouldTransform }) {
1817
const log = debug("vite:preact-devtools");
1918

2019
let entry = "";
21-
let config: ResolvedConfig;
20+
/** @type {ResolvedConfig} */
21+
let config;
2222
let found = false;
2323

24-
const plugin: Plugin = {
24+
/** @type {Plugin} */
25+
const plugin = {
2526
name: "preact:devtools",
2627

2728
// Ensure that we resolve before everything else

src/index.d.ts

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import type { Plugin } from "vite";
2+
import type { CreateFilter, FilterPattern } from "@rollup/pluginutils";
3+
import type { ParserOptions } from "@babel/parser";
4+
import type { TransformOptions } from "@babel/core";
5+
import type { Node } from "estree";
6+
7+
export type BabelOptions = Omit<
8+
TransformOptions,
9+
| "ast"
10+
| "filename"
11+
| "root"
12+
| "sourceFileName"
13+
| "sourceMaps"
14+
| "inputSourceMap"
15+
>;
16+
17+
export interface PreactPluginOptions {
18+
/**
19+
* Whether to use Preact devtools
20+
* @default !isProduction
21+
*/
22+
devToolsEnabled?: boolean;
23+
24+
/**
25+
* Whether to use prefresh HMR
26+
* @default true
27+
*/
28+
prefreshEnabled?: boolean;
29+
30+
/**
31+
* Whether to alias react, react-dom to preact/compat
32+
* @default true
33+
*/
34+
reactAliasesEnabled?: boolean;
35+
36+
/**
37+
* Prerender plugin options
38+
*/
39+
prerender?: {
40+
/**
41+
* Whether to prerender your app on build
42+
*/
43+
enabled: boolean;
44+
/**
45+
* Absolute path to script containing an exported `prerender()` function
46+
*/
47+
prerenderScript?: string;
48+
/**
49+
* Query selector for specifying where to insert prerender result in your HTML template
50+
*/
51+
renderTarget?: string;
52+
/**
53+
* Additional routes that should be prerendered
54+
*/
55+
additionalPrerenderRoutes?: string[];
56+
/**
57+
* Vite's preview server won't use our prerendered HTML by default, this middleware correct this
58+
*/
59+
previewMiddlewareEnabled?: boolean;
60+
/**
61+
* Path to use as a fallback/404 route, i.e., `/404` or `/not-found`
62+
*/
63+
previewMiddlewareFallback?: string;
64+
};
65+
66+
/**
67+
* RegExp or glob to match files to be transformed
68+
*/
69+
include?: FilterPattern;
70+
71+
/**
72+
* RegExp or glob to match files to NOT be transformed
73+
*/
74+
exclude?: FilterPattern;
75+
76+
/**
77+
* Babel configuration applied in both dev and prod.
78+
*/
79+
babel?: BabelOptions;
80+
/**
81+
* Import Source for jsx. Defaults to "preact".
82+
*/
83+
jsxImportSource?: string;
84+
}
85+
86+
export interface PreactBabelOptions extends BabelOptions {
87+
plugins: Extract<BabelOptions["plugins"], any[]>;
88+
presets: Extract<BabelOptions["presets"], any[]>;
89+
overrides: Extract<BabelOptions["overrides"], any[]>;
90+
parserOpts: ParserOptions & {
91+
plugins: Extract<ParserOptions["plugins"], any[]>;
92+
};
93+
}
94+
95+
type RollupFilter = ReturnType<CreateFilter>;
96+
97+
export interface PreactDevtoolsPluginOptions {
98+
devToolsEnabled?: boolean;
99+
shouldTransform: RollupFilter;
100+
}
101+
102+
export interface TransformHookNamesPluginOptions {
103+
devToolsEnabled?: boolean;
104+
shouldTransform: RollupFilter;
105+
}
106+
107+
export type NodeWithRange<N extends Node = Node> = N & {
108+
start: number;
109+
end: number;
110+
};
111+
112+
declare function preactPlugin(options?: PreactPluginOptions): Plugin[];
113+
114+
export default preactPlugin;
115+
export { preactPlugin as preact };

0 commit comments

Comments
 (0)