Skip to content

Commit

Permalink
Merge pull request #24 from high1/solid-mdx
Browse files Browse the repository at this point in the history
Extensions option added
  • Loading branch information
amoutonbrady authored Jan 3, 2022
2 parents 634dece + 5667873 commit 1a55ce8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
39 changes: 20 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 37 additions & 2 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 All @@ -37,6 +40,13 @@ export interface Options {
* @default true
*/
hot: boolean;
/**
* This registers additional extensions that should be processed by
* vite-plugin-solid.
*
* @default undefined
*/
extensions?: (string | [string, ExtensionOptions])[];
/**
* Pass any additional babel transform options. They will be merged with
* the transformations required by Solid.
Expand Down Expand Up @@ -230,6 +240,11 @@ export interface Options {
};
}

function getExtension(filename: string): string {
const index = filename.lastIndexOf('.');
return index < 0 ? '' : filename.substring(index);
}

export default function solidPlugin(options: Partial<Options> = {}): Plugin {
let needHmr = false;
let replaceDev = false;
Expand Down Expand Up @@ -286,8 +301,23 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {

async transform(source, id, transformOptions) {
const ssr: boolean = transformOptions?.ssr;
let extension: string;

if (!/\.[jt]sx/.test(id)) {
if (options.extensions) {
extension = getExtension(id);
if (
!options.extensions
.map((ext) => (typeof ext === 'string' ? ext : ext[0]))
.includes(extension)
) {
return null;
}
} else {
return null;
}
}

if (!/\.[jt]sx/.test(id)) return null;
const inNodeModules = /node_modules/.test(id);

let solidOptions: { generate: 'ssr' | 'dom'; hydratable: boolean };
Expand Down Expand Up @@ -315,7 +345,12 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
inputSourceMap: false as any,
};

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

Expand Down

0 comments on commit 1a55ce8

Please sign in to comment.