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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ yarn add -D solid-loader
bun add --dev solid-loader
```

Add `solid-loader` after `builtin:swc-loader` in the `use` array for JSX and TSX files. Rspack runs loaders from right to left, so Solid compiles the JSX first and SWC then strips TypeScript types.
Add `solid-loader` after `builtin:swc-loader` in the `use` array. Rspack runs loaders from right to left, so Solid compiles JSX first and SWC then strips TypeScript types. Plain JavaScript and TypeScript files pass directly to SWC.

```ts
// rspack.config.ts
Expand All @@ -38,7 +38,7 @@ export default defineConfig({
module: {
rules: [
{
test: /\.[mc]?[jt]sx$/i,
test: /\.[mc]?[jt]sx?$/i,
use: [
{
loader: 'builtin:swc-loader',
Expand Down Expand Up @@ -200,15 +200,16 @@ export default {
### scriptRegex

- **Type:** `RegExp`
- **Default:** `/\.[cm]?[jt]sx$/i`

Filter the files passed to the loader by filename. Without a filter, the loader processes every file matched by the Rspack rule. For example, a rule that handles TypeScript can restrict Solid compilation to TSX files.
The main loader compiles files whose names match this pattern. By default it matches JSX and TSX files, including their ESM and CommonJS variants. The following example also enables JSX in JavaScript files.

```ts
export default {
module: {
rules: [
{
test: /\.tsx?$/i,
test: /\.[mc]?[jt]sx?$/i,
use: [
{
loader: 'builtin:swc-loader',
Expand All @@ -219,7 +220,7 @@ export default {
{
loader: 'solid-loader',
options: {
scriptRegex: /\.tsx$/i,
scriptRegex: /\.[cm]?(?:jsx?|tsx)$/i,
},
},
],
Expand Down
2 changes: 1 addition & 1 deletion playground/rspack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default (
module: {
rules: [
{
test: /\.[mc]?[jt]sx$/i,
test: /\.[mc]?[jt]sx?$/i,
use: [
{ loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } },
'solid-loader',
Expand Down
6 changes: 3 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { LoaderContext, RawSourceMap } from '@rspack/core';
import remapping from '@jridgewell/remapping';

const DEFAULT_SOLID_SCRIPT_REGEX = /\.(?:jsx|tsx)$/i;
const NATIVE_JSX_EXTENSION = /\.[jt]sx$/;

// Native transforms infer the parser from a standard JSX/TSX extension.
// Native transforms infer the parser from a lowercase .jsx or .tsx extension.
export const getTransformFilename = (filename: string): string =>
DEFAULT_SOLID_SCRIPT_REGEX.test(filename)
NATIVE_JSX_EXTENSION.test(filename)
? filename
: `${filename}${/\.[mc]?tsx$/i.test(filename) ? '.tsx' : '.jsx'}`;

Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export type { SolidCompiler, SolidPresetOptions } from './types.js';

const require = createRequire(import.meta.url);
const TSX_REGEX = /\.tsx$/i;
const DEFAULT_SCRIPT_REGEX = /\.[cm]?[jt]sx$/i;

export type SolidLoaderOptions = {
compiler?: SolidCompiler;
Expand All @@ -34,10 +35,8 @@ const solidLoader: LoaderDefinition<SolidLoaderOptions> = async function (
const sourceFilename = this.resourcePath;
const resourceFilename = options.transformFilename ?? sourceFilename;

if (
options.scriptRegex &&
resourceFilename.search(options.scriptRegex) === -1
) {
const scriptRegex = options.scriptRegex ?? DEFAULT_SCRIPT_REGEX;
if (resourceFilename.search(scriptRegex) === -1) {
callback(null, source, sourceMap, additionalData);
return;
}
Expand Down
28 changes: 27 additions & 1 deletion test/basic/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { cp, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { createRequire } from 'node:module';
import path from 'node:path';
import { promisify } from 'node:util';
import { type RuleSetRule, rspack } from '@rspack/core';
import { RspackDevServer } from '@rspack/dev-server';
import { expect, test } from '@rstest/playwright';
Expand All @@ -9,6 +10,31 @@ import playgroundConfig from '../../playground/rspack.config.ts';

const playground = path.resolve(import.meta.dirname, '../../playground');

test('builds a TSX component that imports a plain TypeScript module', async () => {
const cwd = await mkdtemp(path.join(playground, 'test-temp-'));
try {
await writeFile(
path.join(cwd, 'count.ts'),
'export const count: number = 42;',
);
await writeFile(
path.join(cwd, 'app.tsx'),
`import { render } from '@solidjs/web';
import { count } from './count.ts';
render(() => <h1>{count}</h1>, document.body);`,
);
const stats = await promisify(rspack)({
...playgroundConfig({}, { mode: 'production' }),
context: cwd,
entry: './app.tsx',
output: { path: path.join(cwd, 'dist') },
});
expect(stats.toJson({ all: false, errors: true }).errors).toEqual([]);
} finally {
await rm(cwd, { recursive: true, force: true });
}
});

for (const compiler of ['native', 'babel']) {
test(`renders typed SSR output with ${compiler}`, async () => {
const cwd = await mkdtemp(path.join(playground, 'test-temp-'));
Expand Down Expand Up @@ -102,7 +128,7 @@ for (const mode of [
];
if (standalone) {
rules.unshift({
test: jsxRule.test,
test: /\.[mc]?[jt]sx$/i,
enforce: 'pre',
loader: 'solid-loader/refresh',
});
Expand Down