From dc34b2f38efb84aab85096524d2ab728045cebc6 Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 19 Nov 2026 10:44:55 +0800 Subject: [PATCH 1/2] fix: preserve additionalData when skipping a file --- src/helpers.ts | 6 +--- src/index.ts | 6 ++-- src/refresh.ts | 9 ++++-- test/basic/additionalData.test.ts | 54 +++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 9 deletions(-) create mode 100644 test/basic/additionalData.test.ts diff --git a/src/helpers.ts b/src/helpers.ts index 0e257c6..2214d8b 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -37,7 +37,7 @@ export const mergeSourceMaps = ( ) as RawSourceMap; }; -const NODE_MODULES_REGEX = /[\\/]node_modules[\\/]/; +export const NODE_MODULES_REGEX = /[\\/]node_modules[\\/]/; export async function applyRefresh( source: string, @@ -48,10 +48,6 @@ export async function applyRefresh( granular, }: { filename?: string; granular?: boolean } = {}, ) { - if (NODE_MODULES_REGEX.test(context.resourcePath)) { - return { code: source, map: sourceMap }; - } - const { transformRefreshAsync } = await import('@solidjs/compiler'); const result = await transformRefreshAsync(source, { filename: getTransformFilename(filename), diff --git a/src/index.ts b/src/index.ts index 7690a38..964bb61 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { applyRefresh, getTransformFilename, mergeSourceMaps, + NODE_MODULES_REGEX, normalizeSourceMap, } from './helpers.js'; import type { SolidCompiler, SolidPresetOptions } from './types.js'; @@ -27,6 +28,7 @@ export type SolidLoaderOptions = { const solidLoader: LoaderDefinition = async function ( source, sourceMap, + additionalData, ): Promise { const callback = this.async(); const options = this.getOptions(); @@ -37,7 +39,7 @@ const solidLoader: LoaderDefinition = async function ( options.scriptRegex && resourceFilename.search(options.scriptRegex) === -1 ) { - callback(null, source, sourceMap); + callback(null, source, sourceMap, additionalData); return; } @@ -52,7 +54,7 @@ const solidLoader: LoaderDefinition = async function ( solid.generate !== 'ssr'); try { - if (refresh) { + if (refresh && !NODE_MODULES_REGEX.test(sourceFilename)) { const result = await applyRefresh(source, sourceMap, this, { filename: resourceFilename, }); diff --git a/src/refresh.ts b/src/refresh.ts index 524373e..1fe0f40 100644 --- a/src/refresh.ts +++ b/src/refresh.ts @@ -1,14 +1,19 @@ import type { LoaderDefinition } from '@rspack/core'; -import { applyRefresh } from './helpers.js'; +import { applyRefresh, NODE_MODULES_REGEX } from './helpers.js'; export type SolidRefreshLoaderOptions = { granular?: boolean; }; const solidRefreshLoader: LoaderDefinition = - async function (source, sourceMap): Promise { + async function (source, sourceMap, additionalData): Promise { const callback = this.async(); + if (NODE_MODULES_REGEX.test(this.resourcePath)) { + callback(null, source, sourceMap, additionalData); + return; + } + try { const { granular } = this.getOptions(); const result = await applyRefresh(source, sourceMap, this, { granular }); diff --git a/test/basic/additionalData.test.ts b/test/basic/additionalData.test.ts new file mode 100644 index 0000000..4331fe7 --- /dev/null +++ b/test/basic/additionalData.test.ts @@ -0,0 +1,54 @@ +import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; +import path from 'node:path'; +import { promisify } from 'node:util'; +import { rspack } from '@rspack/core'; +import { expect, test } from '@rstest/playwright'; + +test('preserves loader data when both Solid loaders skip a file', async () => { + const playground = path.resolve(import.meta.dirname, '../../playground'); + const cwd = await mkdtemp(path.join(playground, 'test-temp-')); + try { + await mkdir(path.join(cwd, 'node_modules')); + await writeFile( + path.join(cwd, 'node_modules/entry.js'), + 'export default 0;', + ); + await writeFile( + path.join(cwd, 'send.cjs'), + `module.exports = function (source, map) { + this.callback(null, source, map, { value: 'from upstream' }); + };`, + ); + await writeFile( + path.join(cwd, 'receive.cjs'), + `module.exports = function (source, map, data) { + if (data?.value !== 'from upstream') throw new Error('Missing loader data'); + return source; + };`, + ); + + const stats = await promisify(rspack)({ + context: cwd, + entry: './node_modules/entry.js', + mode: 'development', + target: 'node', + output: { path: cwd }, + module: { + rules: [ + { + test: /entry\.js$/, + use: [ + path.join(cwd, 'receive.cjs'), + { loader: 'solid-loader', options: { scriptRegex: /\.jsx$/ } }, + 'solid-loader/refresh', + path.join(cwd, 'send.cjs'), + ], + }, + ], + }, + }); + expect(stats?.toJson({ all: false, errors: true }).errors).toEqual([]); + } finally { + await rm(cwd, { recursive: true, force: true }); + } +}); From 90b215dce054c92188586612f40cfe2d5033516c Mon Sep 17 00:00:00 2001 From: neverland Date: Thu, 19 Nov 2026 11:12:39 +0800 Subject: [PATCH 2/2] test: remove the additionalData regression case --- test/basic/additionalData.test.ts | 54 ------------------------------- 1 file changed, 54 deletions(-) delete mode 100644 test/basic/additionalData.test.ts diff --git a/test/basic/additionalData.test.ts b/test/basic/additionalData.test.ts deleted file mode 100644 index 4331fe7..0000000 --- a/test/basic/additionalData.test.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'; -import path from 'node:path'; -import { promisify } from 'node:util'; -import { rspack } from '@rspack/core'; -import { expect, test } from '@rstest/playwright'; - -test('preserves loader data when both Solid loaders skip a file', async () => { - const playground = path.resolve(import.meta.dirname, '../../playground'); - const cwd = await mkdtemp(path.join(playground, 'test-temp-')); - try { - await mkdir(path.join(cwd, 'node_modules')); - await writeFile( - path.join(cwd, 'node_modules/entry.js'), - 'export default 0;', - ); - await writeFile( - path.join(cwd, 'send.cjs'), - `module.exports = function (source, map) { - this.callback(null, source, map, { value: 'from upstream' }); - };`, - ); - await writeFile( - path.join(cwd, 'receive.cjs'), - `module.exports = function (source, map, data) { - if (data?.value !== 'from upstream') throw new Error('Missing loader data'); - return source; - };`, - ); - - const stats = await promisify(rspack)({ - context: cwd, - entry: './node_modules/entry.js', - mode: 'development', - target: 'node', - output: { path: cwd }, - module: { - rules: [ - { - test: /entry\.js$/, - use: [ - path.join(cwd, 'receive.cjs'), - { loader: 'solid-loader', options: { scriptRegex: /\.jsx$/ } }, - 'solid-loader/refresh', - path.join(cwd, 'send.cjs'), - ], - }, - ], - }, - }); - expect(stats?.toJson({ all: false, errors: true }).errors).toEqual([]); - } finally { - await rm(cwd, { recursive: true, force: true }); - } -});