// Copyright 2024 The Lynx Authors. All rights reserved. // Licensed under the Apache License Version 2.0 that can be found in the // LICENSE file in the root directory of this source tree. import { createRequire } from 'node:module'; import path from 'node:path'; import type { LoaderContext } from '@rspack/core'; import type { ElementTemplateConfig } from '@lynx-js/react/transform'; import { ELEMENT_TEMPLATE_INTERNAL_PKG, ELEMENT_TEMPLATE_RUNTIME_PKG, JSX_IMPORT_SOURCE, RUNTIME_PKG, } from './options.js'; import type { ReactLoaderOptions } from './options.js'; function normalizeSlashes(file: string) { return file.replaceAll(path.win32.sep, '/'); } function testingLoader( this: LoaderContext, content: string, ): void { const require = createRequire(import.meta.url); const { compat = false, defineDCE = { define: {} }, engineVersion = '', experimental_transformBuiltinAttributeNames, experimental_useElementTemplate = false, shake = false, transformPath = '@lynx-js/react/transform', } = this.getOptions(); const { transformReactLynxSync } = require( transformPath, ) as typeof import('@lynx-js/react/transform'); const filename = normalizeSlashes( path.relative( this.rootContext, this.resourcePath, ), ); const normalizedCompat = typeof compat === 'object' ? { target: 'MIXED' as const, addComponentElement: compat.addComponentElement ?? false, additionalComponentAttributes: compat.additionalComponentAttributes ?? [], componentsPkg: compat.componentsPkg ?? ['@lynx-js/react-components'], disableDeprecatedWarning: compat.disableDeprecatedWarning ?? false, newRuntimePkg: compat.newRuntimePkg ?? '@lynx-js/react', oldRuntimePkg: compat.oldRuntimePkg ?? ['@lynx-js/react-runtime'], simplifyCtorLikeReactLynx2: compat.simplifyCtorLikeReactLynx2 ?? false, transformLegacyEventAttributeNames: compat.transformLegacyEventAttributeNames ?? true, ...(typeof compat.removeComponentAttrRegex === 'string' && { removeComponentAttrRegex: compat.removeComponentAttrRegex, }), darkMode: compat.darkMode ?? false, } : (compat ?? false); const result = transformReactLynxSync( content, { mode: 'test', pluginName: '', filename: this.resourcePath, sourcemap: true, snapshot: experimental_useElementTemplate ? false : { preserveJsx: false, runtimePkg: RUNTIME_PKG, jsxImportSource: JSX_IMPORT_SOURCE.BACKGROUND, filename, target: 'MIXED', }, elementTemplate: experimental_useElementTemplate ? { preserveJsx: false, runtimePkg: ELEMENT_TEMPLATE_RUNTIME_PKG, // MIXED mode runs in a no-layer test environment; keep standard Preact // JSX so background diffing preserves keys and VNode metadata. jsxImportSource: JSX_IMPORT_SOURCE.BACKGROUND, filename, target: 'MIXED', } satisfies ElementTemplateConfig : false, // snapshot: true, directiveDCE: false, defineDCE, shake, compat: normalizedCompat, engineVersion, worklet: { filename, runtimePkg: experimental_useElementTemplate ? ELEMENT_TEMPLATE_INTERNAL_PKG : RUNTIME_PKG, target: 'MIXED', }, refresh: false, cssScope: false, ...(experimental_transformBuiltinAttributeNames !== undefined && { experimental_transformBuiltinAttributeNames, }), }, ); if (result.errors.length > 0) { for (const error of result.errors) { if (this.experiments?.emitDiagnostic) { // Rspack with `emitDiagnostic` API try { this.experiments.emitDiagnostic({ message: error.text!, sourceCode: content, location: { line: error.location?.line ?? 1, column: error.location?.column ?? 0, length: error.location?.length ?? 0, text: error.text ?? '', }, severity: 'error', }); } catch { // Rspack may throw on invalid line & column when containing UTF-8. // We catch it up here. this.emitError(new Error(error.text)); } } else { // Webpack or legacy Rspack this.emitError(new Error(error.text)); } } this.callback(new Error('react-transform failed')); return; } for (const warning of result.warnings) { if (this.experiments?.emitDiagnostic) { // Rspack with `emitDiagnostic` API try { this.experiments.emitDiagnostic({ message: warning.text!, sourceCode: content, location: { line: warning.location?.line ?? 1, column: warning.location?.column ?? 0, length: warning.location?.length ?? 0, text: warning.text ?? '', }, severity: 'warning', }); } catch { // Rspack may throw on invalid line & column when containing UTF-8. // We catch it up here. this.emitWarning(new Error(warning.text)); } } else { // Webpack or legacy Rspack this.emitWarning(new Error(warning.text)); } } this.callback(null, result.code, result.map); } export default testingLoader;