Skip to content

Commit

Permalink
Updated the code
Browse files Browse the repository at this point in the history
- made everything more explicit, as requested
- added optional typescript opt-in behavior per extension, like
['.solid', { typescript: true }]
  • Loading branch information
high1 committed Jan 3, 2022
1 parent e5b6389 commit 2e07b2b
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const runtimePublicPath = '/@solid-refresh';
const runtimeFilePath = require.resolve('solid-refresh/dist/solid-refresh.mjs');
const runtimeCode = readFileSync(runtimeFilePath, 'utf-8');

export interface ExtensionOptions {
typescript?: boolean;
}
/** Configuration options for vite-plugin-solid. */
export interface Options {
/**
Expand Down Expand Up @@ -43,7 +46,7 @@ export interface Options {
*
* @default undefined
*/
extensions?: string[];
extensions?: (string | [string, ExtensionOptions])[];
/**
* Pass any additional babel transform options. They will be merged with
* the transformations required by Solid.
Expand Down Expand Up @@ -270,7 +273,7 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
return {
/**
* We only need esbuild on .ts or .js files.
* .tsx & .jsx files are handled by us
* .tsx & .jsx files are handled by us
*/
esbuild: { include: /\.ts$/ },
resolve: {
Expand Down Expand Up @@ -300,9 +303,24 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
// @ts-expect-error anticipate vite changing second parameter as options object
// see https://github.com/vitejs/vite/discussions/5109
const ssr: boolean = transformOptions === true || transformOptions?.ssr;

if (!(/\.[jt]sx/.test(id) || options.extensions?.includes(getExtension(id).split('?')[0])))
return null;
let extension: string;

if (!/\.[jt]sx/.test(id)) {
if (options.extensions) {
const extensionWithFlags = getExtension(id);
extension = extensionWithFlags.split('?')[0];

if (
!options.extensions
.map((ext) => (typeof ext === 'string' ? ext : ext[0]))
.includes(extension)
) {
return null;
}
} else {
return null;
}
}

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

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

if (id.includes('tsx')) opts.presets.push([ts, options.typescript || {}]);
if (
id.includes('tsx') ||
options.extensions.find((ext) =>
typeof ext === 'string' ? ext === extension : ext[0] === extension && ext[1].typescript,
)
) {
opts.presets.push([ts, options.typescript || {}]);
}

// Default value for babel user options
let babelUserOptions: TransformOptions = {};
Expand Down

0 comments on commit 2e07b2b

Please sign in to comment.