@rsbuild/plugin-source-build 1.0.0 → 1.0.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,23 @@
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ import type { Project } from './project';
3
+ import { type ExtraMonorepoStrategies } from './project-utils';
4
+ export declare const PLUGIN_SOURCE_BUILD_NAME = "rsbuild:source-build";
5
+ export declare const getSourceInclude: (options: {
6
+ projects: Project[];
7
+ sourceField: string;
8
+ }) => Promise<string[]>;
9
+ export interface PluginSourceBuildOptions {
10
+ /**
11
+ * Used to configure the resolve field of the source code files.
12
+ * @default 'source''
13
+ */
14
+ sourceField?: string;
15
+ /**
16
+ * Whether to read source code or output code first.
17
+ * @default 'source'
18
+ */
19
+ resolvePriority?: 'source' | 'output';
20
+ projectName?: string;
21
+ extraMonorepoStrategies?: ExtraMonorepoStrategies;
22
+ }
23
+ export declare function pluginSourceBuild(options?: PluginSourceBuildOptions): RsbuildPlugin;
@@ -0,0 +1,4 @@
1
+ import type { Project } from '../project';
2
+ export type Filter = FilterFunction;
3
+ export type FilterFunction = (projects: Project[]) => Project[] | Promise<Project[]>;
4
+ export declare const filterByField: (fieldName: string, checkExports?: boolean) => FilterFunction;
@@ -0,0 +1,12 @@
1
+ import type { Project } from '../project';
2
+ import type { MonorepoAnalyzer } from '../types';
3
+ import type { Filter } from './filter';
4
+ export type ExtraMonorepoStrategies = Record<string, MonorepoAnalyzer>;
5
+ export interface GetDependentProjectsOptions {
6
+ cwd?: string;
7
+ recursive?: boolean;
8
+ filter?: Filter;
9
+ extraMonorepoStrategies?: ExtraMonorepoStrategies;
10
+ }
11
+ declare const getDependentProjects: (projectNameOrRootPath: string, options: GetDependentProjectsOptions) => Promise<Project[]>;
12
+ export { getDependentProjects };
@@ -0,0 +1,2 @@
1
+ export * from './getDependentProjects';
2
+ export * from './filter';
@@ -0,0 +1,18 @@
1
+ import type { INodePackageJson } from './types/packageJson';
2
+ export declare class Project {
3
+ #private;
4
+ name: string;
5
+ dir: string;
6
+ metaData: INodePackageJson;
7
+ constructor(name: string, dir: string);
8
+ init(): Promise<void>;
9
+ getMetaData(): INodePackageJson;
10
+ getDependentProjects(monorepoProjects: Project[], options?: {
11
+ recursive?: boolean;
12
+ }): Project[];
13
+ getDirectDependentProjects(allProjectMap: Map<string, Project>): Project[];
14
+ getSourceEntryPaths(options?: {
15
+ field?: string;
16
+ exports?: boolean;
17
+ }): string[];
18
+ }
@@ -0,0 +1,16 @@
1
+ import type { GetProjectsFunc } from '../common/getProjects';
2
+ import type { IsMonorepoFn } from '../common/isMonorepo';
3
+ export * from './packageJson';
4
+ export * from './rushJson';
5
+ export interface MonorepoAnalyzer {
6
+ check: IsMonorepoFn;
7
+ getProjects: GetProjectsFunc;
8
+ }
9
+ export interface IPnpmWorkSpace {
10
+ packages: string[];
11
+ }
12
+ export type TsConfig = {
13
+ references?: Array<{
14
+ path?: string;
15
+ }>;
16
+ };
@@ -0,0 +1,152 @@
1
+ /**
2
+ * The following code is modified based on
3
+ * https://github.com/microsoft/rushstack/blob/main/libraries/node-core-library/src/IPackageJson.ts
4
+ *
5
+ * MIT Licensed
6
+ * Copyright (c) Microsoft Corporation
7
+ * https://github.com/microsoft/rushstack/blob/main/LICENSE
8
+ */
9
+ /**
10
+ * This interface is part of the {@link IPackageJson} file format. It is used for the
11
+ * "dependencies", "optionalDependencies", and "devDependencies" fields.
12
+ * @public
13
+ */
14
+ export interface IPackageJsonDependencyTable {
15
+ /**
16
+ * The key is the name of a dependency. The value is a Semantic Versioning (SemVer)
17
+ * range specifier.
18
+ */
19
+ [dependencyName: string]: string;
20
+ }
21
+ export interface IPackageJsonRepository {
22
+ /**
23
+ * The source control type for the repository that hosts the project. This is typically "git".
24
+ */
25
+ type: string;
26
+ /**
27
+ * The URL of the repository that hosts the project.
28
+ */
29
+ url: string;
30
+ /**
31
+ * If the project does not exist at the root of the repository, its path is specified here.
32
+ */
33
+ directory?: string;
34
+ }
35
+ /**
36
+ * This interface is part of the {@link IPackageJson} file format. It is used for the
37
+ * "scripts" field.
38
+ * @public
39
+ */
40
+ export interface IPackageJsonScriptTable {
41
+ /**
42
+ * The key is the name of the script hook. The value is the script body which may
43
+ * be a file path or shell script command.
44
+ */
45
+ [scriptName: string]: string;
46
+ }
47
+ /**
48
+ * This interface is part of the {@link IPackageJson} file format. It is used for the
49
+ * "peerDependenciesMeta" field.
50
+ * @public
51
+ */
52
+ export interface IPeerDependenciesMetaTable {
53
+ [dependencyName: string]: {
54
+ optional?: boolean;
55
+ };
56
+ }
57
+ export type ExportsModuleRules = string | Record<string, string> | Record<string, string | Record<string, string>>;
58
+ export interface ExportsConfig {
59
+ [modulePath: string]: ExportsModuleRules;
60
+ }
61
+ export interface INodePackageJson {
62
+ /**
63
+ * The name of the package.
64
+ */
65
+ name: string;
66
+ /**
67
+ * A version number conforming to the Semantic Versioning (SemVer) standard.
68
+ */
69
+ version?: string;
70
+ /**
71
+ * Indicates whether this package is allowed to be published or not.
72
+ */
73
+ private?: boolean;
74
+ /**
75
+ * A brief description of the package.
76
+ */
77
+ description?: string;
78
+ /**
79
+ * The URL of the project's repository.
80
+ */
81
+ repository?: string | IPackageJsonRepository;
82
+ /**
83
+ * The URL to the project's web page.
84
+ */
85
+ homepage?: string;
86
+ /**
87
+ * The name of the license.
88
+ */
89
+ license?: string;
90
+ /**
91
+ * The path to the module file that will act as the main entry point.
92
+ */
93
+ main?: string;
94
+ exports?: ExportsConfig;
95
+ /**
96
+ * The path to the TypeScript *.d.ts file describing the module file
97
+ * that will act as the main entry point.
98
+ */
99
+ types?: string;
100
+ /**
101
+ * Alias for `types`
102
+ */
103
+ typings?: string;
104
+ /**
105
+ * The path to the TSDoc metadata file.
106
+ * This is still being standardized: https://github.com/microsoft/tsdoc/issues/7#issuecomment-442271815
107
+ * @beta
108
+ */
109
+ tsdocMetadata?: string;
110
+ /**
111
+ * The main entry point for the package.
112
+ */
113
+ bin?: string;
114
+ /**
115
+ * An array of dependencies that must always be installed for this package.
116
+ */
117
+ dependencies?: IPackageJsonDependencyTable;
118
+ /**
119
+ * An array of optional dependencies that may be installed for this package.
120
+ */
121
+ optionalDependencies?: IPackageJsonDependencyTable;
122
+ /**
123
+ * An array of dependencies that must only be installed for developers who will
124
+ * build this package.
125
+ */
126
+ devDependencies?: IPackageJsonDependencyTable;
127
+ /**
128
+ * An array of dependencies that must be installed by a consumer of this package,
129
+ * but which will not be automatically installed by this package.
130
+ */
131
+ peerDependencies?: IPackageJsonDependencyTable;
132
+ /**
133
+ * An array of metadata about peer dependencies.
134
+ */
135
+ peerDependenciesMeta?: IPeerDependenciesMetaTable;
136
+ /**
137
+ * A table of script hooks that a package manager or build tool may invoke.
138
+ */
139
+ scripts?: IPackageJsonScriptTable;
140
+ /**
141
+ * A table of package version resolutions. This feature is only implemented by the Yarn package manager.
142
+ *
143
+ * @remarks
144
+ * See the {@link https://github.com/yarnpkg/rfcs/blob/master/implemented/0000-selective-versions-resolutions.md
145
+ * | 0000-selective-versions-resolutions.md RFC} for details.
146
+ */
147
+ resolutions?: Record<string, string>;
148
+ }
149
+ export interface IPackageJson extends INodePackageJson {
150
+ /** {@inheritDoc INodePackageJson.version} */
151
+ version: string;
152
+ }
@@ -0,0 +1,7 @@
1
+ export interface RushConfigurationProject {
2
+ readonly packageName: string;
3
+ readonly projectFolder: string;
4
+ }
5
+ export interface IRushConfig {
6
+ projects?: RushConfigurationProject[];
7
+ }
@@ -0,0 +1,4 @@
1
+ import type { INodePackageJson, IRushConfig } from './types';
2
+ export declare const readPackageJson: (pkgJsonFilePath: string) => Promise<INodePackageJson>;
3
+ export declare const readRushJson: (rushJsonFilePath: string) => Promise<IRushConfig>;
4
+ export declare const readJson: <T>(jsonFileAbsPath: string) => Promise<T>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-source-build",
3
- "version": "1.0.0",
3
+ "version": "1.0.1-beta.0",
4
4
  "description": "Source build plugin of Rsbuild",
5
5
  "homepage": "https://rsbuild.dev",
6
6
  "repository": {
@@ -9,31 +9,32 @@
9
9
  "directory": "packages/plugin-source-build"
10
10
  },
11
11
  "license": "MIT",
12
+ "type": "module",
12
13
  "exports": {
13
14
  ".": {
14
15
  "types": "./dist/index.d.ts",
15
- "import": "./dist/index.mjs",
16
- "default": "./dist/index.js"
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs"
17
18
  }
18
19
  },
19
- "main": "./dist/index.js",
20
+ "main": "./dist/index.cjs",
20
21
  "types": "./dist/index.d.ts",
21
22
  "files": [
22
23
  "dist"
23
24
  ],
24
25
  "dependencies": {
25
- "@rsbuild/monorepo-utils": "1.0.0",
26
- "@rsbuild/shared": "1.0.0"
26
+ "fast-glob": "^3.3.2",
27
+ "json5": "^2.2.3"
27
28
  },
28
29
  "devDependencies": {
29
- "@babel/core": "^7.23.2",
30
- "typescript": "^5.3.0",
31
- "@rsbuild/core": "1.0.0",
32
- "@rsbuild/test-helper": "1.0.0",
33
- "@rsbuild/webpack": "1.0.0"
30
+ "typescript": "^5.5.2",
31
+ "yaml": "^2.4.5",
32
+ "@rsbuild/core": "1.0.1-beta.0",
33
+ "@rsbuild/plugin-babel": "1.0.1-beta.0",
34
+ "@scripts/test-helper": "1.0.1-beta.0"
34
35
  },
35
36
  "peerDependencies": {
36
- "@rsbuild/core": "^1.0.0"
37
+ "@rsbuild/core": "^1.0.1-beta.0"
37
38
  },
38
39
  "publishConfig": {
39
40
  "access": "public",
package/dist/index.mjs DELETED
@@ -1,80 +0,0 @@
1
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.40.0_typescript@5.3.2/node_modules/@modern-js/module-tools/shims/esm.js
2
- import { fileURLToPath } from "url";
3
- import path from "path";
4
-
5
- // ../../scripts/require_shims.js
6
- import { createRequire } from "module";
7
- global.require = createRequire(import.meta.url);
8
-
9
- // src/index.ts
10
- import fs from "fs";
11
- import path2 from "path";
12
- import { TS_CONFIG_FILE } from "@rsbuild/shared";
13
- import {
14
- filterByField,
15
- getDependentProjects
16
- } from "@rsbuild/monorepo-utils";
17
- var pluginName = "rsbuild:source-build";
18
- var getSourceInclude = async (options) => {
19
- const { projects, sourceField } = options;
20
- const includes = [];
21
- for (const project of projects) {
22
- includes.push(...project.getSourceEntryPaths({ field: sourceField }));
23
- }
24
- return includes;
25
- };
26
- function pluginSourceBuild(options) {
27
- const {
28
- projectName,
29
- sourceField = "source",
30
- extraMonorepoStrategies
31
- } = options ?? {};
32
- return {
33
- name: pluginName,
34
- setup(api) {
35
- const projectRootPath = api.context.rootPath;
36
- let projects = [];
37
- api.modifyRsbuildConfig(async (config) => {
38
- projects = await getDependentProjects(projectName || projectRootPath, {
39
- cwd: projectRootPath,
40
- recursive: true,
41
- filter: filterByField(sourceField),
42
- extraMonorepoStrategies
43
- });
44
- const includes = await getSourceInclude({
45
- projects,
46
- sourceField
47
- });
48
- config.source = config.source ?? {};
49
- config.source.include = [...config.source.include ?? [], ...includes];
50
- });
51
- if (api.context.bundlerType === "webpack") {
52
- api.modifyWebpackChain((chain, { CHAIN_ID }) => {
53
- const {
54
- tools: { tsLoader }
55
- } = api.getNormalizedConfig();
56
- const useTsLoader = Boolean(tsLoader);
57
- chain.module.rule(useTsLoader ? CHAIN_ID.RULE.TS : CHAIN_ID.RULE.JS).resolve.mainFields.merge([sourceField, "..."]);
58
- chain.module.rule(useTsLoader ? CHAIN_ID.RULE.TS : CHAIN_ID.RULE.JS).resolve.merge({
59
- conditionNames: ["...", sourceField]
60
- });
61
- const { TS_CONFIG_PATHS } = CHAIN_ID.RESOLVE_PLUGIN;
62
- if (chain.resolve.plugins.has(TS_CONFIG_PATHS)) {
63
- chain.resolve.plugin(TS_CONFIG_PATHS).tap((options2) => {
64
- const references = projects.map((project) => path2.join(project.dir, TS_CONFIG_FILE)).filter((filePath) => fs.existsSync(filePath));
65
- return options2.map((option) => ({
66
- ...option,
67
- references
68
- }));
69
- });
70
- }
71
- });
72
- }
73
- }
74
- };
75
- }
76
- export {
77
- getSourceInclude,
78
- pluginName,
79
- pluginSourceBuild
80
- };