Skip to content

Commit 2e07b2b

Browse files
committed
Updated the code
- made everything more explicit, as requested - added optional typescript opt-in behavior per extension, like ['.solid', { typescript: true }]
1 parent e5b6389 commit 2e07b2b

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/index.ts

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ const runtimePublicPath = '/@solid-refresh';
1313
const runtimeFilePath = require.resolve('solid-refresh/dist/solid-refresh.mjs');
1414
const runtimeCode = readFileSync(runtimeFilePath, 'utf-8');
1515

16+
export interface ExtensionOptions {
17+
typescript?: boolean;
18+
}
1619
/** Configuration options for vite-plugin-solid. */
1720
export interface Options {
1821
/**
@@ -43,7 +46,7 @@ export interface Options {
4346
*
4447
* @default undefined
4548
*/
46-
extensions?: string[];
49+
extensions?: (string | [string, ExtensionOptions])[];
4750
/**
4851
* Pass any additional babel transform options. They will be merged with
4952
* the transformations required by Solid.
@@ -270,7 +273,7 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
270273
return {
271274
/**
272275
* We only need esbuild on .ts or .js files.
273-
* .tsx & .jsx files are handled by us
276+
* .tsx & .jsx files are handled by us
274277
*/
275278
esbuild: { include: /\.ts$/ },
276279
resolve: {
@@ -300,9 +303,24 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
300303
// @ts-expect-error anticipate vite changing second parameter as options object
301304
// see https://github.com/vitejs/vite/discussions/5109
302305
const ssr: boolean = transformOptions === true || transformOptions?.ssr;
303-
304-
if (!(/\.[jt]sx/.test(id) || options.extensions?.includes(getExtension(id).split('?')[0])))
305-
return null;
306+
let extension: string;
307+
308+
if (!/\.[jt]sx/.test(id)) {
309+
if (options.extensions) {
310+
const extensionWithFlags = getExtension(id);
311+
extension = extensionWithFlags.split('?')[0];
312+
313+
if (
314+
!options.extensions
315+
.map((ext) => (typeof ext === 'string' ? ext : ext[0]))
316+
.includes(extension)
317+
) {
318+
return null;
319+
}
320+
} else {
321+
return null;
322+
}
323+
}
306324

307325
const inNodeModules = /node_modules/.test(id);
308326

@@ -331,7 +349,14 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
331349
inputSourceMap: false as any,
332350
};
333351

334-
if (id.includes('tsx')) opts.presets.push([ts, options.typescript || {}]);
352+
if (
353+
id.includes('tsx') ||
354+
options.extensions.find((ext) =>
355+
typeof ext === 'string' ? ext === extension : ext[0] === extension && ext[1].typescript,
356+
)
357+
) {
358+
opts.presets.push([ts, options.typescript || {}]);
359+
}
335360

336361
// Default value for babel user options
337362
let babelUserOptions: TransformOptions = {};

0 commit comments

Comments
 (0)