Skip to content

Commit

Permalink
chore: bump rsbuild to support environment plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Jul 23, 2024
1 parent 78b2401 commit 7c1a28b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 26 deletions.
2 changes: 1 addition & 1 deletion e2e/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"devDependencies": {
"@playwright/test": "1.43.1",
"@rsbuild/core": "1.0.1-beta.3",
"@rsbuild/core": "0.0.0-next-20240723074951",
"@rslib/core": "workspace:*",
"@rslib/tsconfig": "workspace:*",
"@types/fs-extra": "^11.0.4",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"prebundle": "prebundle"
},
"dependencies": {
"@rsbuild/core": "1.0.1-beta.3",
"@rsbuild/core": "0.0.0-next-20240723074951",
"rsbuild-plugin-dts": "workspace:*"
},
"devDependencies": {
Expand Down
5 changes: 0 additions & 5 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,6 @@ export async function initRsbuild(rslibConfig: RslibConfig) {
return createRsbuild({
rsbuildConfig: {
environments: rsbuildConfigObject,
// TODO: temporarily inject the plugin externally
...getDefaultDtsConfig(
rslibConfig.lib[0]?.dts,
rslibConfig.lib[0]?.bundle,
),
},
});
}
2 changes: 1 addition & 1 deletion packages/plugin-dts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"devDependencies": {
"@microsoft/api-extractor": "^7.47.2",
"@rsbuild/core": "1.0.1-beta.3",
"@rsbuild/core": "0.0.0-next-20240723074951",
"@rslib/tsconfig": "workspace:*",
"typescript": "^5.5.3"
},
Expand Down
12 changes: 7 additions & 5 deletions packages/plugin-dts/src/apiExtractor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
type ExtractorResult,
} from '@microsoft/api-extractor';
import { logger } from '@rsbuild/core';
import { ensureTempDeclarationDir } from './utils';

export function bundleDts(outDir: string, tsconfigPath = 'tsconfig.json') {
const cwd = process.cwd();
export function bundleDts(
cwd: string,
outDir: string,
entry = 'index.d.ts',
tsconfigPath = 'tsconfig.json',
) {
const internalConfig = {
// TODO: use source.entry.main
mainEntryPointFilePath: join(ensureTempDeclarationDir(), 'index.d.ts'),
mainEntryPointFilePath: entry,
// TODO: use !externals
// bundledPackages: [],
dtsRollup: {
Expand Down
10 changes: 7 additions & 3 deletions packages/plugin-dts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { join } from 'node:path';
import type { RsbuildPlugin } from '@rsbuild/core';
import { bundleDts } from './apiExtractor';
import { emitDts } from './tsc';
Expand All @@ -24,11 +25,14 @@ export const pluginDts = (
setup(api) {
const { tsconfigPath } = options;

api.onAfterBuild(async () => {
const { outDir } = emitDts(options);
api.onAfterBuild(async ({ environments }) => {
const { outDir, cwd, dtsFileMap } = emitDts(options);

if (options.bundle === true) {
bundleDts(outDir, tsconfigPath);
const entry = environments.esm?.config.source.entry?.main as string;
const entryDtsFile = dtsFileMap.get(join(cwd, entry));

bundleDts(cwd, outDir, entryDtsFile, tsconfigPath);
}
});
},
Expand Down
34 changes: 33 additions & 1 deletion packages/plugin-dts/src/tsc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ensureTempDeclarationDir, loadTsconfig } from './utils';

export function emitDts(options: pluginDtsOptions): {
outDir: string;
cwd: string;
dtsFileMap: Map<string, string>;
} {
const { tsconfigPath, distPath, bundle } = options;
const cwd = process.cwd();
Expand All @@ -30,6 +32,8 @@ export function emitDts(options: pluginDtsOptions): {
emitDeclarationOnly: true,
};

const currentDirectory = ts.sys.getCurrentDirectory();

const host: ts.CompilerHost = ts.createCompilerHost(compilerOptions);

const program: ts.Program = ts.createProgram(
Expand All @@ -38,7 +42,33 @@ export function emitDts(options: pluginDtsOptions): {
host,
);

const emitResult = program.emit();
const dtsFileMap = new Map<string, string>();

const writeFile: ts.WriteFileCallback = (
fileName,
text,
writeByteOrderMark,
onError,
sourceFiles,
data,
) => {
const sourceFileName = sourceFiles?.[0]?.fileName;

if (sourceFileName) {
dtsFileMap.set(sourceFileName, fileName);
}

return host.writeFile(
fileName,
text,
writeByteOrderMark,
onError,
sourceFiles,
data,
);
};

const emitResult = program.emit(undefined, writeFile, undefined, true);

const allDiagnostics = ts
.getPreEmitDiagnostics(program)
Expand Down Expand Up @@ -79,5 +109,7 @@ export function emitDts(options: pluginDtsOptions): {

return {
outDir: outDir || './dist',
cwd: currentDirectory,
dtsFileMap,
};
}
20 changes: 11 additions & 9 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 7c1a28b

Please sign in to comment.