Skip to content

Commit 39a9276

Browse files
committed
refactor: Switch to JS
1 parent d3df2ac commit 39a9276

File tree

7 files changed

+166
-136
lines changed

7 files changed

+166
-136
lines changed

tsconfig.json renamed to jsconfig.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
"compilerOptions": {
33
"target": "ES2019",
44
"strict": true,
5+
"strictNullChecks": true,
56
"esModuleInterop": true,
67
"module": "node20",
78
"moduleResolution": "node16",
8-
"declaration": true,
9-
"outDir": "dist/",
10-
"skipLibCheck": true
11-
},
12-
"files": ["./src/index.ts"]
9+
"noEmit": true,
10+
"allowJs": true,
11+
"checkJs": true,
12+
"skipLibCheck": false
13+
}
1314
}

package.json

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
"version": "2.10.2",
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",
18-
"test": "premove demo/node_modules && node --test test",
19-
"prepublishOnly": "npm run build"
17+
"test": "premove demo/node_modules && node --test test"
2018
},
2119
"keywords": [
2220
"preact",
@@ -31,7 +29,7 @@
3129
},
3230
"license": "MIT",
3331
"files": [
34-
"dist/"
32+
"src/"
3533
],
3634
"dependencies": {
3735
"@babel/plugin-transform-react-jsx": "^7.27.1",

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
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 { createFilter } from "./utils.js";
76
import { parseId } from "./utils.js";
87

9-
export interface PreactDevtoolsPluginOptions {
10-
devToolsEnabled?: boolean;
11-
shouldTransform: ReturnType<typeof createFilter>;
12-
}
13-
14-
export function preactDevtoolsPlugin({
15-
devToolsEnabled,
16-
shouldTransform,
17-
}: PreactDevtoolsPluginOptions): Plugin {
8+
/**
9+
* @typedef {import('vite').Plugin} Plugin
10+
* @typedef {import('vite').ResolvedConfig} ResolvedConfig
11+
*
12+
* @typedef {import('./index.d.ts').PreactDevtoolsPluginOptions} PreactDevtoolsPluginOptions
13+
*/
14+
15+
/**
16+
* @param {PreactDevtoolsPluginOptions} options
17+
* @returns {Plugin}
18+
*/
19+
export function preactDevtoolsPlugin({ devToolsEnabled, shouldTransform }) {
1820
const log = debug("vite:preact-devtools");
1921

2022
let entry = "";
21-
let config: ResolvedConfig;
23+
/** @type {ResolvedConfig} */
24+
let config;
2225
let found = false;
2326

24-
const plugin: Plugin = {
27+
/** @type {Plugin} */
28+
const plugin = {
2529
name: "preact:devtools",
2630

2731
// Ensure that we resolve before everything else

src/index.d.ts

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

src/index.ts renamed to src/index.js

Lines changed: 27 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
import type { Plugin, ResolvedConfig } from "vite";
2-
import type { FilterPattern } from "vite";
3-
import type { ParserPlugin, ParserOptions } from "@babel/parser";
4-
import type { TransformOptions } from "@babel/core";
5-
61
import prefresh from "@prefresh/vite";
7-
import { preactDevtoolsPlugin } from "./devtools.js";
8-
import { createFilter, parseId } from "./utils.js";
92
import { vitePrerenderPlugin } from "vite-prerender-plugin";
103
import { transformAsync } from "@babel/core";
114
// @ts-ignore package doesn't ship with declaration files
@@ -14,96 +7,23 @@ import babelReactJsx from "@babel/plugin-transform-react-jsx";
147
import babelReactJsxDev from "@babel/plugin-transform-react-jsx-development";
158
// @ts-ignore package doesn't ship with declaration files
169
import babelHookNames from "babel-plugin-transform-hook-names";
10+
import { preactDevtoolsPlugin } from "./devtools.js";
11+
import { createFilter, parseId } from "./utils.js";
1712

18-
export type BabelOptions = Omit<
19-
TransformOptions,
20-
| "ast"
21-
| "filename"
22-
| "root"
23-
| "sourceFileName"
24-
| "sourceMaps"
25-
| "inputSourceMap"
26-
>;
27-
28-
export interface PreactPluginOptions {
29-
/**
30-
* Whether to use Preact devtools
31-
* @default !isProduction
32-
*/
33-
devToolsEnabled?: boolean;
34-
35-
/**
36-
* Whether to use prefresh HMR
37-
* @default true
38-
*/
39-
prefreshEnabled?: boolean;
40-
41-
/**
42-
* Whether to alias react, react-dom to preact/compat
43-
* @default true
44-
*/
45-
reactAliasesEnabled?: boolean;
46-
47-
/**
48-
* Prerender plugin options
49-
*/
50-
prerender?: {
51-
/**
52-
* Whether to prerender your app on build
53-
*/
54-
enabled: boolean;
55-
/**
56-
* Absolute path to script containing an exported `prerender()` function
57-
*/
58-
prerenderScript?: string;
59-
/**
60-
* Query selector for specifying where to insert prerender result in your HTML template
61-
*/
62-
renderTarget?: string;
63-
/**
64-
* Additional routes that should be prerendered
65-
*/
66-
additionalPrerenderRoutes?: string[];
67-
/**
68-
* Vite's preview server won't use our prerendered HTML by default, this middleware correct this
69-
*/
70-
previewMiddlewareEnabled?: boolean;
71-
/**
72-
* Path to use as a fallback/404 route, i.e., `/404` or `/not-found`
73-
*/
74-
previewMiddlewareFallback?: string;
75-
};
76-
77-
/**
78-
* RegExp or glob to match files to be transformed
79-
*/
80-
include?: FilterPattern;
81-
82-
/**
83-
* RegExp or glob to match files to NOT be transformed
84-
*/
85-
exclude?: FilterPattern;
86-
87-
/**
88-
* Babel configuration applied in both dev and prod.
89-
*/
90-
babel?: BabelOptions;
91-
/**
92-
* Import Source for jsx. Defaults to "preact".
93-
*/
94-
jsxImportSource?: string;
95-
}
96-
97-
export interface PreactBabelOptions extends BabelOptions {
98-
plugins: Extract<BabelOptions["plugins"], any[]>;
99-
presets: Extract<BabelOptions["presets"], any[]>;
100-
overrides: Extract<BabelOptions["overrides"], any[]>;
101-
parserOpts: ParserOptions & {
102-
plugins: Extract<ParserOptions["plugins"], any[]>;
103-
};
104-
}
105-
106-
// Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts
13+
/**
14+
* @typedef {import('vite').Plugin} Plugin
15+
* @typedef {import('vite').ResolvedConfig} ResolvedConfig
16+
* @typedef {import('@babel/parser').ParserPlugin} ParserPlugin
17+
*
18+
* @typedef {import('./index.d.ts').preact} PreactPlugin
19+
* @typedef {import('./index.d.ts').PreactBabelOptions} PreactBabelOptions
20+
*/
21+
22+
/**
23+
* Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts
24+
*
25+
* @type {PreactPlugin}
26+
*/
10727
function preactPlugin({
10828
devToolsEnabled,
10929
prefreshEnabled,
@@ -113,24 +33,25 @@ function preactPlugin({
11333
exclude,
11434
babel,
11535
jsxImportSource,
116-
}: PreactPluginOptions = {}): Plugin[] {
36+
} = {}) {
11737
const baseParserOptions = [
11838
"importMeta",
11939
"explicitResourceManagement",
12040
"topLevelAwait",
12141
];
122-
let config: ResolvedConfig;
42+
/** @type {ResolvedConfig} */
43+
let config;
12344

124-
let babelOptions = {
45+
let babelOptions = /** @type {PreactBabelOptions} */ ({
12546
babelrc: false,
12647
configFile: false,
12748
...babel,
128-
} as PreactBabelOptions;
49+
});
12950

13051
babelOptions.plugins ||= [];
13152
babelOptions.presets ||= [];
13253
babelOptions.overrides ||= [];
133-
babelOptions.parserOpts ||= {} as any;
54+
babelOptions.parserOpts ||= /** @type {any} */ ({});
13455
babelOptions.parserOpts.plugins ||= [];
13556

13657
let useBabel = typeof babel !== "undefined";
@@ -153,7 +74,8 @@ function preactPlugin({
15374
}
15475
}
15576

156-
const jsxPlugin: Plugin = {
77+
/** @type {Plugin} */
78+
const jsxPlugin = {
15779
name: "vite:preact-jsx",
15880
enforce: "pre",
15981
config() {
@@ -204,7 +126,7 @@ function preactPlugin({
204126

205127
if (!useBabel || !shouldTransform(id)) return;
206128

207-
const parserPlugins = [
129+
const parserPlugins = /** @type {ParserPlugin[]} */ ([
208130
...baseParserOptions,
209131
"classProperties",
210132
"classPrivateProperties",
@@ -214,7 +136,7 @@ function preactPlugin({
214136
// Whilst our limited transforms (JSX & hook names) are fine, if users
215137
// add their own, they may run into unhelpful errors. See #170
216138
/\.[cm]?tsx?$/.test(id) && typeof babel === "undefined" && "typescript",
217-
].filter(Boolean) as ParserPlugin[];
139+
].filter(Boolean));
218140

219141
const result = await transformAsync(code, {
220142
...babelOptions,
@@ -243,7 +165,7 @@ function preactPlugin({
243165
...(devToolsEnabled ? [babelHookNames] : []),
244166
],
245167
sourceMaps: true,
246-
inputSourceMap: false as any,
168+
inputSourceMap: undefined,
247169
});
248170

249171
// NOTE: Since no config file is being loaded, this path wouldn't occur.

0 commit comments

Comments
 (0)