Skip to content

Commit

Permalink
feat: use environment API to build (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 authored Jul 2, 2024
1 parent a645e60 commit df12da2
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 86 deletions.
1 change: 1 addition & 0 deletions e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
19 changes: 15 additions & 4 deletions e2e/scripts/shared.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,38 @@
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: {
root: join(cwd, './dist/esm'),
},
},
};

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: {
root: join(cwd, './dist/cjs'),
},
},
};

return mergeConfig(cjsBasicConfig, config);
}

export async function getEntryJsResults(rslibConfig: RslibConfig) {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"prebundle": "prebundle"
},
"dependencies": {
"@rsbuild/core": "0.7.10"
"@rsbuild/core": "1.0.0-alpha.0"
},
"devDependencies": {
"@rslib/tsconfig": "workspace:*",
Expand Down
19 changes: 6 additions & 13 deletions packages/core/src/build.ts
Original file line number Diff line number Diff line change
@@ -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;
}
37 changes: 21 additions & 16 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@rsbuild/core';
import { DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS } from './constant';
import type {
Format,
LibConfig,
RslibConfig,
RslibConfigAsyncFn,
Expand Down Expand Up @@ -73,6 +74,9 @@ export async function loadConfig(

export async function createInternalRsbuildConfig(): Promise<RsbuildConfig> {
return defineRsbuildConfig({
dev: {
progressBar: false,
},
tools: {
htmlPlugin: false,
},
Expand Down Expand Up @@ -143,17 +147,20 @@ export function convertLibConfigToRsbuildConfig(

export async function composeCreateRsbuildConfig(
rslibConfig: RslibConfig,
): Promise<RsbuildConfig[]> {
): Promise<Partial<Record<Format, RsbuildConfig>>> {
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<Record<Format, RsbuildConfig>> = {};

for (const libConfig of libConfigsArray) {
const { format, ...overrideRsbuildConfig } = libConfig;

// Merge order matters, keep `internalRsbuildConfig` at the last position
Expand All @@ -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;
});
}
59 changes: 34 additions & 25 deletions packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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": "./",
Expand All @@ -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": "./",
Expand All @@ -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": "./",
Expand All @@ -92,5 +101,5 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config 1
},
},
},
]
}
`;
Loading

0 comments on commit df12da2

Please sign in to comment.