Skip to content

Commit 9d61e11

Browse files
committed
refactor: Switch to JS
1 parent 3a12007 commit 9d61e11

7 files changed

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

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

Lines changed: 29 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,24 @@ 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').preact} preactPlugin
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+
* @type {preactPlugin}
27+
*/
10728
function preactPlugin({
10829
devToolsEnabled,
10930
prefreshEnabled,
@@ -113,24 +34,25 @@ function preactPlugin({
11334
exclude,
11435
babel,
11536
jsxImportSource,
116-
}: PreactPluginOptions = {}): Plugin[] {
37+
} = {}) {
11738
const baseParserOptions = [
11839
"importMeta",
11940
"explicitResourceManagement",
12041
"topLevelAwait",
12142
];
122-
let config: ResolvedConfig;
43+
/** @type {ResolvedConfig} */
44+
let config;
12345

124-
let babelOptions = {
46+
let babelOptions = /** @type {PreactBabelOptions} */ ({
12547
babelrc: false,
12648
configFile: false,
12749
...babel,
128-
} as PreactBabelOptions;
50+
});
12951

13052
babelOptions.plugins ||= [];
13153
babelOptions.presets ||= [];
13254
babelOptions.overrides ||= [];
133-
babelOptions.parserOpts ||= {} as any;
55+
babelOptions.parserOpts ||= /** @type {any} */ ({});
13456
babelOptions.parserOpts.plugins ||= [];
13557

13658
let useBabel = typeof babel !== "undefined";
@@ -153,7 +75,8 @@ function preactPlugin({
15375
}
15476
}
15577

156-
const jsxPlugin: Plugin = {
78+
/** @type {Plugin} */
79+
const jsxPlugin = {
15780
name: "vite:preact-jsx",
15881
enforce: "pre",
15982
config() {
@@ -204,7 +127,7 @@ function preactPlugin({
204127

205128
if (!useBabel || !shouldTransform(id)) return;
206129

207-
const parserPlugins = [
130+
const parserPlugins = /** @type {ParserPlugin[]} */ ([
208131
...baseParserOptions,
209132
"classProperties",
210133
"classPrivateProperties",
@@ -214,7 +137,7 @@ function preactPlugin({
214137
// Whilst our limited transforms (JSX & hook names) are fine, if users
215138
// add their own, they may run into unhelpful errors. See #170
216139
/\.[cm]?tsx?$/.test(id) && typeof babel !== "undefined" && "typescript",
217-
].filter(Boolean) as ParserPlugin[];
140+
].filter(Boolean));
218141

219142
const result = await transformAsync(code, {
220143
...babelOptions,
@@ -243,7 +166,7 @@ function preactPlugin({
243166
...(devToolsEnabled ? [babelHookNames] : []),
244167
],
245168
sourceMaps: true,
246-
inputSourceMap: false as any,
169+
inputSourceMap: undefined,
247170
});
248171

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

0 commit comments

Comments
 (0)