Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect vite ssr option in first place when transforming. #105

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 31 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,28 @@ 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,
},
);

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

Expand All @@ -402,8 +413,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 +442,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