// 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 { existsSync } from 'node:fs' import { mkdtemp } from 'node:fs/promises' import { tmpdir } from 'node:os' import path, { dirname } from 'node:path' import { fileURLToPath } from 'node:url' import type { Rspack } from '@rsbuild/core' import { describe, expect, rstest, test } from '@rstest/core' import { createStubRspeedy as createRspeedy } from './createRspeedy.js' import { pluginStubRspeedyAPI } from './stub-rspeedy-api.plugin.js' rstest .stubEnv('USE_RSPACK', 'true') .stubEnv('NODE_ENV', 'development') describe('ReactLynx rsbuild', () => { test('basic usage', async () => { // TODO(react-refresh): support refresh rstest.stubEnv('NODE_ENV', 'production') const { pluginReactLynx } = await import('../src/index.js') const tmp = await mkdtemp(path.join(tmpdir(), 'rspeedy-react-test-basic-')) const rsbuild = await createRspeedy({ rspeedyConfig: { source: { entry: { main: fileURLToPath( new URL('./fixtures/basic.tsx', import.meta.url), ), }, }, output: { // Isolate the dist root so this build cannot race other tests in // this package writing to the default `test/dist/` directory. distPath: { root: tmp }, }, tools: { rspack: { context: dirname(fileURLToPath(import.meta.url)), resolve: { extensionAlias: { '.js': ['.ts', '.js'], '.jsx': ['.tsx', '.jsx'], }, }, }, }, plugins: [ pluginReactLynx(), pluginStubRspeedyAPI(), ], }, }) await rsbuild.build() expect(1).toBe(1) }) test('basic usage with Element Template enabled', async () => { rstest.stubEnv('NODE_ENV', 'production') const { pluginReactLynx } = await import('../src/index.js') const tmp = await mkdtemp( path.join(tmpdir(), 'rspeedy-react-test-element-template-'), ) const rsbuild = await createRspeedy({ rspeedyConfig: { source: { entry: { main: fileURLToPath( new URL( './fixtures/element-template-basic.tsx', import.meta.url, ), ), }, }, output: { // Isolate the dist root so this build cannot race other tests in // this package writing to the default `test/dist/` directory. distPath: { root: tmp }, }, tools: { rspack: { context: dirname(fileURLToPath(import.meta.url)), resolve: { extensionAlias: { '.js': ['.ts', '.js'], '.jsx': ['.tsx', '.jsx'], }, }, }, }, plugins: [ pluginReactLynx({ experimental_useElementTemplate: true, }), pluginStubRspeedyAPI(), ], }, }) await rsbuild.build() expect(1).toBe(1) }) test('Element Template dynamic import build resolves the ET lazy import entry', async () => { rstest.stubEnv('NODE_ENV', 'production') const { pluginReactLynx } = await import('../src/index.js') const tmp = await mkdtemp( path.join(tmpdir(), 'rspeedy-react-test-et-dynamic-import-'), ) const resolvedResources = new Set() const collectResolvedResources: Rspack.RspackPluginInstance = { name: 'collectResolvedResources', apply(compiler) { compiler.hooks.compilation.tap( 'collectResolvedResources', (compilation) => { compilation.hooks.finishModules.tap( 'collectResolvedResources', (modules) => { for (const module of modules) { const resource = (module as { resource?: string nameForCondition?: () => string | null | undefined }).resource ?? (module as { nameForCondition?: () => string | null | undefined }).nameForCondition?.() if (resource) { resolvedResources.add(resource.replaceAll(path.sep, '/')) } } }, ) }, ) }, } const rsbuild = await createRspeedy({ rspeedyConfig: { source: { entry: { main: fileURLToPath( new URL( './fixtures/element-template-dynamic-import.tsx', import.meta.url, ), ), }, }, output: { distPath: { root: tmp }, }, tools: { rspack: { context: dirname(fileURLToPath(import.meta.url)), resolve: { extensionAlias: { '.js': ['.ts', '.js'], '.jsx': ['.tsx', '.jsx'], }, }, plugins: [collectResolvedResources], }, }, plugins: [ pluginReactLynx({ experimental_useElementTemplate: true, }), pluginStubRspeedyAPI(), ], }, }) await rsbuild.build() const resolved = [...resolvedResources] expect( resolved.some(resource => resource.endsWith( '/packages/react/runtime/lazy/element-template-import.js', ) ), ).toBe(true) expect( resolved.some(resource => resource.endsWith('/packages/react/runtime/lazy/import.js') ), ).toBe(false) }) test('special var name', async () => { const { pluginReactLynx } = await import('../src/index.js') rstest.stubEnv('NODE_ENV', 'production') const tmp = await mkdtemp(path.join(tmpdir(), 'rspeedy-react-test')) const rspeedy = await createRspeedy({ rspeedyConfig: { source: { entry: { main: fileURLToPath( new URL( './fixtures/special-var-name/index.jsx', import.meta.url, ), ), }, }, output: { distPath: { root: tmp, }, filenameHash: false, }, plugins: [ pluginReactLynx(), ], }, }) const result = await rspeedy.build() await result.close() const backgroundJSPath = path.resolve(tmp, '.lynx/main/background.js') expect(existsSync(backgroundJSPath)).toBe(true) const define = rstest.fn() Object.assign(globalThis, { tt: { define, require: rstest.fn(), }, }) await expect(import(backgroundJSPath)).resolves.not.toThrow() expect(define).toHaveBeenCalledWith('background.js', expect.any(Function)) }) })