UNPKG

@lynx-js/react-webpack-plugin

Version:

A webpack plugin for ReactLynx

130 lines 5.38 kB
// 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 { ELEMENT_TEMPLATE_RUNTIME_PKG, JSX_IMPORT_SOURCE, RUNTIME_PKG, } from './options.js'; function normalizeSlashes(file) { return file.replaceAll(path.win32.sep, '/'); } function testingLoader(content) { 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); const filename = normalizeSlashes(path.relative(this.rootContext, this.resourcePath)); const normalizedCompat = typeof compat === 'object' ? { target: 'MIXED', 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, ...(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, jsxImportSource: JSX_IMPORT_SOURCE.ELEMENT_TEMPLATE, filename, target: 'MIXED', } : false, // snapshot: true, directiveDCE: false, defineDCE, shake, compat: normalizedCompat, engineVersion, worklet: { filename, runtimePkg: 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; //# sourceMappingURL=testing.js.map