Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
shulaoda committed Aug 24, 2024
1 parent 60e60fe commit 8cac7c0
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 9 deletions.
3 changes: 3 additions & 0 deletions examples/zero-configuration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @examples/zero-configuration

This example demonstrates how to use Rslib's zero configuration feature.
12 changes: 12 additions & 0 deletions examples/zero-configuration/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@examples/zero-configuration",
"private": true,
"scripts": {
"build": "rslib build"
},
"dependencies": {},
"devDependencies": {
"@rslib/core": "workspace:*",
"typescript": "^5.5.4"
}
}
1 change: 1 addition & 0 deletions examples/zero-configuration/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(123);
6 changes: 6 additions & 0 deletions examples/zero-configuration/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"strict": true
},
"include": ["src/**/*"]
}
29 changes: 21 additions & 8 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { getDefaultExtension } from './utils/extension';
import {
calcLongestCommonPath,
color,
getExportEntries,
isObject,
nodeBuiltInModules,
readPackageJson,
Expand All @@ -48,7 +49,10 @@ const findConfig = (basePath: string): string | undefined => {
return DEFAULT_EXTENSIONS.map((ext) => basePath + ext).find(fs.existsSync);
};

const resolveConfigPath = (root: string, customConfig?: string): string => {
const resolveConfigPath = (
root: string,
customConfig?: string,
): string | null => {
if (customConfig) {
const customConfigPath = isAbsolute(customConfig)
? customConfig
Expand All @@ -65,7 +69,7 @@ const resolveConfigPath = (root: string, customConfig?: string): string => {
return configFilePath;
}

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

export async function loadConfig({
Expand All @@ -78,13 +82,22 @@ export async function loadConfig({
envMode?: string;
}): Promise<RslibConfig> {
const configFilePath = resolveConfigPath(cwd, path);
const { content } = await loadRsbuildConfig({
cwd: dirname(configFilePath),
path: configFilePath,
envMode,
});
if (configFilePath) {
const { content } = await loadRsbuildConfig({
cwd: dirname(configFilePath),
path: configFilePath,
envMode,
});

return content as RslibConfig;
}

const pkgJson = readPackageJson(cwd);
if (pkgJson) {
const exportEntries = getExportEntries(pkgJson);
}

return content as RslibConfig;
return {} as RslibConfig;
}

export const composeAutoExternalConfig = (options: {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import type { RsbuildConfig } from '@rsbuild/core';

export type Format = 'esm' | 'cjs' | 'umd';

export type PackageType = 'module' | 'commonjs';

export type ExportEntry = {
outputPath: string;
type: PackageType | 'types';
from: string;
};

export type EcmaScriptVersion =
| 'esnext'
| 'es5'
Expand Down
52 changes: 51 additions & 1 deletion packages/core/src/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'node:fs';
import fsP from 'node:fs/promises';
import path from 'node:path';
import color from 'picocolors';
import type { PackageJson } from '../types';
import type { ExportEntry, PackageJson, PackageType } from '../types';
import { logger } from './logger';

/**
Expand Down Expand Up @@ -123,6 +123,56 @@ export const readPackageJson = (rootPath: string): undefined | PackageJson => {
}
};

export const getExportEntries = (pkgJson: PackageJson): ExportEntry[] => {
const exportEntriesMap: Record<string, ExportEntry> = {};
const packageType = pkgJson.type ?? 'commonjs';

const getFileType = (filePath: string): PackageType => {
if (filePath.endsWith('.mjs')) {
return 'module';
}

if (filePath.endsWith('.cjs')) {
return 'commonjs';
}

return packageType;
};

const addExportPath = (
exportPathsMap: Record<string, ExportEntry>,
exportEntry: any,
) => {
exportEntry.outputPath = path.normalize(exportEntry.outputPath);

const { outputPath: exportPath, type } = exportEntry;

const existingExportPath = exportPathsMap[exportPath];
if (existingExportPath) {
if (existingExportPath.type !== type) {
throw new Error(
`Conflicting export types "${existingExportPath.type}" & "${type}" found for ${exportPath}`,
);
}

Object.assign(existingExportPath, exportEntry);
} else {
exportPathsMap[exportPath] = exportEntry;
}
};

if (pkgJson.main) {
const mainPath = pkgJson.main;
addExportPath(exportEntriesMap, {
outputPath: mainPath,
type: getFileType(mainPath),
from: 'main',
});
}

return Object.values(exportEntriesMap);
};

export const isObject = (obj: unknown): obj is Record<string, any> =>
Object.prototype.toString.call(obj) === '[object Object]';

Expand Down
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8cac7c0

Please sign in to comment.