Skip to content

Commit

Permalink
chore: dedup & merge composeAutoExternalConfig to config.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
9aoy committed Aug 8, 2024
1 parent 6babfee commit 6fce2ed
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 60 deletions.
56 changes: 55 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import glob from 'fast-glob';
import { DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS } from './constant';
import type {
AutoExternal,
Format,
LibConfig,
PkgJson,
Expand All @@ -21,7 +22,6 @@ import type {
Syntax,
} from './types';
import { getDefaultExtension } from './utils/extension';
import { composeAutoExternalConfig } from './utils/external';
import {
calcLongestCommonPath,
color,
Expand Down Expand Up @@ -82,6 +82,60 @@ export async function loadConfig(
return content as RslibConfig;
}

export const composeAutoExternalConfig = (options: {
autoExternal: AutoExternal;
pkgJson?: PkgJson;
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
}): RsbuildConfig => {
const { autoExternal, pkgJson, userExternals } = options;

if (!autoExternal) {
return {};
}

if (!pkgJson) {
logger.warn(
'autoExternal configuration will not be applied due to read package.json failed',
);
return {};
}

const externalOptions = {
dependencies: true,
peerDependencies: true,
devDependencies: false,
...(autoExternal === true ? {} : autoExternal),
};

// User externals configuration has higher priority than autoExternal
// eg: autoExternal: ['react'], user: output: { externals: { react: 'react-1' } }
// Only handle the case where the externals type is object, string / string[] does not need to be processed, other types are too complex.
const userExternalKeys =
userExternals &&
Object.prototype.toString.call(userExternals) === '[object Object]'
? Object.keys(userExternals)
: [];

const externals = (
['dependencies', 'peerDependencies', 'devDependencies'] as const
)
.reduce<string[]>((prev, type) => {
if (externalOptions[type]) {
return pkgJson[type] ? prev.concat(Object.keys(pkgJson[type]!)) : prev;
}
return prev;
}, [])
.filter((name) => !userExternalKeys.includes(name));

return externals.length
? {
output: {
externals: Array.from(new Set(externals)),
},
}
: {};
};

export async function createInternalRsbuildConfig(): Promise<RsbuildConfig> {
return defineRsbuildConfig({
dev: {
Expand Down
58 changes: 0 additions & 58 deletions packages/core/src/utils/external.ts

This file was deleted.

28 changes: 27 additions & 1 deletion packages/core/tests/external.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest';
import { composeAutoExternalConfig } from '../src/utils/external';
import { composeAutoExternalConfig } from '../src/config';

vi.mock('rslog');

Expand Down Expand Up @@ -28,6 +28,32 @@ describe('should composeAutoExternalConfig correctly', () => {
});
});

it('autoExternal will deduplication ', () => {
const result = composeAutoExternalConfig({
autoExternal: true,
pkgJson: {
dependencies: {
foo: '1.0.0',
foo1: '1.0.0',
},
devDependencies: {
bar: '1.0.0',
},
peerDependencies: {
baz: '1.0.0',
foo: '1.0.0',
foo1: '1.0.0',
},
},
});

expect(result).toEqual({
output: {
externals: ['foo', 'foo1', 'baz'],
},
});
});

it('autoExternal is object', () => {
const result = composeAutoExternalConfig({
autoExternal: {
Expand Down

0 comments on commit 6fce2ed

Please sign in to comment.