diff --git a/README.md b/README.md index ff32f85..738dadf 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 JavaScript and TypeScript files pass directly to SWC. ```ts // rspack.config.ts @@ -38,7 +38,7 @@ export default defineConfig({ module: { rules: [ { - test: /\.[mc]?[jt]sx$/i, + test: /\.[mc]?[jt]sx?$/i, use: [ { loader: 'builtin:swc-loader', @@ -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', @@ -219,7 +220,7 @@ export default { { loader: 'solid-loader', options: { - scriptRegex: /\.tsx$/i, + scriptRegex: /\.[cm]?(?:jsx?|tsx)$/i, }, }, ], diff --git a/playground/rspack.config.ts b/playground/rspack.config.ts index af4450a..68e5370 100644 --- a/playground/rspack.config.ts +++ b/playground/rspack.config.ts @@ -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', 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 dd5de58..4e60088 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 DEFAULT_SCRIPT_REGEX = /\.[cm]?[jt]sx$/i; export type SolidLoaderOptions = { compiler?: SolidCompiler; @@ -34,10 +35,8 @@ const solidLoader: LoaderDefinition = 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; } diff --git a/test/basic/index.test.ts b/test/basic/index.test.ts index db73a77..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,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(() =>

{count}

, 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-')); @@ -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', });