Skip to content

Commit

Permalink
feat(source-build): support for conditional exports
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jun 24, 2024
1 parent cb01b9b commit bbd3c28
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 20 deletions.
3 changes: 2 additions & 1 deletion e2e/cases/source-build/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
}
},
"dependencies": {
"@e2e/source-build-utils": "workspace:*"
"@e2e/source-build-utils": "workspace:*",
"@e2e/source-build-utils2": "workspace:*"
}
}
3 changes: 2 additions & 1 deletion e2e/cases/source-build/components/src/card/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { strAdd } from '@e2e/source-build-utils';
import { toLowerCase } from '@e2e/source-build-utils2';
import './index.less';

export interface CardProps {
Expand All @@ -10,7 +11,7 @@ export const Card = (props: CardProps) => {
const { title, content = '' } = props;
return (
<div className="card-comp">
<h2>Card Comp Title: {title}</h2>
<h2>Card Comp Title: {toLowerCase(title)}</h2>
<article>{strAdd('Card Comp Content:', content)}</article>
</div>
);
Expand Down
3 changes: 3 additions & 0 deletions e2e/cases/source-build/components/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
"references": [
{
"path": "../utils"
},
{
"path": "../utils2"
}
],
"include": ["src"]
Expand Down
2 changes: 1 addition & 1 deletion e2e/cases/source-build/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rspackOnlyTest(

const locator = page.locator('#root');
await expect(locator).toHaveText(
'Card Comp Title: AppCARD COMP CONTENT:hello world',
'Card Comp Title: appCARD COMP CONTENT:hello world',
);

await rsbuild.close();
Expand Down
10 changes: 10 additions & 0 deletions e2e/cases/source-build/utils2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@e2e/source-build-utils2",
"private": true,
"version": "1.0.0",
"types": "./dist/types/index.d.ts",
"exports": {
"types": "./dist/types/index.d.ts",
"source": "./src/index.ts"
}
}
2 changes: 2 additions & 0 deletions e2e/cases/source-build/utils2/src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './toUpperCase';
export * from './toLowerCase';
1 change: 1 addition & 0 deletions e2e/cases/source-build/utils2/src/common/toLowerCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const toLowerCase = (s: string) => s.toLowerCase();
1 change: 1 addition & 0 deletions e2e/cases/source-build/utils2/src/common/toUpperCase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const toUpperCase = (s: string) => s.toUpperCase();
3 changes: 3 additions & 0 deletions e2e/cases/source-build/utils2/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { toLowerCase } from '@common/index';

export { toLowerCase };
18 changes: 18 additions & 0 deletions e2e/cases/source-build/utils2/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": "@rsbuild/config/tsconfig",
"compilerOptions": {
"composite": true,
"declaration": true,
"declarationDir": "./dist/types",
"declarationMap": true,
"jsx": "react-jsx",
"baseUrl": "./",
"paths": {
"@/*": ["./src/*"],
"@common/*": ["./src/common/*"]
},
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}
13 changes: 7 additions & 6 deletions packages/plugin-source-build/src/project-utils/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ function hasExportsSourceField(
exportsConfig: ExportsConfig,
sourceField: string,
) {
return Object.values(exportsConfig).some(
(moduleRules) =>
typeof moduleRules === 'object' &&
typeof moduleRules[sourceField] === 'string',
return (
typeof exportsConfig[sourceField] === 'string' ||
Object.values(exportsConfig).some(
(moduleRules) =>
typeof moduleRules === 'object' &&
typeof moduleRules[sourceField] === 'string',
)
);
}

export const defaultFilter: FilterFunction = (projects) => projects;

export const filterByField =
(fieldName: string, checkExports?: boolean): FilterFunction =>
(projects: Project[]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import fs from 'node:fs';
import path from 'node:path';
import { getMonorepoBaseData, getMonorepoSubProjects } from '../common';
import type { Project } from '../project/project';
import type { MonorepoAnalyzer } from '../types';
import { readPackageJson } from '../utils';
import { type Filter, defaultFilter } from './filter';
import type { Filter } from './filter';

export type ExtraMonorepoStrategies = Record<string, MonorepoAnalyzer>;

Expand All @@ -23,14 +22,6 @@ async function pathExists(path: string) {
.catch(() => false);
}

const filterProjects = async (projects: Project[], filter?: Filter) => {
if (!filter) {
return defaultFilter(projects);
}

return filter(projects);
};

const getDependentProjects = async (
projectNameOrRootPath: string,
options: GetDependentProjectsOptions,
Expand Down Expand Up @@ -72,7 +63,9 @@ const getDependentProjects = async (
let dependentProjects = currentProject.getDependentProjects(projects, {
recursive,
});
dependentProjects = await filterProjects(dependentProjects, filter);
if (filter) {
dependentProjects = await filter(dependentProjects);
}

return dependentProjects;
};
Expand Down
6 changes: 6 additions & 0 deletions packages/plugin-source-build/src/project/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ export class Project {
#getExportsSourceDirs(exportsConfig: ExportsConfig, sourceField: string) {
const exportsSourceDirs: string[] = [];

if (typeof exportsConfig[sourceField] === 'string') {
exportsSourceDirs.push(
path.normalize(exportsConfig[sourceField] as string),
);
}

for (const moduleRules of Object.values(exportsConfig)) {
if (
typeof moduleRules === 'object' &&
Expand Down
5 changes: 5 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 bbd3c28

Please sign in to comment.