Skip to content
Merged
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@ jobs:
- name: Install Playwright
run: pnpm exec playwright install chromium

- name: Build loaders
run: node --run build

- name: Run Test
run: node --run test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ test-results
!.vscode/settings.json
!.vscode/extensions.json
.idea
playground/test-temp-*/
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
24
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
MIT License

Copyright (c) 2024 Rspack Contrib
Copyright (c) 2023-present Bytedance, Inc. and its affiliates.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
97 changes: 69 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,50 +1,91 @@
# rsbuild-plugin-example
# solid-loader

Example plugin for Rsbuild.

<p>
<a href="https://npmjs.com/package/rsbuild-plugin-example">
<img src="https://img.shields.io/npm/v/rsbuild-plugin-example?style=flat-square&colorA=564341&colorB=EDED91" alt="npm version" />
</a>
<img src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="license" />
<a href="https://npmcharts.com/compare/rsbuild-plugin-example?minimal=true"><img src="https://img.shields.io/npm/dm/rsbuild-plugin-example.svg?style=flat-square&colorA=564341&colorB=EDED91" alt="downloads" /></a>
</p>
Compile Solid 2 JSX and TSX in Rspack 2. The default loader uses the Solid native compiler. The package also provides a Babel backend and a separate loader for Solid Refresh.

## Usage

Install:
Install the loader in a Solid 2 application:

```bash
npm add rsbuild-plugin-example -D
```sh
pnpm add solid-js@2 @solidjs/web@2
pnpm add -D solid-loader
```

Add plugin to your `rsbuild.config.ts`:
Add the loader to the JSX rule in your Rspack config:

```ts
// rsbuild.config.ts
import { pluginExample } from 'rsbuild-plugin-example';
// rspack.config.ts
import { defineConfig } from '@rspack/cli';

export default {
plugins: [pluginExample()],
};
export default defineConfig({
resolve: {
extensions: ['.tsx', '.jsx', '.ts', '...'],
conditionNames: ['solid', '...'],
},
module: {
rules: [
{
test: /\.[mc]?[jt]sx$/i,
use: [
{ loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } },
'solid-loader',
],
},
],
},
});
```

## Options
Rspack runs the loaders from right to left. Solid compiles JSX first, then SWC removes TypeScript types and lowers JavaScript syntax. If you already have an SWC rule for TSX, add `solid-loader` to that rule. For TypeScript projects, set `jsx` to `"preserve"` and `jsxImportSource` to `"@solidjs/web"` in `tsconfig.json`.

## Solid Refresh

For browser development builds with HMR enabled, add `development` to the resolve conditions alongside `solid` and register this extra rule. The pre-loader instruments components before JSX compilation.

```js
{
test: /\.[mc]?[jt]sx$/i,
enforce: 'pre',
exclude: /node_modules/,
dependency: { not: 'url' },
resourceQuery: { not: /[?&]raw(?:&|=|$)/ },
with: { type: { not: 'text' } },
loader: 'solid-loader/refresh',
}
```

### foo
Refresh generates per-component metadata by default. Set `options: { granular: false }` on the refresh rule to disable it. The [playground](./playground/rspack.config.ts) shows the full configuration for development and production.

Some description.
## Server-side rendering

- Type: `string`
- Default: `undefined`
- Example:
Set `options.solid` on the main loader for each build:

| Build | Compiler options |
| ---------------------------- | --------------------------------------- |
| Server | `{ generate: 'ssr', hydratable: true }` |
| Client hydrating server HTML | `{ generate: 'dom', hydratable: true }` |

To let the Solid runtime import hydration modules by URL, add the following rule:

```js
pluginExample({
foo: 'bar',
});
{
descriptionData: { name: /^@solidjs\/web$/ },
parser: { importDynamic: false },
}
```

## Loader options

| Option | Behavior |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `compiler` | Defaults to `'native'`. Set to `'babel'` to use `@solidjs/babel-plugin`. |
| `solid` | Options for the Solid JSX compiler. By default it generates DOM output, with `dev` enabled in Rspack development mode. |
| `decoratorVersion` | Set to `'legacy'` to parse legacy decorators in the Babel backend. The default parser accepts standard decorators. |
| `scriptRegex` | Optional filename filter for integrations that share a rule with other script types. By default, the loader compiles every resource matched by the rule. |
| `transformFilename` | Filename to use for filtering and parser selection, e.g. `'data-uri.jsx'` for an inline module. Source maps still refer to the original resource. |

The main entry exports the `SolidLoaderOptions`, `SolidCompiler`, and `SolidPresetOptions` types. The refresh entry exports `SolidRefreshLoaderOptions`.

## License

[MIT](./LICENSE).
30 changes: 21 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
{
"name": "rsbuild-plugin-example",
"name": "solid-loader",
"version": "0.0.0",
"repository": "https://github.com/rstackjs/rsbuild-plugin-template",
"description": "Solid JSX and refresh loaders for Rspack",
"repository": "https://github.com/rstackjs/solid-loader",
"license": "MIT",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./refresh": {
"types": "./dist/refresh.d.ts",
"default": "./dist/refresh.js"
}
},
"types": "./dist/index.d.ts",
Expand All @@ -24,22 +29,29 @@
"prepare": "rs hooks",
"test": "rs test"
},
"dependencies": {
"@babel/core": "^7.29.13",
"@jridgewell/remapping": "^2.3.5",
"@solidjs/babel-plugin": "2.0.0",
"@solidjs/compiler": "2.0.0"
},
"devDependencies": {
"@rsbuild/core": "^2.3.2",
"@rspack/core": "catalog:",
"@rspack/dev-server": "catalog:",
"@rstackjs/test-utils": "^1.0.0",
"@rstest/playwright": "^0.12.6",
"@solidjs/web": "catalog:",
"@types/babel__core": "^7.20.5",
"@types/node": "^24.19.2",
"playwright": "^1.65.0",
"rstack": "^0.9.1",
"solid-js": "catalog:",
"typescript": "^7.0.2"
},
"peerDependencies": {
"@rsbuild/core": "^2.0.0"
},
"peerDependenciesMeta": {
"@rsbuild/core": {
"optional": true
}
"@rspack/core": "^2.0.0",
"@solidjs/web": "^2.0.0",
"solid-js": "^2.0.0"
},
"packageManager": "pnpm@12.18.0",
"publishConfig": {
Expand Down
14 changes: 12 additions & 2 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@
"private": true,
"type": "module",
"scripts": {
"build": "rs build",
"dev": "rs dev"
"build": "rspack build",
"dev": "rspack serve --mode development"
},
"dependencies": {
"@solidjs/web": "catalog:",
"solid-js": "catalog:"
},
"devDependencies": {
"@rspack/cli": "~2.3.3",
"@rspack/core": "catalog:",
"@rspack/dev-server": "catalog:",
"solid-loader": "workspace:*"
}
}
44 changes: 44 additions & 0 deletions playground/rspack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { type Configuration, HtmlRspackPlugin } from '@rspack/core';

export default (
_env: unknown,
{ mode }: Pick<Configuration, 'mode'>,
): Configuration => {
const dev = mode === 'development';
const test = /\.[mc]?[jt]sx$/i;

return {
context: import.meta.dirname,
mode,
entry: './src/index.tsx',
resolve: {
extensions: ['.tsx', '.jsx', '...'],
conditionNames: ['solid', ...(dev ? ['development'] : []), '...'],
},
module: {
rules: [
{
test,
use: [
{ loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } },
'solid-loader',
],
},
dev && {
test,
enforce: 'pre',
exclude: /node_modules/,
dependency: { not: 'url' },
resourceQuery: { not: /[?&]raw(?:&|=|$)/ },
with: { type: { not: 'text' } },
loader: 'solid-loader/refresh',
},
{
descriptionData: { name: /^@solidjs\/web$/ },
parser: { importDynamic: false },
},
],
},
plugins: [new HtmlRspackPlugin()],
};
};
7 changes: 0 additions & 7 deletions playground/rstack.config.ts

This file was deleted.

3 changes: 3 additions & 0 deletions playground/src/Counter.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function Counter(props: { count: number; increment: () => void }) {
return <button onClick={props.increment}>Count: {props.count}</button>;
}
26 changes: 0 additions & 26 deletions playground/src/index.css

This file was deleted.

11 changes: 0 additions & 11 deletions playground/src/index.ts

This file was deleted.

10 changes: 10 additions & 0 deletions playground/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { render } from '@solidjs/web';
import { createSignal } from 'solid-js';
import { Counter } from './Counter';

function App() {
const [count, setCount] = createSignal<number>(0);
return <Counter count={count()} increment={() => setCount(count() + 1)} />;
}

render(() => <App />, document.body);
6 changes: 4 additions & 2 deletions playground/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"target": "ES2020",
"noEmit": true,
"skipLibCheck": true,
"types": ["rstack/types"],
"types": ["node"],
"jsx": "preserve",
"jsxImportSource": "@solidjs/web",
"useDefineForClassFields": true,

/* modules */
Expand All @@ -18,5 +20,5 @@
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": ["src"]
"include": ["src", "rspack.config.ts"]
}
Loading
Loading