From c58260317be1a0099b00899e064527f951f95e2f Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 19 Nov 2026 18:46:13 +0800 Subject: [PATCH 1/4] fix: skip plain TypeScript files by default --- README.md | 12 +++++++----- playground/rspack.config.ts | 3 ++- src/index.ts | 9 +++++---- test/basic/index.test.ts | 34 +++++++++++++++++++++++++++++++++- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index ff32f85..5fa3c5b 100644 --- a/README.md +++ b/README.md @@ -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 TypeScript files pass directly to SWC. ```ts // rspack.config.ts @@ -38,7 +38,8 @@ export default defineConfig({ module: { rules: [ { - test: /\.[mc]?[jt]sx$/i, + test: /\.[mc]?[jt]sx?$/i, + exclude: /node_modules/, use: [ { loader: 'builtin:swc-loader', @@ -201,14 +202,15 @@ export default { - **Type:** `RegExp` -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. +By default, the loader skips `.ts`, `.mts`, and `.cts` files. Set `scriptRegex` to override this default and compile only filenames that match your pattern. For example, a shared script rule can restrict Solid compilation to JSX and TSX files. ```ts export default { module: { rules: [ { - test: /\.tsx?$/i, + test: /\.[mc]?[jt]sx?$/i, + exclude: /node_modules/, use: [ { loader: 'builtin:swc-loader', @@ -219,7 +221,7 @@ export default { { loader: 'solid-loader', options: { - scriptRegex: /\.tsx$/i, + scriptRegex: /\.[mc]?[jt]sx$/i, }, }, ], diff --git a/playground/rspack.config.ts b/playground/rspack.config.ts index af4450a..373425e 100644 --- a/playground/rspack.config.ts +++ b/playground/rspack.config.ts @@ -17,7 +17,8 @@ export default ( module: { rules: [ { - test: /\.[mc]?[jt]sx$/i, + test: /\.[mc]?[jt]sx?$/i, + exclude: /node_modules/, use: [ { loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } }, 'solid-loader', diff --git a/src/index.ts b/src/index.ts index dd5de58..d236254 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,6 +14,7 @@ export type { SolidCompiler, SolidPresetOptions } from './types.js'; const require = createRequire(import.meta.url); const TSX_REGEX = /\.tsx$/i; +const PLAIN_TS_REGEX = /\.[mc]?ts$/i; export type SolidLoaderOptions = { compiler?: SolidCompiler; @@ -34,10 +35,10 @@ const solidLoader: LoaderDefinition = async function ( const sourceFilename = this.resourcePath; const resourceFilename = options.transformFilename ?? sourceFilename; - if ( - options.scriptRegex && - resourceFilename.search(options.scriptRegex) === -1 - ) { + const skip = options.scriptRegex + ? resourceFilename.search(options.scriptRegex) === -1 + : PLAIN_TS_REGEX.test(resourceFilename); + if (skip) { callback(null, source, sourceMap, additionalData); return; } diff --git a/test/basic/index.test.ts b/test/basic/index.test.ts index db73a77..889eb81 100644 --- a/test/basic/index.test.ts +++ b/test/basic/index.test.ts @@ -9,6 +9,38 @@ import playgroundConfig from '../../playground/rspack.config.ts'; const playground = path.resolve(import.meta.dirname, '../../playground'); +test('renders a TSX component that imports a plain TypeScript module', async ({ + page, +}) => { + const cwd = await mkdtemp(path.join(playground, 'test-temp-')); + const port = await getRandomPort(); + const config = playgroundConfig({}, { mode: 'development' }); + config.context = cwd; + config.entry = './app.tsx'; + const server = new RspackDevServer( + { host: '127.0.0.1', port, hot: true }, + rspack(config), + ); + 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(() =>

{count}

, document.body);`, + ); + await server.start(); + await page.goto(`http://127.0.0.1:${port}`); + await expect(page.getByRole('heading')).toHaveText('42'); + } finally { + await server.stop(); + 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-')); @@ -102,7 +134,7 @@ for (const mode of [ ]; if (standalone) { rules.unshift({ - test: jsxRule.test, + test: /\.[mc]?[jt]sx$/i, enforce: 'pre', loader: 'solid-loader/refresh', }); From d8ce6716b76451b1cb644c8e9f9c852a27cf9026 Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 19 Nov 2026 18:51:58 +0800 Subject: [PATCH 2/4] fix: default scriptRegex to JSX and TSX extensions --- README.md | 7 ++++--- src/helpers.ts | 6 +++--- src/index.ts | 8 +++----- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 5fa3c5b..00db329 100644 --- a/README.md +++ b/README.md @@ -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. Rspack runs loaders from right to left, so Solid compiles JSX first and SWC then strips TypeScript types. Plain TypeScript files pass directly to SWC. +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 @@ -201,8 +201,9 @@ export default { ### scriptRegex - **Type:** `RegExp` +- **Default:** `/\.[cm]?[jt]sx$/i` -By default, the loader skips `.ts`, `.mts`, and `.cts` files. Set `scriptRegex` to override this default and compile only filenames that match your pattern. For example, a shared script rule can restrict Solid compilation to JSX and 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 { @@ -221,7 +222,7 @@ export default { { loader: 'solid-loader', options: { - scriptRegex: /\.[mc]?[jt]sx$/i, + scriptRegex: /\.[cm]?(?:jsx?|tsx)$/i, }, }, ], diff --git a/src/helpers.ts b/src/helpers.ts index 2214d8b..2c9651b 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -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'}`; diff --git a/src/index.ts b/src/index.ts index d236254..4e60088 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,7 @@ export type { SolidCompiler, SolidPresetOptions } from './types.js'; const require = createRequire(import.meta.url); const TSX_REGEX = /\.tsx$/i; -const PLAIN_TS_REGEX = /\.[mc]?ts$/i; +const DEFAULT_SCRIPT_REGEX = /\.[cm]?[jt]sx$/i; export type SolidLoaderOptions = { compiler?: SolidCompiler; @@ -35,10 +35,8 @@ const solidLoader: LoaderDefinition = async function ( const sourceFilename = this.resourcePath; const resourceFilename = options.transformFilename ?? sourceFilename; - const skip = options.scriptRegex - ? resourceFilename.search(options.scriptRegex) === -1 - : PLAIN_TS_REGEX.test(resourceFilename); - if (skip) { + const scriptRegex = options.scriptRegex ?? DEFAULT_SCRIPT_REGEX; + if (resourceFilename.search(scriptRegex) === -1) { callback(null, source, sourceMap, additionalData); return; } From f257d0d0490385bda8d3804ca8dc418e444d75cb Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 19 Nov 2026 18:53:47 +0800 Subject: [PATCH 3/4] docs: remove node_modules exclusions from loader examples --- README.md | 2 -- playground/rspack.config.ts | 1 - 2 files changed, 3 deletions(-) diff --git a/README.md b/README.md index 00db329..738dadf 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,6 @@ export default defineConfig({ rules: [ { test: /\.[mc]?[jt]sx?$/i, - exclude: /node_modules/, use: [ { loader: 'builtin:swc-loader', @@ -211,7 +210,6 @@ export default { rules: [ { test: /\.[mc]?[jt]sx?$/i, - exclude: /node_modules/, use: [ { loader: 'builtin:swc-loader', diff --git a/playground/rspack.config.ts b/playground/rspack.config.ts index 373425e..68e5370 100644 --- a/playground/rspack.config.ts +++ b/playground/rspack.config.ts @@ -18,7 +18,6 @@ export default ( rules: [ { test: /\.[mc]?[jt]sx?$/i, - exclude: /node_modules/, use: [ { loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } }, 'solid-loader', From e63a6d2566938fd431e52c591dfa70bd8d74201f Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 19 Nov 2026 19:00:57 +0800 Subject: [PATCH 4/4] test: cover plain TypeScript with a production build --- test/basic/index.test.ts | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/test/basic/index.test.ts b/test/basic/index.test.ts index 889eb81..0a43795 100644 --- a/test/basic/index.test.ts +++ b/test/basic/index.test.ts @@ -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'; @@ -9,18 +10,8 @@ import playgroundConfig from '../../playground/rspack.config.ts'; const playground = path.resolve(import.meta.dirname, '../../playground'); -test('renders a TSX component that imports a plain TypeScript module', async ({ - page, -}) => { +test('builds a TSX component that imports a plain TypeScript module', async () => { const cwd = await mkdtemp(path.join(playground, 'test-temp-')); - const port = await getRandomPort(); - const config = playgroundConfig({}, { mode: 'development' }); - config.context = cwd; - config.entry = './app.tsx'; - const server = new RspackDevServer( - { host: '127.0.0.1', port, hot: true }, - rspack(config), - ); try { await writeFile( path.join(cwd, 'count.ts'), @@ -32,11 +23,14 @@ test('renders a TSX component that imports a plain TypeScript module', async ({ import { count } from './count.ts'; render(() =>

{count}

, document.body);`, ); - await server.start(); - await page.goto(`http://127.0.0.1:${port}`); - await expect(page.getByRole('heading')).toHaveText('42'); + 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 server.stop(); await rm(cwd, { recursive: true, force: true }); } });