Skip to content

Commit c5dd02c

Browse files
authored
fix: default scriptRegex to JSX and TSX extensions (#16)
1 parent 3e231a1 commit c5dd02c

5 files changed

Lines changed: 40 additions & 14 deletions

File tree

‎README.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ yarn add -D solid-loader
2424
bun add --dev solid-loader
2525
```
2626

27-
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.
27+
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.
2828

2929
```ts
3030
// rspack.config.ts
@@ -38,7 +38,7 @@ export default defineConfig({
3838
module: {
3939
rules: [
4040
{
41-
test: /\.[mc]?[jt]sx$/i,
41+
test: /\.[mc]?[jt]sx?$/i,
4242
use: [
4343
{
4444
loader: 'builtin:swc-loader',
@@ -200,15 +200,16 @@ export default {
200200
### scriptRegex
201201

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

204-
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.
205+
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.
205206

206207
```ts
207208
export default {
208209
module: {
209210
rules: [
210211
{
211-
test: /\.tsx?$/i,
212+
test: /\.[mc]?[jt]sx?$/i,
212213
use: [
213214
{
214215
loader: 'builtin:swc-loader',
@@ -219,7 +220,7 @@ export default {
219220
{
220221
loader: 'solid-loader',
221222
options: {
222-
scriptRegex: /\.tsx$/i,
223+
scriptRegex: /\.[cm]?(?:jsx?|tsx)$/i,
223224
},
224225
},
225226
],

‎playground/rspack.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default (
1717
module: {
1818
rules: [
1919
{
20-
test: /\.[mc]?[jt]sx$/i,
20+
test: /\.[mc]?[jt]sx?$/i,
2121
use: [
2222
{ loader: 'builtin:swc-loader', options: { detectSyntax: 'auto' } },
2323
'solid-loader',

‎src/helpers.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { LoaderContext, RawSourceMap } from '@rspack/core';
22
import remapping from '@jridgewell/remapping';
33

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

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

‎src/index.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type { SolidCompiler, SolidPresetOptions } from './types.js';
1414

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

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

37-
if (
38-
options.scriptRegex &&
39-
resourceFilename.search(options.scriptRegex) === -1
40-
) {
38+
const scriptRegex = options.scriptRegex ?? DEFAULT_SCRIPT_REGEX;
39+
if (resourceFilename.search(scriptRegex) === -1) {
4140
callback(null, source, sourceMap, additionalData);
4241
return;
4342
}

‎test/basic/index.test.ts‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { cp, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
22
import { createRequire } from 'node:module';
33
import path from 'node:path';
4+
import { promisify } from 'node:util';
45
import { type RuleSetRule, rspack } from '@rspack/core';
56
import { RspackDevServer } from '@rspack/dev-server';
67
import { expect, test } from '@rstest/playwright';
@@ -9,6 +10,31 @@ import playgroundConfig from '../../playground/rspack.config.ts';
910

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

13+
test('builds a TSX component that imports a plain TypeScript module', async () => {
14+
const cwd = await mkdtemp(path.join(playground, 'test-temp-'));
15+
try {
16+
await writeFile(
17+
path.join(cwd, 'count.ts'),
18+
'export const count: number = 42;',
19+
);
20+
await writeFile(
21+
path.join(cwd, 'app.tsx'),
22+
`import { render } from '@solidjs/web';
23+
import { count } from './count.ts';
24+
render(() => <h1>{count}</h1>, document.body);`,
25+
);
26+
const stats = await promisify(rspack)({
27+
...playgroundConfig({}, { mode: 'production' }),
28+
context: cwd,
29+
entry: './app.tsx',
30+
output: { path: path.join(cwd, 'dist') },
31+
});
32+
expect(stats.toJson({ all: false, errors: true }).errors).toEqual([]);
33+
} finally {
34+
await rm(cwd, { recursive: true, force: true });
35+
}
36+
});
37+
1238
for (const compiler of ['native', 'babel']) {
1339
test(`renders typed SSR output with ${compiler}`, async () => {
1440
const cwd = await mkdtemp(path.join(playground, 'test-temp-'));
@@ -102,7 +128,7 @@ for (const mode of [
102128
];
103129
if (standalone) {
104130
rules.unshift({
105-
test: jsxRule.test,
131+
test: /\.[mc]?[jt]sx$/i,
106132
enforce: 'pre',
107133
loader: 'solid-loader/refresh',
108134
});

0 commit comments

Comments
 (0)