Skip to content

Commit 379b53a

Browse files
committed
refactor: Switch to JS
1 parent 3a12007 commit 379b53a

7 files changed

Lines changed: 166 additions & 140 deletions

File tree

tsconfig.json renamed to jsconfig.json

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

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 { RollupFilter } from "./utils.js";
76
import { parseId } from "./utils.js";
87

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

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

Lines changed: 30 additions & 106 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 "@rollup/pluginutils";
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,25 @@ 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";
17-
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
10+
import { createFilter } from "@rollup/pluginutils";
11+
import { preactDevtoolsPlugin } from "./devtools.js";
12+
import { parseId } from "./utils.js";
13+
14+
/**
15+
* @typedef {import('vite').Plugin} Plugin
16+
* @typedef {import('vite').ResolvedConfig} ResolvedConfig
17+
* @typedef {import('@babel/parser').ParserPlugin} ParserPlugin
18+
*
19+
* @typedef {import('./index.d.ts').PreactPluginOptions} PreactPluginOptions
20+
* @typedef {import('./index.d.ts').PreactBabelOptions} PreactBabelOptions
21+
*/
22+
23+
/**
24+
* Taken from https://github.com/vitejs/vite/blob/main/packages/plugin-react/src/index.ts
25+
*
26+
* @param {PreactPluginOptions} options
27+
* @returns {import('vite').Plugin[]}
28+
*/
10729
function preactPlugin({
10830
devToolsEnabled,
10931
prefreshEnabled,
@@ -113,24 +35,25 @@ function preactPlugin({
11335
exclude,
11436
babel,
11537
jsxImportSource,
116-
}: PreactPluginOptions = {}): Plugin[] {
38+
} = {}) {
11739
const baseParserOptions = [
11840
"importMeta",
11941
"explicitResourceManagement",
12042
"topLevelAwait",
12143
];
122-
let config: ResolvedConfig;
44+
/** @type {ResolvedConfig} */
45+
let config;
12346

124-
let babelOptions = {
47+
let babelOptions = /** @type {PreactBabelOptions} */ ({
12548
babelrc: false,
12649
configFile: false,
12750
...babel,
128-
} as PreactBabelOptions;
51+
});
12952

13053
babelOptions.plugins ||= [];
13154
babelOptions.presets ||= [];
13255
babelOptions.overrides ||= [];
133-
babelOptions.parserOpts ||= {} as any;
56+
babelOptions.parserOpts ||= /** @type {any} */ ({});
13457
babelOptions.parserOpts.plugins ||= [];
13558

13659
let useBabel = typeof babel !== "undefined";
@@ -153,7 +76,8 @@ function preactPlugin({
15376
}
15477
}
15578

156-
const jsxPlugin: Plugin = {
79+
/** @type {Plugin} */
80+
const jsxPlugin = {
15781
name: "vite:preact-jsx",
15882
enforce: "pre",
15983
config() {
@@ -204,7 +128,7 @@ function preactPlugin({
204128

205129
if (!useBabel || !shouldTransform(id)) return;
206130

207-
const parserPlugins = [
131+
const parserPlugins = /** @type {ParserPlugin[]} */ ([
208132
...baseParserOptions,
209133
"classProperties",
210134
"classPrivateProperties",
@@ -214,7 +138,7 @@ function preactPlugin({
214138
// Whilst our limited transforms (JSX & hook names) are fine, if users
215139
// add their own, they may run into unhelpful errors. See #170
216140
/\.[cm]?tsx?$/.test(id) && typeof babel !== "undefined" && "typescript",
217-
].filter(Boolean) as ParserPlugin[];
141+
].filter(Boolean));
218142

219143
const result = await transformAsync(code, {
220144
...babelOptions,
@@ -243,7 +167,7 @@ function preactPlugin({
243167
...(devToolsEnabled ? [babelHookNames] : []),
244168
],
245169
sourceMaps: true,
246-
inputSourceMap: false as any,
170+
inputSourceMap: null,
247171
});
248172

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

0 commit comments

Comments
 (0)