Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/wet-humans-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@lynx-js/qrcode-rsbuild-plugin': patch
'@lynx-js/rspeedy': patch
---

feat(qrcode): support get entry from api exposed from rspeedy.env.entries
2 changes: 2 additions & 0 deletions packages/rspeedy/core/etc/rspeedy.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { logger } from '@rsbuild/core';
import type { PerformanceConfig } from '@rsbuild/core';
import type { ProxyConfig } from '@rsbuild/core';
import type { RsbuildConfig } from '@rsbuild/core';
import type { RsbuildEntry } from '@rsbuild/core';
import type { RsbuildInstance } from '@rsbuild/core';
import { RsbuildPlugin } from '@rsbuild/core';
import { RsbuildPluginAPI } from '@rsbuild/core';
Expand Down Expand Up @@ -195,6 +196,7 @@ export interface EntryDescription {
export interface ExposedAPI {
config: Config;
debug: (message: string | (() => string)) => void;
entries?: RsbuildEntry;
exit: (code?: number) => Promise<void> | void;
logger: typeof logger;
version: string;
Expand Down
6 changes: 6 additions & 0 deletions packages/rspeedy/core/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 type { logger } from '@rsbuild/core'
import type { RsbuildEntry } from '@rsbuild/core'

import type { Config } from './config/index.js'

Expand Down Expand Up @@ -52,4 +53,9 @@ export interface ExposedAPI {
* The version of Rspeedy.
*/
version: string

/**
* Used for plugin qrcode get entry points from self-defined environments rather than default lynx environment.
*/
entries?: RsbuildEntry
}
1 change: 1 addition & 0 deletions packages/rspeedy/core/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"tasks": {
"build": {
"dependsOn": [
"//#build",
"^build"
],
"env": [
Expand Down
32 changes: 23 additions & 9 deletions packages/rspeedy/plugin-qrcode/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
* A rsbuild plugin that print the template.js url using QRCode.
*/

import type { EnvironmentContext, RsbuildPlugin } from '@rsbuild/core'
import type {
EnvironmentContext,
RsbuildEntry,
RsbuildPlugin,
} from '@rsbuild/core'

import type { ExposedAPI } from '@lynx-js/rspeedy'

import { registerConsoleShortcuts } from './shortcuts.js'

Expand Down Expand Up @@ -102,7 +108,7 @@ export function pluginQRCode(
pre: ['lynx:rsbuild:api'],
setup(api) {
api.onAfterStartProdServer(async ({ environments, port }) => {
await main(environments['lynx'], port)
await main(getEntries(environments), port)
})

let printedQRCode = false
Expand All @@ -122,27 +128,35 @@ export function pluginQRCode(

printedQRCode = true

await main(environments['lynx'], api.context.devServer.port)
await main(getEntries(environments), api.context.devServer.port)
})

function getEntries(
environments: Record<string, EnvironmentContext> | undefined,
) {
// biome-ignore lint/correctness/useHookAtTopLevel: not react hooks
return api.useExposed<ExposedAPI>(Symbol.for('rspeedy.env.entries'))
?.entries ?? environments?.['lynx']?.entry
}

async function main(
environmentContext: EnvironmentContext | undefined,
entries: RsbuildEntry | undefined,
port: number,
) {
if (!environmentContext) {
// Not lynx environment, skip print QRCode
if (!entries) {
// No entry points, skip print QRCode
return
}

const entries = Object.keys(environmentContext.entry)
const entriesArray = Object.keys(entries)

if (entries.length === 0) {
if (entriesArray.length === 0) {
return
}

const unregister = await registerConsoleShortcuts(
{
entries,
entries: entriesArray,
api,
port,
schema,
Expand Down
62 changes: 61 additions & 1 deletion packages/rspeedy/plugin-qrcode/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { fileURLToPath } from 'node:url'

import { isCancel } from '@clack/prompts'
import { createRsbuild, logger } from '@rsbuild/core'
import type { RsbuildInstance, RsbuildPlugin } from '@rsbuild/core'
import type {
RsbuildEntry,
RsbuildInstance,
RsbuildPlugin,
} from '@rsbuild/core'
import { beforeEach, describe, expect, onTestFinished, test, vi } from 'vitest'

import type { Config, ExposedAPI } from '@lynx-js/rspeedy'
Expand All @@ -30,6 +34,13 @@ const pluginStubRspeedyAPI = (config: Config = {}): RsbuildPlugin => ({
},
})

const pluginStubEnvEntries = (entries: RsbuildEntry): RsbuildPlugin => ({
name: 'lynx:rsbuild:env-entries',
setup(api) {
api.expose(Symbol.for('rspeedy.env.entries'), { entries })
},
})

vi.mock('@clack/prompts')

describe('Plugins - Terminal', () => {
Expand Down Expand Up @@ -340,6 +351,55 @@ describe('Plugins - Terminal', () => {
expect(renderUnicodeCompact).toBeCalledTimes(1)
})

test('print qrcode with exposed custom environment entries', async () => {
vi.stubEnv('NODE_ENV', 'development')
const { selectKey, isCancel } = await import('@clack/prompts')
vi.mocked(selectKey).mockResolvedValue('foo')
vi.mocked(isCancel).mockReturnValueOnce(false)
const { renderUnicodeCompact } = await import('uqr')
vi.mocked(renderUnicodeCompact).mockReturnValueOnce('<data>')

const entry = join(
dirname(fileURLToPath(import.meta.url)),
'fixtures',
'hello-world',
)

const rsbuild = await createRsbuild(
{
rsbuildConfig: {
dev: {
assetPrefix: 'http://example.com/foo/',
},
environments: {
custom: {},
},
server: {
port: getRandomNumberInRange(3000, 60000),
},
source: {
entry: {
main: entry,
},
},
plugins: [
pluginStubRspeedyAPI(),
pluginStubEnvEntries({
main: entry,
}),
pluginQRCode(),
],
},
},
)

await using server = await usingDevServer(rsbuild)

await server.waitDevCompileDone()

expect(renderUnicodeCompact).toBeCalledTimes(1)
})

test('print qrcode when dev with host specified', async () => {
vi.stubEnv('NODE_ENV', 'development')
vi.mock('qrcode', () => ({
Expand Down
67 changes: 64 additions & 3 deletions packages/rspeedy/plugin-qrcode/test/preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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 { createRsbuild, logger } from '@rsbuild/core'
import type { RsbuildEntry } from '@rsbuild/core'
import { beforeEach, describe, expect, test, vi } from 'vitest'

import type { Config, ExposedAPI, RsbuildPlugin } from '@lynx-js/rspeedy'
Expand All @@ -27,6 +28,13 @@ const pluginStubRspeedyAPI = (config: Config = {}): RsbuildPlugin => ({
},
})

const pluginStubEnvEntries = (entries: RsbuildEntry): RsbuildPlugin => ({
name: 'lynx:rsbuild:env-entries',
setup(api) {
api.expose(Symbol.for('rspeedy.env.entries'), { entries })
},
})

describe('Preview', () => {
beforeEach(() => {
vi.restoreAllMocks()
Expand Down Expand Up @@ -231,7 +239,7 @@ describe('Preview', () => {
expect(exit).not.toBeCalled()
})

test('preview without entry', async () => {
test('preview with exposed custom environment entries', async () => {
vi.stubEnv('NODE_ENV', 'development')
const { renderUnicodeCompact } = await import('uqr')

Expand All @@ -243,12 +251,65 @@ describe('Preview', () => {

const rsbuild = await createRsbuild({
rsbuildConfig: {
source: {
entry: {
main: './fixtures/hello-world/index.js',
},
},
plugins: [
pluginStubRspeedyAPI(),
pluginStubEnvEntries({
main: './fixtures/hello-world/index.js',
}),
pluginQRCode(),
],
source: {
entry: {},
environments: {
custom: {},
},
dev: {
assetPrefix: 'http://example.com/',
},
server: {
port: getRandomNumberInRange(3000, 60000),
},
},
})

const { server } = await rsbuild.preview({ checkDistDir: false })

expect(renderUnicodeCompact).toBeCalled()
expect(renderUnicodeCompact).toBeCalledWith(
'http://example.com/main.lynx.bundle',
)

await server.close()
await vi.waitFor(() => {
expect(exit).toBeCalledTimes(1)
})
})

test('preview with empty exposed entries', async () => {
vi.stubEnv('NODE_ENV', 'development')
const { renderUnicodeCompact } = await import('uqr')

const { selectKey, isCancel } = await import('@clack/prompts')
vi.mocked(selectKey).mockResolvedValue('foo')
vi.mocked(isCancel).mockReturnValue(true)

vi.mocked(renderUnicodeCompact).mockReturnValueOnce('<data>')

const rsbuild = await createRsbuild({
rsbuildConfig: {
plugins: [
pluginStubRspeedyAPI(),
pluginStubEnvEntries({}),
pluginQRCode(),
],
environments: {
custom: {},
},
dev: {
assetPrefix: 'http://example.com/',
},
server: {
port: getRandomNumberInRange(3000, 50000),
Expand Down
Loading