Skip to content

Commit 3a249f3

Browse files
committed
🎨 Refactor code
1 parent 0a14b3a commit 3a249f3

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

src/index.ts

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

16+
/** Possible options for the extensions property */
1617
export interface ExtensionOptions {
1718
typescript?: boolean;
1819
}
20+
1921
/** Configuration options for vite-plugin-solid. */
2022
export interface Options {
2123
/**
@@ -300,30 +302,25 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
300302
},
301303

302304
async transform(source, id, transformOptions) {
303-
const ssr: boolean = transformOptions?.ssr;
304-
let extension: string;
305-
306-
if (!/\.[jt]sx/.test(id)) {
307-
if (options.extensions) {
308-
extension = getExtension(id);
309-
if (
310-
!options.extensions
311-
.map((ext) => (typeof ext === 'string' ? ext : ext[0]))
312-
.includes(extension)
313-
) {
314-
return null;
315-
}
316-
} else {
317-
return null;
318-
}
305+
const isSsr = transformOptions?.ssr;
306+
const currentFileExtension = getExtension(id);
307+
308+
const extensionsToWatch = [...(options.extensions || []), '.tsx', '.jsx'];
309+
const allCustomExtensions = extensionsToWatch.map((extension) =>
310+
// An extension can be a string or a tuple [extension, options]
311+
typeof extension === 'string' ? extension : extension[0],
312+
);
313+
314+
if (!allCustomExtensions.includes(currentFileExtension)) {
315+
return null;
319316
}
320317

321318
const inNodeModules = /node_modules/.test(id);
322319

323320
let solidOptions: { generate: 'ssr' | 'dom'; hydratable: boolean };
324321

325322
if (options.ssr) {
326-
if (ssr) {
323+
if (isSsr) {
327324
solidOptions = { generate: 'ssr', hydratable: true };
328325
} else {
329326
solidOptions = { generate: 'dom', hydratable: true };
@@ -345,12 +342,19 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
345342
inputSourceMap: false as any,
346343
};
347344

348-
if (
349-
id.includes('tsx') ||
350-
options.extensions?.find(
351-
(ext) => typeof ext !== 'string' && ext[0] === extension && ext[1].typescript,
352-
)
353-
) {
345+
// We need to know if the current file extension has a typescript options tied to it
346+
const shouldBeProcessedWithTypescript = extensionsToWatch.some((extension) => {
347+
if (typeof extension === 'string') {
348+
return extension.includes('tsx');
349+
}
350+
351+
const [extensionName, extensionOptions] = extension;
352+
if (extensionName !== currentFileExtension) return false;
353+
354+
return extensionOptions.typescript;
355+
});
356+
357+
if (shouldBeProcessedWithTypescript) {
354358
opts.presets.push([ts, options.typescript || {}]);
355359
}
356360

@@ -359,7 +363,7 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
359363

360364
if (options.babel) {
361365
if (typeof options.babel === 'function') {
362-
const babelOptions = options.babel(source, id, ssr);
366+
const babelOptions = options.babel(source, id, isSsr);
363367
babelUserOptions = babelOptions instanceof Promise ? await babelOptions : babelOptions;
364368
} else {
365369
babelUserOptions = options.babel;

0 commit comments

Comments
 (0)