diff --git a/.changeset/chubby-hounds-hug.md b/.changeset/chubby-hounds-hug.md new file mode 100644 index 0000000000..853d812bb3 --- /dev/null +++ b/.changeset/chubby-hounds-hug.md @@ -0,0 +1,3 @@ +--- + +--- diff --git a/.changeset/little-candies-carry.md b/.changeset/little-candies-carry.md new file mode 100644 index 0000000000..7ca9f0c3aa --- /dev/null +++ b/.changeset/little-candies-carry.md @@ -0,0 +1,22 @@ +--- +"@lynx-js/rspeedy": patch +--- + +Add `callerName` option to `createRspeedy`. + +It can be accessed by Rsbuild plugins through [`api.context.callerName`](https://rsbuild.dev/api/javascript-api/instance#contextcallername), and execute different logic based on this identifier. + +```js +export const myPlugin = { + name: 'my-plugin', + setup(api) { + const { callerName } = api.context; + + if (callerName === 'rslib') { + // ... + } else if (callerName === 'rspeedy') { + // ... + } + }, +}; +``` diff --git a/.github/workflows/relative-ci.yml b/.github/workflows/relative-ci.yml index 1ee2ec2953..de62433658 100644 --- a/.github/workflows/relative-ci.yml +++ b/.github/workflows/relative-ci.yml @@ -21,6 +21,9 @@ jobs: - path: ./packages/web-platform/web-explorer name: web-explorer key: RELATIVE_CI_PROJECT_WEB_EXPLORER_KEY + - path: ./packages/rspeedy/core + name: rspeedy + key: RELATIVE_CI_PROJECT_RSPEEDY_CORE_KEY runs-on: lynx-ubuntu-24.04-medium name: Upload ${{ matrix.project.name }} env: diff --git a/.github/workflows/workflow-bundle-analysis.yml b/.github/workflows/workflow-bundle-analysis.yml index 04577f93c7..2c50c59f6c 100644 --- a/.github/workflows/workflow-bundle-analysis.yml +++ b/.github/workflows/workflow-bundle-analysis.yml @@ -18,6 +18,8 @@ jobs: name: example-react - path: ./packages/web-platform/web-explorer name: web-explorer + - path: ./packages/rspeedy/core + name: rspeedy name: Build ${{ matrix.project.name }} steps: - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 @@ -42,6 +44,8 @@ jobs: run: | pnpm turbo build --summarize - name: Build ${{ matrix.project.name }} + env: + RSPEEDY_BUNDLE_ANALYSIS: 1 run: | pnpm --filter ${{ matrix.project.path }} run build - uses: relative-ci/agent-upload-artifact-action@a2b5741b4f7e6a989c84ec1a3059696b23c152e5 #v2 diff --git a/package.json b/package.json index e33a97b4e4..2368ac7943 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "eslint-plugin-headers": "^1.2.1", "eslint-plugin-import": "^2.31.0", "eslint-plugin-markdown": "^5.1.0", - "eslint-plugin-n": "^17.17.0", + "eslint-plugin-n": "^17.18.0", "eslint-plugin-regexp": "^2.7.0", "eslint-plugin-unicorn": "^59.0.1", "globals": "^16.1.0", diff --git a/packages/rspeedy/core/etc/rspeedy.api.md b/packages/rspeedy/core/etc/rspeedy.api.md index 35065e1c80..bd4699a590 100644 --- a/packages/rspeedy/core/etc/rspeedy.api.md +++ b/packages/rspeedy/core/etc/rspeedy.api.md @@ -71,10 +71,11 @@ export interface Config { export type ConsoleType = 'log' | 'warn' | 'error' | 'info' | 'debug' | 'profile' | 'profileEnd' | (string & Record); // @public -export function createRspeedy({ cwd, rspeedyConfig, loadEnv, environment }: CreateRspeedyOptions): Promise; +export function createRspeedy({ cwd, rspeedyConfig, loadEnv, environment, callerName, }: CreateRspeedyOptions): Promise; // @public export interface CreateRspeedyOptions { + callerName?: string; cwd?: string; environment?: CreateRsbuildOptions['environment']; loadEnv?: CreateRsbuildOptions['loadEnv']; diff --git a/packages/rspeedy/core/package.json b/packages/rspeedy/core/package.json index aff89b277d..0802e35037 100644 --- a/packages/rspeedy/core/package.json +++ b/packages/rspeedy/core/package.json @@ -72,7 +72,7 @@ "ipaddr.js": "^2.2.0", "javascript-stringify": "^2.1.0", "picocolors": "^1.1.1", - "rsbuild-plugin-publint": "0.3.1", + "rsbuild-plugin-publint": "0.3.2", "tiny-invariant": "^1.3.3", "ts-blank-space": "^0.6.1", "type-fest": "^4.41.0", diff --git a/packages/rspeedy/core/rslib.config.ts b/packages/rspeedy/core/rslib.config.ts index ebee51cca0..4359719a88 100644 --- a/packages/rspeedy/core/rslib.config.ts +++ b/packages/rspeedy/core/rslib.config.ts @@ -9,6 +9,9 @@ export default defineConfig({ syntax: 'es2022', dts: { bundle: true }, plugins: [pluginTypia()], + performance: { + profile: !!process.env.RSPEEDY_BUNDLE_ANALYSIS, + }, }, { format: 'esm', diff --git a/packages/rspeedy/core/src/config/source/index.ts b/packages/rspeedy/core/src/config/source/index.ts index b8b807869d..d97172b8b1 100644 --- a/packages/rspeedy/core/src/config/source/index.ts +++ b/packages/rspeedy/core/src/config/source/index.ts @@ -245,7 +245,9 @@ export interface Source { * ```js * import { defineConfig } from '@lynx-js/rspeedy' * export default defineConfig({ - * entry: './src/pages/main/index.js', + * source: { + * entry: './src/pages/main/index.js', + * }, * }) * ``` * @@ -256,7 +258,9 @@ export interface Source { * ```js * import { defineConfig } from '@lynx-js/rspeedy' * export default defineConfig({ - * entry: ['./src/prefetch.js', './src/pages/main/index.js'], + * source: { + * entry: ['./src/prefetch.js', './src/pages/main/index.js'], + * }, * }) * ``` * @@ -267,9 +271,11 @@ export interface Source { * ```js * import { defineConfig } from '@lynx-js/rspeedy' * export default defineConfig({ - * entry: { - * foo: './src/pages/foo/index.js', - * bar: ['./src/pages/bar/index.js', './src/post.js'], // multiple entry modules is allowed + * source: { + * entry: { + * foo: './src/pages/foo/index.js', + * bar: ['./src/pages/bar/index.js', './src/post.js'], // multiple entry modules is allowed + * }, * }, * }) * ``` @@ -281,10 +287,12 @@ export interface Source { * ```js * import { defineConfig } from '@lynx-js/rspeedy' * export default defineConfig({ - * entry: { - * foo: './src/pages/foo/index.js', - * bar: { - * import: ['./src/prefetch.js', './src/pages/bar'], + * source: { + * entry: { + * foo: './src/pages/foo/index.js', + * bar: { + * import: ['./src/prefetch.js', './src/pages/bar'], + * }, * }, * }, * }) diff --git a/packages/rspeedy/core/src/create-rspeedy.ts b/packages/rspeedy/core/src/create-rspeedy.ts index 27a0395c53..b41ea12fe6 100644 --- a/packages/rspeedy/core/src/create-rspeedy.ts +++ b/packages/rspeedy/core/src/create-rspeedy.ts @@ -55,6 +55,43 @@ export interface CreateRspeedyOptions { * @defaultValue [] */ environment?: CreateRsbuildOptions['environment'] + + /** + * The name of the framework or tool that is currently invoking Rsbuild. + * This allows plugins to tailor their behavior based on the calling context. + * + * @example + * + * Rsbuild plugins can access this value via `api.context.callerName`. + * + * ```js + * export function myPlugin() { + * return { + * name: 'my-plugin', + * setup(api) { + * // Log the name of the tool invoking Rsbuild + * console.log(`Called by: ${api.context.callerName}`); + * + * // Conditionally apply plugin logic based on caller + * if (api.context.callerName === 'rspeedy') { + * api.modifyRsbuildConfig((config) => { + * // Apply rspeedy-specific config changes + * return config; + * }); + * } else if (api.context.callerName === 'rslib') { + * api.modifyRsbuildConfig((config) => { + * // Apply rslib-specific config changes + * return config; + * }); + * } + * } + * }; + * } + * ``` + * + * @defaultValue 'rspeedy' + */ + callerName?: string } /** * The `createRspeedy` method can let you create a Rspeedy instance and you can customize the build or development process in Node.js Runtime. @@ -76,8 +113,13 @@ export interface CreateRspeedyOptions { * @public */ export async function createRspeedy( - { cwd = process.cwd(), rspeedyConfig = {}, loadEnv = true, environment = [] }: - CreateRspeedyOptions, + { + cwd = process.cwd(), + rspeedyConfig = {}, + loadEnv = true, + environment = [], + callerName = 'rspeedy', + }: CreateRspeedyOptions, ): Promise { const config = applyDefaultRspeedyConfig(rspeedyConfig) @@ -87,6 +129,7 @@ export async function createRspeedy( loadEnv, rsbuildConfig: toRsbuildConfig(config) as RsbuildConfig, environment, + callerName, }), import('./plugins/index.js'), ]) diff --git a/packages/rspeedy/core/test/createRspeedy.test.ts b/packages/rspeedy/core/test/createRspeedy.test.ts new file mode 100644 index 0000000000..4c9475949f --- /dev/null +++ b/packages/rspeedy/core/test/createRspeedy.test.ts @@ -0,0 +1,48 @@ +// 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 type { RsbuildPluginAPI } from '@rsbuild/core' +import { describe, expect, test } from 'vitest' + +import { createRspeedy } from '../src/create-rspeedy.js' + +describe('createRspeedy', () => { + test('default callerName', async () => { + const rspeedy = await createRspeedy({ + rspeedyConfig: { + plugins: [ + { + name: 'test', + setup(api: RsbuildPluginAPI) { + expect(api.context.callerName).toBe('rspeedy') + }, + }, + ], + }, + }) + + await rspeedy.initConfigs() + + expect.assertions(1) + }) + + test('custom callerName', async () => { + const rspeedy = await createRspeedy({ + callerName: 'my-custom-framework', + rspeedyConfig: { + plugins: [ + { + name: 'test', + setup(api: RsbuildPluginAPI) { + expect(api.context.callerName).toBe('my-custom-framework') + }, + }, + ], + }, + }) + + await rspeedy.initConfigs() + + expect.assertions(1) + }) +}) diff --git a/packages/rspeedy/core/turbo.json b/packages/rspeedy/core/turbo.json index 3eda40d2fa..c4be311f1d 100644 --- a/packages/rspeedy/core/turbo.json +++ b/packages/rspeedy/core/turbo.json @@ -8,6 +8,9 @@ "dependsOn": [ "^build" ], + "env": [ + "RSPEEDY_BUNDLE_ANALYSIS" + ], "inputs": [ "register", "src", diff --git a/packages/rspeedy/create-rspeedy/package.json b/packages/rspeedy/create-rspeedy/package.json index 63abac6733..65c4fbf4ed 100644 --- a/packages/rspeedy/create-rspeedy/package.json +++ b/packages/rspeedy/create-rspeedy/package.json @@ -35,7 +35,7 @@ "test": "vitest" }, "dependencies": { - "create-rstack": "1.4.1" + "create-rstack": "1.4.2" }, "devDependencies": { "@lynx-js/qrcode-rsbuild-plugin": "workspace:^", diff --git a/packages/testing-library/testing-environment/package.json b/packages/testing-library/testing-environment/package.json index aa957b7a0d..bdf4bbe5a1 100644 --- a/packages/testing-library/testing-environment/package.json +++ b/packages/testing-library/testing-environment/package.json @@ -53,6 +53,6 @@ "devDependencies": { "@testing-library/jest-dom": "^6.6.3", "@types/jsdom": "^21.1.7", - "rsbuild-plugin-publint": "0.3.1" + "rsbuild-plugin-publint": "0.3.2" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 901e1efc6d..78a7ff9a9a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -113,8 +113,8 @@ importers: specifier: ^5.1.0 version: 5.1.0(eslint@9.26.0(jiti@2.4.2)) eslint-plugin-n: - specifier: ^17.17.0 - version: 17.17.0(eslint@9.26.0(jiti@2.4.2)) + specifier: ^17.18.0 + version: 17.18.0(eslint@9.26.0(jiti@2.4.2)) eslint-plugin-regexp: specifier: ^2.7.0 version: 2.7.0(eslint@9.26.0(jiti@2.4.2)) @@ -331,8 +331,8 @@ importers: specifier: ^1.1.1 version: 1.1.1 rsbuild-plugin-publint: - specifier: 0.3.1 - version: 0.3.1(@rsbuild/core@1.3.18) + specifier: 0.3.2 + version: 0.3.2(@rsbuild/core@1.3.18) tiny-invariant: specifier: ^1.3.3 version: 1.3.3 @@ -358,8 +358,8 @@ importers: packages/rspeedy/create-rspeedy: dependencies: create-rstack: - specifier: 1.4.1 - version: 1.4.1 + specifier: 1.4.2 + version: 1.4.2 devDependencies: '@lynx-js/qrcode-rsbuild-plugin': specifier: workspace:^ @@ -582,8 +582,8 @@ importers: specifier: ^21.1.7 version: 21.1.7 rsbuild-plugin-publint: - specifier: 0.3.1 - version: 0.3.1(@rsbuild/core@1.3.18) + specifier: 0.3.2 + version: 0.3.2(@rsbuild/core@1.3.18) packages/third-party/tailwind-preset: devDependencies: @@ -4221,8 +4221,8 @@ packages: resolution: {integrity: sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==} engines: {node: '>= 0.10'} - create-rstack@1.4.1: - resolution: {integrity: sha512-xg5GCjnW1G2/KJ1WokiOOcCE6jvlT6I+468EvdzQi0mjSQZ0HS/U6/eln1WbcR5itJbQBpGnSoGZmUzew0sp7w==} + create-rstack@1.4.2: + resolution: {integrity: sha512-29T3PENApWZkTkB6IXy69fSRckjil9zW8xr2tSgyIWQQ1FDlzXUC4rAVF6Up+1aQCartgx5iETWealL819ZZeg==} cross-env@7.0.3: resolution: {integrity: sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==} @@ -4830,8 +4830,8 @@ packages: peerDependencies: eslint: '>=8' - eslint-plugin-n@17.17.0: - resolution: {integrity: sha512-2VvPK7Mo73z1rDFb6pTvkH6kFibAmnTubFq5l83vePxu0WiY1s0LOtj2WHb6Sa40R3w4mnh8GFYbHBQyMlotKw==} + eslint-plugin-n@17.18.0: + resolution: {integrity: sha512-hvZ/HusueqTJ7VDLoCpjN0hx4N4+jHIWTXD4TMLHy9F23XkDagR9v+xQWRWR57yY55GPF8NnD4ox9iGTxirY8A==} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} peerDependencies: eslint: '>=8.23.0' @@ -6683,6 +6683,9 @@ packages: package-manager-detector@0.2.11: resolution: {integrity: sha512-BEnLolu+yuz22S56CU1SUKq3XC3PkwD5wv4ikR4MfGvnRVcmzXR9DwSlW2fEamyTPyXHomBJRzgapeuBvRNzJQ==} + package-manager-detector@1.3.0: + resolution: {integrity: sha512-ZsEbbZORsyHuO00lY1kV3/t72yp6Ysay6Pd17ZAlNGuGwmWDLCJxFpRs0IzfXfj1o4icJOkUEioexFHzyPurSQ==} + param-case@3.0.4: resolution: {integrity: sha512-RXlj7zCYokReqWpOPH9oYivUzLYZ5vAPIfEmCTNViosC78F8F0H9y7T7gG2M39ymgutxF5gcFEsyZQSph9Bp3A==} @@ -7103,8 +7106,8 @@ packages: proxy-from-env@1.1.0: resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} - publint@0.3.9: - resolution: {integrity: sha512-irTwfRfYW38vomkxxoiZQtFtUOQKpz5m0p9Z60z4xpXrl1KmvSrX1OMARvnnolB5usOXeNfvLj6d/W3rwXKfBQ==} + publint@0.3.12: + resolution: {integrity: sha512-1w3MMtL9iotBjm1mmXtG3Nk06wnq9UhGNRpQ2j6n1Zq7YAD6gnxMMZMIxlRPAydVjVbjSm+n0lhwqsD1m4LD5w==} engines: {node: '>=18'} hasBin: true @@ -7392,8 +7395,8 @@ packages: typescript: optional: true - rsbuild-plugin-publint@0.3.1: - resolution: {integrity: sha512-oBeem58KQa9LDWjRAsUZjoOzmw53X/EeIOKE+swG8pudVRSR0a9FSGNvLMJdZCmB3iw1d+brmGeJlnDi81mthQ==} + rsbuild-plugin-publint@0.3.2: + resolution: {integrity: sha512-vFqPvierA+ePsVouf05RDIMbsPoylDxveLTK+FS8mFxpFij9mM7uHl/8YQCbGOPpoY1qeykfkIEKZroXMuSU3A==} peerDependencies: '@rsbuild/core': 1.x peerDependenciesMeta: @@ -12134,7 +12137,7 @@ snapshots: object-assign: 4.1.1 vary: 1.1.2 - create-rstack@1.4.1: {} + create-rstack@1.4.2: {} cross-env@7.0.3: dependencies: @@ -12886,7 +12889,7 @@ snapshots: transitivePeerDependencies: - supports-color - eslint-plugin-n@17.17.0(eslint@9.26.0(jiti@2.4.2)): + eslint-plugin-n@17.18.0(eslint@9.26.0(jiti@2.4.2)): dependencies: '@eslint-community/eslint-utils': 4.7.0(eslint@9.26.0(jiti@2.4.2)) enhanced-resolve: 5.18.1 @@ -15247,6 +15250,8 @@ snapshots: dependencies: quansync: 0.2.10 + package-manager-detector@1.3.0: {} + param-case@3.0.4: dependencies: dot-case: 3.0.4 @@ -15616,10 +15621,10 @@ snapshots: proxy-from-env@1.1.0: {} - publint@0.3.9: + publint@0.3.12: dependencies: '@publint/pack': 0.1.2 - package-manager-detector: 0.2.11 + package-manager-detector: 1.3.0 picocolors: 1.1.1 sade: 1.8.1 @@ -15995,10 +16000,10 @@ snapshots: '@microsoft/api-extractor': 7.52.7(@types/node@22.15.17) typescript: 5.8.3 - rsbuild-plugin-publint@0.3.1(@rsbuild/core@1.3.18): + rsbuild-plugin-publint@0.3.2(@rsbuild/core@1.3.18): dependencies: picocolors: 1.1.1 - publint: 0.3.9 + publint: 0.3.12 optionalDependencies: '@rsbuild/core': 1.3.18