Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 21 additions & 10 deletions packages/core/src/cli/initConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import path from 'node:path';
import util from 'node:util';
import { loadEnv, type RsbuildEntry } from '@rsbuild/core';
import { loadConfig } from '../config';
import type { RsbuildConfigOutputTarget, RslibConfig } from '../types';
import type {
LibConfig,
RsbuildConfigOutputTarget,
RslibConfig,
} from '../types';
import { getAbsolutePath } from '../utils/helper';
import { logger } from '../utils/logger';
import type { BuildOptions, CommonOptions } from './commands';
Expand Down Expand Up @@ -85,16 +89,16 @@ export const applyCliOptions = (
lib.autoExtension = options.autoExtension;
if (options.autoExternal !== undefined)
lib.autoExternal = options.autoExternal;
const output = lib.output ?? {};
lib.output ??= {};
if (options.target !== undefined)
output.target = options.target as RsbuildConfigOutputTarget;
if (options.minify !== undefined) output.minify = options.minify;
if (options.clean !== undefined) output.cleanDistPath = options.clean;
lib.output.target = options.target as RsbuildConfigOutputTarget;
if (options.minify !== undefined) lib.output.minify = options.minify;
if (options.clean !== undefined) lib.output.cleanDistPath = options.clean;
const externals = options.externals?.filter(Boolean) ?? [];
if (externals.length > 0) output.externals = externals;
if (externals.length > 0) lib.output.externals = externals;
if (options.distPath) {
output.distPath = {
...(typeof output.distPath === 'object' ? output.distPath : {}),
lib.output.distPath = {
...(typeof lib.output.distPath === 'object' ? lib.output.distPath : {}),
root: options.distPath,
};
}
Expand All @@ -103,7 +107,7 @@ export const applyCliOptions = (

export async function initConfig(options: CommonOptions): Promise<{
config: RslibConfig;
configFilePath: string;
configFilePath?: string;
watchFiles: string[];
}> {
const cwd = process.cwd();
Expand All @@ -122,6 +126,13 @@ export async function initConfig(options: CommonOptions): Promise<{
loader: options.configLoader,
});

if (configFilePath === undefined) {
config.lib = [{} satisfies LibConfig];
logger.debug(
'No config file found. Falling back to CLI options for the default library.',
);
}

config.source ||= {};
config.source.define = {
...envs.publicVars,
Expand All @@ -136,6 +147,6 @@ export async function initConfig(options: CommonOptions): Promise<{
return {
config,
configFilePath,
watchFiles: [configFilePath, ...envs.filePaths],
watchFiles: [configFilePath, ...envs.filePaths].filter(Boolean) as string[],
};
}
18 changes: 14 additions & 4 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ const findConfig = (basePath: string): string | undefined => {
);
};

const resolveConfigPath = (root: string, customConfig?: string): string => {
const resolveConfigPath = (
root: string,
customConfig?: string,
): string | undefined => {
if (customConfig) {
const customConfigPath = isAbsolute(customConfig)
? customConfig
Expand All @@ -115,8 +118,7 @@ const resolveConfigPath = (root: string, customConfig?: string): string => {
if (configFilePath) {
return configFilePath;
}

throw new Error(`${DEFAULT_CONFIG_NAME} not found in ${root}`);
return undefined;
};

export type ConfigLoader = 'auto' | 'jiti' | 'native';
Expand All @@ -133,9 +135,17 @@ export async function loadConfig({
loader?: ConfigLoader;
}): Promise<{
content: RslibConfig;
filePath: string;
filePath?: string;
}> {
const configFilePath = resolveConfigPath(cwd, path);
if (!configFilePath) {
return {
content: {
lib: [],
},
filePath: undefined,
};
}
const { content } = await loadRsbuildConfig({
cwd: dirname(configFilePath),
path: configFilePath,
Expand Down
19 changes: 13 additions & 6 deletions tests/integration/cli/build/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,22 @@ describe('build command', async () => {
`);
});

test('should throw error if config file is absent, but it should work if the future', async () => {
test('should build when config file is absent', async () => {
const fixturePath = path.join(__dirname, 'no-config');
await fse.remove(path.join(fixturePath, 'dist'));

expect(() =>
runCliSync('build --format cjs', {
cwd: fixturePath,
}),
).toThrowError(/rslib\.config not found in.*cli[\\/]build[\\/]no-config/);
runCliSync('build --dist-path=dist/a --syntax=es2015', {
cwd: fixturePath,
});

const files = await globContentJSON(path.join(fixturePath, 'dist'));
expect(files).toMatchInlineSnapshot(`
{
"<ROOT>/tests/integration/cli/build/no-config/dist/a/index.js": "const withoutConfig = 1000;
export { withoutConfig };
",
}
`);
});

test('build options', async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/cli/build/no-config/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const withoutConfig = 'works';
export const withoutConfig = 1_000;
4 changes: 4 additions & 0 deletions website/docs/en/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Options:
--tsconfig <path> use specific tsconfig (relative to project root)
```

:::note
If the project does not provide an `rslib.config.*` file, the CLI falls back to a default single [lib](/config/lib/) configuration and applies all build options from the command line. You can still specify `--config` later once you need more complex setups.
:::

### Environment variables

Rslib supports injecting environment variables or expressions into the code during the build, which is helpful for distinguishing running environments or replacing constants. You can see more details in [Rsbuild - Environment variables](https://rsbuild.rs/guide/advanced/env-vars).
Expand Down
4 changes: 4 additions & 0 deletions website/docs/zh/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ Options:
--tsconfig <path> 使用指定的 tsconfig(相对于项目根目录)
```

:::note
如果项目中不存在 `rslib.config.*` 文件,CLI 会退回到仅包含单个 [lib](/config/lib/) 的默认配置,并按照命令行参数执行构建。一旦需要更复杂的配置或多库产物,再补充配置文件即可。
:::

### 环境变量

Rslib 支持在构建过程中向代码中注入环境变量或表达式,这对于区分运行环境、替换常量值等场景很有帮助。你可以查看 [Rsbuild - 环境变量](https://rsbuild.rs/zh/guide/advanced/env-vars) 了解更多详细信息。
Expand Down
Loading