From df12da2bc4034e7726a224ac86f7c1cc960b6ccb Mon Sep 17 00:00:00 2001 From: Timeless0911 <50201324+Timeless0911@users.noreply.github.com> Date: Wed, 3 Jul 2024 02:13:38 +0800 Subject: [PATCH] feat: use environment API to build (#23) --- e2e/package.json | 1 + e2e/scripts/shared.ts | 19 ++++- packages/core/package.json | 2 +- packages/core/src/build.ts | 19 ++--- packages/core/src/config.ts | 37 +++++---- .../tests/__snapshots__/config.test.ts.snap | 59 ++++++++------ pnpm-lock.yaml | 79 ++++++++++++------- 7 files changed, 130 insertions(+), 86 deletions(-) diff --git a/e2e/package.json b/e2e/package.json index fe91f47d2..3e967936e 100644 --- a/e2e/package.json +++ b/e2e/package.json @@ -11,6 +11,7 @@ }, "devDependencies": { "@playwright/test": "1.43.1", + "@rsbuild/core": "1.0.0-alpha.0", "@rslib/core": "workspace:*", "@rslib/tsconfig": "workspace:*", "@types/fs-extra": "^11.0.4", diff --git a/e2e/scripts/shared.ts b/e2e/scripts/shared.ts index 7bba7a009..8d233d4bc 100644 --- a/e2e/scripts/shared.ts +++ b/e2e/scripts/shared.ts @@ -1,9 +1,13 @@ import { join } from 'node:path'; +import { mergeRsbuildConfig as mergeConfig } from '@rsbuild/core'; import type { LibConfig, RslibConfig } from '@rslib/core'; import { globContentJSON } from '#helper'; -export function generateBundleEsmConfig(cwd: string): LibConfig { - return { +export function generateBundleEsmConfig( + cwd: string, + config: LibConfig = {}, +): LibConfig { + const esmBasicConfig: LibConfig = { format: 'esm', output: { distPath: { @@ -11,10 +15,15 @@ export function generateBundleEsmConfig(cwd: string): LibConfig { }, }, }; + + return mergeConfig(esmBasicConfig, config); } -export function generateBundleCjsConfig(cwd: string): LibConfig { - return { +export function generateBundleCjsConfig( + cwd: string, + config: LibConfig = {}, +): LibConfig { + const cjsBasicConfig: LibConfig = { format: 'cjs', output: { distPath: { @@ -22,6 +31,8 @@ export function generateBundleCjsConfig(cwd: string): LibConfig { }, }, }; + + return mergeConfig(cjsBasicConfig, config); } export async function getEntryJsResults(rslibConfig: RslibConfig) { diff --git a/packages/core/package.json b/packages/core/package.json index 479c94341..d511256c2 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -38,7 +38,7 @@ "prebundle": "prebundle" }, "dependencies": { - "@rsbuild/core": "0.7.10" + "@rsbuild/core": "1.0.0-alpha.0" }, "devDependencies": { "@rslib/tsconfig": "workspace:*", diff --git a/packages/core/src/build.ts b/packages/core/src/build.ts index 7ac7ff172..10cfd35e1 100644 --- a/packages/core/src/build.ts +++ b/packages/core/src/build.ts @@ -1,21 +1,14 @@ -import type { RsbuildInstance } from '@rsbuild/core'; import type { BuildOptions } from './cli/commands'; import { initRsbuild } from './config'; import type { RslibConfig } from './types/config'; export async function build(config: RslibConfig, options?: BuildOptions) { - const rsbuildInstances = await initRsbuild(config); + const rsbuildInstance = await initRsbuild(config); - const buildPromises = rsbuildInstances.map( - async (rsbuildInstance: RsbuildInstance) => { - return await rsbuildInstance.build({ - mode: 'production', - watch: options?.watch, - }); - }, - ); + await rsbuildInstance.build({ + mode: 'production', + watch: options?.watch, + }); - await Promise.all(buildPromises); - - return rsbuildInstances; + return rsbuildInstance; } diff --git a/packages/core/src/config.ts b/packages/core/src/config.ts index 194924b54..eb661e207 100644 --- a/packages/core/src/config.ts +++ b/packages/core/src/config.ts @@ -8,6 +8,7 @@ import { } from '@rsbuild/core'; import { DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS } from './constant'; import type { + Format, LibConfig, RslibConfig, RslibConfigAsyncFn, @@ -73,6 +74,9 @@ export async function loadConfig( export async function createInternalRsbuildConfig(): Promise { return defineRsbuildConfig({ + dev: { + progressBar: false, + }, tools: { htmlPlugin: false, }, @@ -143,17 +147,20 @@ export function convertLibConfigToRsbuildConfig( export async function composeCreateRsbuildConfig( rslibConfig: RslibConfig, -): Promise { +): Promise>> { const internalRsbuildConfig = await createInternalRsbuildConfig(); const { lib: libConfigsArray, ...sharedRsbuildConfig } = rslibConfig; if (!libConfigsArray) { - logger.error('You must specify lib field in config file.'); - return []; + throw new Error( + `Expect lib field to be an array, but got ${libConfigsArray}.`, + ); } - const composedRsbuildConfig = libConfigsArray.map((libConfig: LibConfig) => { + const composedRsbuildConfig: Partial> = {}; + + for (const libConfig of libConfigsArray) { const { format, ...overrideRsbuildConfig } = libConfig; // Merge order matters, keep `internalRsbuildConfig` at the last position @@ -164,23 +171,21 @@ export async function composeCreateRsbuildConfig( internalRsbuildConfig, ); - return convertLibConfigToRsbuildConfig(libConfig, mergedRsbuildConfig); - }); + composedRsbuildConfig[format!] = convertLibConfigToRsbuildConfig( + libConfig, + mergedRsbuildConfig, + ); + } return composedRsbuildConfig; } export async function initRsbuild(rslibConfig: RslibConfig) { - // TODO: use environment API instead - const rsbuildConfigArray = await composeCreateRsbuildConfig(rslibConfig); + const rsbuildConfigObject = await composeCreateRsbuildConfig(rslibConfig); - const rsbuildPromises = rsbuildConfigArray.map( - async (rsbuildConfig: RsbuildConfig) => { - return createRsbuild({ rsbuildConfig }); + return createRsbuild({ + rsbuildConfig: { + environments: rsbuildConfigObject, }, - ); - - const rsbuildInstances = await Promise.all(rsbuildPromises); - - return rsbuildInstances; + }); } diff --git a/packages/core/tests/__snapshots__/config.test.ts.snap b/packages/core/tests/__snapshots__/config.test.ts.snap index 66b1f7ab6..b36746bcb 100644 --- a/packages/core/tests/__snapshots__/config.test.ts.snap +++ b/packages/core/tests/__snapshots__/config.test.ts.snap @@ -1,8 +1,11 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1`] = ` -[ - { +{ + "cjs": { + "dev": { + "progressBar": false, + }, "output": { "distPath": { "js": "./", @@ -12,31 +15,30 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 }, "source": { "alias": { - "bar": "bar", - "foo": "foo/esm", + "bar": "bar/cjs", + "foo": "foo", }, - "preEntry": "./b.js", + "preEntry": [ + "./a.js", + "./c.js", + "./d.js", + ], }, "tools": { "htmlPlugin": false, "rspack": { - "experiments": { - "outputModule": true, - }, - "optimization": { - "concatenateModules": true, - }, "output": { - "iife": false, "library": { - "type": "modern-module", + "type": "commonjs", }, - "module": true, }, }, }, }, - { + "esm": { + "dev": { + "progressBar": false, + }, "output": { "distPath": { "js": "./", @@ -46,27 +48,34 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 }, "source": { "alias": { - "bar": "bar/cjs", - "foo": "foo", + "bar": "bar", + "foo": "foo/esm", }, - "preEntry": [ - "./a.js", - "./c.js", - "./d.js", - ], + "preEntry": "./b.js", }, "tools": { "htmlPlugin": false, "rspack": { + "experiments": { + "outputModule": true, + }, + "optimization": { + "concatenateModules": true, + }, "output": { + "iife": false, "library": { - "type": "commonjs", + "type": "modern-module", }, + "module": true, }, }, }, }, - { + "umd": { + "dev": { + "progressBar": false, + }, "output": { "distPath": { "js": "./", @@ -92,5 +101,5 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1 }, }, }, -] +} `; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 66d0f3e9e..fe791662a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -50,6 +50,9 @@ importers: '@playwright/test': specifier: 1.43.1 version: 1.43.1 + '@rsbuild/core': + specifier: 1.0.0-alpha.0 + version: 1.0.0-alpha.0 '@rslib/core': specifier: workspace:* version: link:../packages/core @@ -84,8 +87,8 @@ importers: packages/core: dependencies: '@rsbuild/core': - specifier: 0.7.10 - version: 0.7.10 + specifier: 1.0.0-alpha.0 + version: 1.0.0-alpha.0 devDependencies: '@rslib/tsconfig': specifier: workspace:* @@ -1094,13 +1097,13 @@ packages: cpu: [x64] os: [win32] - '@rsbuild/core@0.7.10': - resolution: {integrity: sha512-m+JbPpuMFuVsMRcsjMxvVk6yc//OW+h72kV2DAD4neoiM0YhkEAN4TXBz3RSOomXHODhhxqhpCqz9nIw6PtvBA==} - engines: {node: '>=16.0.0'} + '@rsbuild/core@1.0.0-alpha.0': + resolution: {integrity: sha512-GVfPf7Vi2XigAxz/X0ayoeCvTqvAZ6VltH4Xxqmcxd3tscu/MW1m2uXqsK58L27m47Y7iKpPlA1EahOs3cnKGg==} + engines: {node: '>=16.7.0'} hasBin: true - '@rsbuild/shared@0.7.10': - resolution: {integrity: sha512-FwTm11DP7KxQKT2mWLvwe80O5KpikgMSlqnw9CQhBaIHSYEypdJU9ZotbNsXsHdML3xcqg+S9ae3bpovC7KlwQ==} + '@rsbuild/shared@1.0.0-alpha.0': + resolution: {integrity: sha512-pv+OaUzazvIpNdZylpjP39KUOMWku7AdyWN8kiSKEJchUO0HjpoxKg3ym2FMDODpOZS0j0ytpDzi3aF3d/lc8g==} '@rspack/binding-darwin-arm64@0.7.5-canary-0d03907-20240624125011': resolution: {integrity: sha512-9e2dMR8BFB0rlfAKIVPmGCb9oaS86EWRbetWzKcqVTw46tHmi30De1eN5ElAfo3q0AtEruCfX7DDa0Shdja1xQ==} @@ -1162,6 +1165,9 @@ packages: '@sinclair/typebox@0.27.8': resolution: {integrity: sha512-+Fj43pSMwJs4KRrH/938Uf+uAELIgVBmQzg/q1YG10djyfA3TnrU8N8XzqCh/okZdszqBQTZf96idMfE5lnwTA==} + '@swc/helpers@0.5.11': + resolution: {integrity: sha512-YNlnKRWF2sVojTpIyzwou9XoTNbzbzONwRhOoniEioF1AtaitTvVZblaQRrAzChWQ1bLYyYSWzM18y4WwgzJ+A==} + '@swc/helpers@0.5.3': resolution: {integrity: sha512-FaruWX6KdudYloq1AHD/4nU+UsMTdNE8CKyrseXWEcgjDAbvkwJg2QGPAnfIJLIWsjZOSPLOAykK6fuYp4vp4A==} @@ -1359,6 +1365,9 @@ packages: caniuse-lite@1.0.30001636: resolution: {integrity: sha512-bMg2vmr8XBsbL6Lr0UHXy/21m84FTxDLWn2FSqMd5PrlbMxwJlQnC2YWYxVgp66PZE+BBNF2jYQUBKCo1FDeZg==} + caniuse-lite@1.0.30001639: + resolution: {integrity: sha512-eFHflNTBIlFwP2AIKaYuBQN/apnUoKNhBdza8ZnW/h2di4LCZ4xFqYlxUxo+LQ76KFI1PGcC1QDxMbxTZpSCAg==} + chai@4.4.1: resolution: {integrity: sha512-13sOfMv2+DWduEU+/xbun3LScLoqN17nBeTLUsmDfKdoiC1fr0n9PU4guu4AhRcOVFk/sW8LyZWHuhWtQZiF+g==} engines: {node: '>=4'} @@ -1450,8 +1459,8 @@ packages: convert-source-map@2.0.0: resolution: {integrity: sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg==} - core-js@3.36.1: - resolution: {integrity: sha512-BTvUrwxVBezj5SZ3f10ImnX2oRByMxql3EimVqMysepbC9EeMUOpLwdy6Eoili2x6E4kf+ZUB5k/+Jv55alPfA==} + core-js@3.37.1: + resolution: {integrity: sha512-Xn6qmxrQZyB0FFY8E3bgRXei3lWDJHhvI+u0q9TKIYM49G8pAr0FgnnrFRAmsbptZL1yxRADVXn+x5AGsbBfyw==} cross-env@7.0.3: resolution: {integrity: sha512-+/HKd6EgcQCJGh2PSjZuUitQBQynKor4wrFbRg4DtAgS1aWO+gU52xpH7M9ScGgXSYmAVS9bIJ8EzuaGw0oNAw==} @@ -2466,6 +2475,10 @@ packages: resolution: {integrity: sha512-Wglpdk03BSfXkHoQa3b/oulrotAkwrlLDRSOb9D0bN86FdRyE9lppSp33aHNPgBa0JKCoB+drFLZkQoRRYae5A==} engines: {node: ^10 || ^12 || >=14} + postcss@8.4.39: + resolution: {integrity: sha512-0vzE+lAiG7hZl1/9I8yzKLx3aR9Xbof3fBHKunvMfOCYAtMhrsnccJY2iTURb9EZd5+pLuiNV9/c/GZJOHsgIw==} + engines: {node: ^10 || ^12 || >=14} + prebundle@1.1.0: resolution: {integrity: sha512-yTfRjx0+xiveeb7kO77OcODVB8RSHMKIiVl/qferU7ZHw4Y8pycXkCAtPDViF8YDo0a8ViDpm4C1O9PFKCw1ig==} hasBin: true @@ -3994,21 +4007,21 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.18.0': optional: true - '@rsbuild/core@0.7.10': + '@rsbuild/core@1.0.0-alpha.0': dependencies: - '@rsbuild/shared': 0.7.10(@swc/helpers@0.5.3) - '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3) - '@swc/helpers': 0.5.3 - core-js: 3.36.1 - html-webpack-plugin: html-rspack-plugin@5.7.2(@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3)) - postcss: 8.4.38 + '@rsbuild/shared': 1.0.0-alpha.0(@swc/helpers@0.5.11) + '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11) + '@swc/helpers': 0.5.11 + core-js: 3.37.1 + html-webpack-plugin: html-rspack-plugin@5.7.2(@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11)) + postcss: 8.4.39 - '@rsbuild/shared@0.7.10(@swc/helpers@0.5.3)': + '@rsbuild/shared@1.0.0-alpha.0(@swc/helpers@0.5.11)': dependencies: - '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3) - caniuse-lite: 1.0.30001636 - html-webpack-plugin: html-rspack-plugin@5.7.2(@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3)) - postcss: 8.4.38 + '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11) + caniuse-lite: 1.0.30001639 + html-webpack-plugin: html-rspack-plugin@5.7.2(@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11)) + postcss: 8.4.39 optionalDependencies: fsevents: 2.3.3 transitivePeerDependencies: @@ -4053,18 +4066,22 @@ snapshots: '@rspack/binding-win32-ia32-msvc': 0.7.5-canary-0d03907-20240624125011 '@rspack/binding-win32-x64-msvc': 0.7.5-canary-0d03907-20240624125011 - '@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3)': + '@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11)': dependencies: '@module-federation/runtime-tools': 0.1.6 '@rspack/binding': 0.7.5-canary-0d03907-20240624125011 - caniuse-lite: 1.0.30001636 + caniuse-lite: 1.0.30001639 tapable: 2.2.1 webpack-sources: 3.2.3 optionalDependencies: - '@swc/helpers': 0.5.3 + '@swc/helpers': 0.5.11 '@sinclair/typebox@0.27.8': {} + '@swc/helpers@0.5.11': + dependencies: + tslib: 2.6.2 + '@swc/helpers@0.5.3': dependencies: tslib: 2.6.2 @@ -4281,6 +4298,8 @@ snapshots: caniuse-lite@1.0.30001636: {} + caniuse-lite@1.0.30001639: {} + chai@4.4.1: dependencies: assertion-error: 1.1.0 @@ -4376,7 +4395,7 @@ snapshots: convert-source-map@2.0.0: {} - core-js@3.36.1: {} + core-js@3.37.1: {} cross-env@7.0.3: dependencies: @@ -4895,9 +4914,9 @@ snapshots: hosted-git-info@2.8.9: {} - html-rspack-plugin@5.7.2(@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3)): + html-rspack-plugin@5.7.2(@rspack/core@0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11)): optionalDependencies: - '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.3) + '@rspack/core': 0.7.5-canary-0d03907-20240624125011(@swc/helpers@0.5.11) human-id@1.0.2: {} @@ -5471,6 +5490,12 @@ snapshots: picocolors: 1.0.1 source-map-js: 1.2.0 + postcss@8.4.39: + dependencies: + nanoid: 3.3.7 + picocolors: 1.0.1 + source-map-js: 1.2.0 + prebundle@1.1.0(typescript@5.4.5): dependencies: '@vercel/ncc': 0.38.1