Skip to content

Commit 7dbe917

Browse files
committed
fix: merge conflict
2 parents 65ce9a4 + f97ff83 commit 7dbe917

File tree

28 files changed

+631
-140
lines changed

28 files changed

+631
-140
lines changed

.changeset/smooth-parents-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rslib/core': patch
3+
---
4+
5+
fix: the moduleIds should be deterministic if the env is production and format is mf

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ coverage/
1111
doc_build/
1212
playwright-report/
1313
tsconfig.tsbuildinfo
14+
tsconfig.esm.tsbuildinfo
15+
tsconfig.cjs.tsbuildinfo
1416
test-temp-*
1517
test-results
1618

packages/core/src/cli/mf.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ function changeEnvToDev(rsbuildConfig: RsbuildConfig) {
4444
rspack: {
4545
optimization: {
4646
nodeEnv: 'development',
47+
moduleIds: 'named',
4748
},
4849
},
4950
},

packages/core/src/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,9 +597,9 @@ const composeFormatConfig = ({
597597
},
598598
// can not set nodeEnv to false, because mf format should build shared module.
599599
// If nodeEnv is false, the process.env.NODE_ENV in third-party packages's will not be replaced
600-
// now we have not provide dev mode for users, so we can always set nodeEnv as 'production'
601600
optimization: {
602601
nodeEnv: 'production',
602+
moduleIds: 'deterministic',
603603
},
604604
},
605605
},

packages/plugin-dts/src/apiExtractor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,7 @@ export async function bundleDts(options: BundleOptions): Promise<void> {
5252
untrimmedFilePath,
5353
},
5454
compiler: {
55-
tsconfigFilePath: tsconfigPath.includes(cwd)
56-
? tsconfigPath
57-
: join(cwd, tsconfigPath),
55+
tsconfigFilePath: tsconfigPath,
5856
},
5957
projectFolder: cwd,
6058
};

packages/plugin-dts/src/dts.ts

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ import {
1010
} from 'node:path';
1111
import { logger } from '@rsbuild/core';
1212
import color from 'picocolors';
13-
import ts from 'typescript';
1413
import type { DtsGenOptions } from './index';
1514
import { emitDts } from './tsc';
16-
import {
17-
calcLongestCommonPath,
18-
ensureTempDeclarationDir,
19-
loadTsconfig,
20-
} from './utils';
15+
import { calcLongestCommonPath, ensureTempDeclarationDir } from './utils';
2116

2217
const isObject = (obj: unknown): obj is Record<string, any> =>
2318
Object.prototype.toString.call(obj) === '[object Object]';
@@ -113,9 +108,10 @@ export const calcBundledPackages = (options: {
113108
export async function generateDts(data: DtsGenOptions): Promise<void> {
114109
const {
115110
bundle,
116-
distPath,
111+
dtsEmitPath,
117112
dtsEntry,
118113
tsconfigPath,
114+
tsConfigResult,
119115
name,
120116
cwd,
121117
build,
@@ -125,33 +121,25 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
125121
userExternals,
126122
banner,
127123
footer,
128-
rootDistPath,
129124
} = data;
130125
logger.start(`Generating DTS... ${color.gray(`(${name})`)}`);
131-
const configPath = ts.findConfigFile(cwd, ts.sys.fileExists, tsconfigPath);
132-
if (!configPath) {
133-
logger.error(`tsconfig.json not found in ${cwd}`);
134-
throw new Error();
135-
}
136-
const { options: rawCompilerOptions, fileNames } = loadTsconfig(configPath);
126+
127+
const { options: rawCompilerOptions, fileNames } = tsConfigResult;
137128

138129
// The longest common path of all non-declaration input files.
139130
// If composite is set, the default is instead the directory containing the tsconfig.json file.
140131
// see https://www.typescriptlang.org/tsconfig/#rootDir
141132
const rootDir =
142133
rawCompilerOptions.rootDir ??
143134
(rawCompilerOptions.composite
144-
? dirname(configPath)
135+
? dirname(tsconfigPath)
145136
: await calcLongestCommonPath(
146137
fileNames.filter((fileName) => !/\.d\.(ts|mts|cts)$/.test(fileName)),
147138
)) ??
148-
dirname(configPath);
149-
150-
const dtsEmitPath =
151-
distPath ?? rawCompilerOptions.declarationDir ?? rootDistPath;
139+
dirname(tsconfigPath);
152140

153141
const resolvedDtsEmitPath = normalize(
154-
resolve(dirname(configPath), dtsEmitPath),
142+
resolve(dirname(tsconfigPath), dtsEmitPath),
155143
);
156144

157145
if (build) {
@@ -173,13 +161,15 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
173161
: 'declarationDir';
174162
throw Error(
175163
`Please set ${info}: "${dtsEmitPath}" in ${color.underline(
176-
configPath,
164+
tsconfigPath,
177165
)} to keep it same as "dts.distPath" or "output.distPath.root" field in lib config.`,
178166
);
179167
}
180168
}
181169

182-
const declarationDir = bundle ? ensureTempDeclarationDir(cwd) : dtsEmitPath;
170+
const declarationDir = bundle
171+
? ensureTempDeclarationDir(cwd, name)
172+
: dtsEmitPath;
183173

184174
const { name: entryName, path: entryPath } = dtsEntry;
185175
let entry = '';
@@ -232,7 +222,8 @@ export async function generateDts(data: DtsGenOptions): Promise<void> {
232222
{
233223
name,
234224
cwd,
235-
configPath,
225+
configPath: tsconfigPath,
226+
tsConfigResult,
236227
declarationDir,
237228
dtsExtension,
238229
banner,

packages/plugin-dts/src/index.ts

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { type ChildProcess, fork } from 'node:child_process';
22
import { dirname, extname, join } from 'node:path';
33
import { fileURLToPath } from 'node:url';
44
import { type RsbuildConfig, type RsbuildPlugin, logger } from '@rsbuild/core';
5-
import { processSourceEntry } from './utils';
5+
import ts from 'typescript';
6+
import {
7+
cleanDtsFiles,
8+
cleanTsBuildInfoFile,
9+
clearTempDeclarationDir,
10+
loadTsconfig,
11+
processSourceEntry,
12+
} from './utils';
613

714
const __filename = fileURLToPath(import.meta.url);
815
const __dirname = dirname(__filename);
@@ -34,9 +41,10 @@ export type DtsGenOptions = PluginDtsOptions & {
3441
cwd: string;
3542
isWatch: boolean;
3643
dtsEntry: DtsEntry;
37-
rootDistPath: string;
44+
dtsEmitPath: string;
3845
build?: boolean;
39-
tsconfigPath?: string;
46+
tsconfigPath: string;
47+
tsConfigResult: ts.ParsedCommandLine;
4048
userExternals?: NonNullable<RsbuildConfig['output']>['externals'];
4149
};
4250

@@ -63,35 +71,69 @@ export const pluginDts = (options: PluginDtsOptions = {}): RsbuildPlugin => ({
6371
let childProcesses: ChildProcess[] = [];
6472

6573
api.onBeforeEnvironmentCompile(
66-
({ isWatch, isFirstCompile, environment }) => {
74+
async ({ isWatch, isFirstCompile, environment }) => {
6775
if (!isFirstCompile) {
6876
return;
6977
}
7078

7179
const { config } = environment;
7280

81+
// TODO: @microsoft/api-extractor only support single entry to bundle DTS
82+
// use first element of Record<string, string> type entry config
83+
const dtsEntry = processSourceEntry(
84+
options.bundle!,
85+
config.source?.entry,
86+
);
87+
88+
const cwd = api.context.rootPath;
89+
const tsconfigPath = ts.findConfigFile(
90+
cwd,
91+
ts.sys.fileExists,
92+
config.source.tsconfigPath,
93+
);
94+
95+
if (!tsconfigPath) {
96+
logger.error(`tsconfig.json not found in ${cwd}`);
97+
throw new Error();
98+
}
99+
100+
const tsConfigResult = loadTsconfig(tsconfigPath);
101+
const dtsEmitPath =
102+
options.distPath ??
103+
tsConfigResult.options.declarationDir ??
104+
config.output?.distPath?.root;
105+
73106
const jsExtension = extname(__filename);
74107
const childProcess = fork(join(__dirname, `./dts${jsExtension}`), [], {
75108
stdio: 'inherit',
76109
});
77110

78111
childProcesses.push(childProcess);
79112

80-
// TODO: @microsoft/api-extractor only support single entry to bundle DTS
81-
// use first element of Record<string, string> type entry config
82-
const dtsEntry = processSourceEntry(
83-
options.bundle!,
84-
config.source?.entry,
85-
);
113+
const { cleanDistPath } = config.output;
114+
115+
// clean dts files
116+
if (cleanDistPath !== false) {
117+
await cleanDtsFiles(dtsEmitPath);
118+
}
119+
120+
// clean .rslib temp folder
121+
if (options.bundle) {
122+
await clearTempDeclarationDir(cwd);
123+
}
124+
125+
// clean tsbuildinfo file
126+
await cleanTsBuildInfoFile(tsconfigPath, tsConfigResult);
86127

87128
const dtsGenOptions: DtsGenOptions = {
88129
...options,
89130
dtsEntry,
131+
dtsEmitPath,
90132
userExternals: config.output.externals,
91-
rootDistPath: config.output?.distPath?.root,
92-
tsconfigPath: config.source.tsconfigPath,
133+
tsconfigPath,
134+
tsConfigResult,
93135
name: environment.name,
94-
cwd: api.context.rootPath,
136+
cwd,
95137
isWatch,
96138
};
97139

packages/plugin-dts/src/tsc.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { logger } from '@rsbuild/core';
22
import color from 'picocolors';
33
import ts from 'typescript';
4-
import {
5-
getFileLoc,
6-
getTimeCost,
7-
loadTsconfig,
8-
processDtsFiles,
9-
} from './utils';
4+
import { getFileLoc, getTimeCost, processDtsFiles } from './utils';
105

116
export type EmitDtsOptions = {
127
name: string;
138
cwd: string;
149
configPath: string;
10+
tsConfigResult: ts.ParsedCommandLine;
1511
declarationDir: string;
1612
dtsExtension: string;
1713
banner?: string;
@@ -63,14 +59,20 @@ export async function emitDts(
6359
build = false,
6460
): Promise<void> {
6561
const start = Date.now();
66-
const { configPath, declarationDir, name, dtsExtension, banner, footer } =
67-
options;
68-
const configFileParseResult = loadTsconfig(configPath);
62+
const {
63+
configPath,
64+
tsConfigResult,
65+
declarationDir,
66+
name,
67+
dtsExtension,
68+
banner,
69+
footer,
70+
} = options;
6971
const {
7072
options: rawCompilerOptions,
7173
fileNames,
7274
projectReferences,
73-
} = configFileParseResult;
75+
} = tsConfigResult;
7476

7577
const compilerOptions = {
7678
...rawCompilerOptions,
@@ -160,9 +162,8 @@ export async function emitDts(
160162
options: compilerOptions,
161163
projectReferences,
162164
host,
163-
configFileParsingDiagnostics: ts.getConfigFileParsingDiagnostics(
164-
configFileParseResult,
165-
),
165+
configFileParsingDiagnostics:
166+
ts.getConfigFileParsingDiagnostics(tsConfigResult),
166167
});
167168

168169
const emitResult = program.emit();
@@ -190,9 +191,8 @@ export async function emitDts(
190191
const program = ts.createIncrementalProgram({
191192
rootNames: fileNames,
192193
options: compilerOptions,
193-
configFileParsingDiagnostics: ts.getConfigFileParsingDiagnostics(
194-
configFileParseResult,
195-
),
194+
configFileParsingDiagnostics:
195+
ts.getConfigFileParsingDiagnostics(tsConfigResult),
196196
projectReferences,
197197
host,
198198
createProgram,

0 commit comments

Comments
 (0)