Skip to content

Commit

Permalink
fix: respect vite ssr option in first place
Browse files Browse the repository at this point in the history
  • Loading branch information
MrFoxPro committed Jul 16, 2023
1 parent 66955a8 commit 22da773
Showing 1 changed file with 32 additions and 19 deletions.
51 changes: 32 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { transformAsync, TransformOptions } from '@babel/core';
import ts from '@babel/preset-typescript';
import solid from 'babel-preset-solid';
import { readFileSync } from 'fs';
import { mergeAndConcat } from 'merge-anything';
import { mergeAndConcat, mergeAndCompare } from 'merge-anything';
import { createRequire } from 'module';
import solidRefresh from 'solid-refresh/babel';
import { createFilter } from 'vite';
Expand All @@ -26,12 +26,12 @@ export interface Options {
* A [picomatch](https://github.com/micromatch/picomatch) pattern, or array of patterns, which specifies the files
* the plugin should operate on.
*/
include?: FilterPattern
include?: FilterPattern;
/**
* A [picomatch](https://github.com/micromatch/picomatch) pattern, or array of patterns, which specifies the files
* to be ignored by the plugin.
*/
exclude?: FilterPattern
exclude?: FilterPattern;
/**
* This will inject solid-js/dev in place of solid-js in dev mode. Has no
* effect in prod. If set to `false`, it won't inject it in dev. This is
Expand Down Expand Up @@ -280,7 +280,7 @@ function isJestDomInstalled() {
}

export default function solidPlugin(options: Partial<Options> = {}): Plugin {
const filter = createFilter(options.include, options.exclude)
const filter = createFilter(options.include, options.exclude);

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

async transform(source, id, transformOptions) {
const isSsr = transformOptions && transformOptions.ssr;
const isSsrRequest = transformOptions?.ssr;
const currentFileExtension = getExtension(id);

const extensionsToWatch = [...(options.extensions || []), '.tsx', '.jsx'];
Expand All @@ -382,17 +382,29 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {

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

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

if (options.ssr) {
if (isSsr) {
solidOptions = { generate: 'ssr', hydratable: true };
} else {
solidOptions = { generate: 'dom', hydratable: true };
}
} else {
solidOptions = { generate: 'dom', hydratable: false };
}
const solidOptions = mergeAndCompare(
(origVal, newVal) => newVal ?? origVal,
// Default options
{
generate: 'dom',
hydratable: false,
},
// `ssr` shorthand
{
generate: options.ssr ? 'ssr' : undefined,
hydratable: options.ssr ? true : undefined,
},
// direct solid options
{
generate: options.solid?.generate,
hydratable: options.solid?.hydratable,
},
// vite request
{
generate: isSsrRequest ? 'ssr' : undefined,
},
);
console.log(solidOptions);

id = id.replace(/\?.+$/, '');

Expand All @@ -402,8 +414,9 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {
root: projectRoot,
filename: id,
sourceFileName: id,
presets: [[solid, { ...solidOptions, ...(options.solid || {}) }]],
plugins: needHmr && !isSsr && !inNodeModules ? [[solidRefresh, { bundler: 'vite' }]] : [],
presets: [[solid, solidOptions]],
plugins:
needHmr && !isSsrRequest && !inNodeModules ? [[solidRefresh, { bundler: 'vite' }]] : [],
sourceMaps: true,
// Vite handles sourcemap flattening
inputSourceMap: false as any,
Expand All @@ -430,7 +443,7 @@ export default function solidPlugin(options: Partial<Options> = {}): Plugin {

if (options.babel) {
if (typeof options.babel === 'function') {
const babelOptions = options.babel(source, id, isSsr);
const babelOptions = options.babel(source, id, isSsrRequest);
babelUserOptions = babelOptions instanceof Promise ? await babelOptions : babelOptions;
} else {
babelUserOptions = options.babel;
Expand Down

0 comments on commit 22da773

Please sign in to comment.