Skip to content

Commit

Permalink
feat: support dts bundleless
Browse files Browse the repository at this point in the history
  • Loading branch information
Timeless0911 committed Jul 22, 2024
1 parent e7bcaf1 commit 32bf58f
Show file tree
Hide file tree
Showing 23 changed files with 343 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ compiled
doc_build

# ignore all JS/TS files, use Biome
**/*[.js,.ts,.jsx,.tsx.json,.json5]
**/*[.js,.ts,.jsx,.tsx]
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
"editor.defaultFormatter": "biomejs.biome"
},
"[json5]": {
"editor.defaultFormatter": "biomejs.biome"
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "biomejs.biome"
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[jsonc]": {
"editor.defaultFormatter": "biomejs.biome"
Expand Down
22 changes: 22 additions & 0 deletions e2e/cases/dts/bundle-false/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from '#shared';

export default defineConfig({
lib: [
generateBundleEsmConfig(__dirname, {
bundle: false,
dts: {
bundle: false,
},
}),
generateBundleCjsConfig(__dirname, {
bundle: false,
dts: false,
}),
],
source: {
entry: {
main: ['./src/**'],
},
},
});
3 changes: 3 additions & 0 deletions e2e/cases/dts/bundle-false/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './utils/numbers';
export * from './utils/strings';
export * from './sum';
5 changes: 5 additions & 0 deletions e2e/cases/dts/bundle-false/src/sum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { num1, num2, num3 } from './utils/numbers';
import { str1, str2, str3 } from './utils/strings';

export const numSum = num1 + num2 + num3;
export const strSum = str1 + str2 + str3;
3 changes: 3 additions & 0 deletions e2e/cases/dts/bundle-false/src/utils/numbers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const num1 = 1;
export const num2 = 2;
export const num3 = 3;
3 changes: 3 additions & 0 deletions e2e/cases/dts/bundle-false/src/utils/strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const str1 = 'str1';
export const str2 = 'str2';
export const str3 = 'str3';
7 changes: 7 additions & 0 deletions e2e/cases/dts/bundle-false/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "@rslib/tsconfig/base",
"compilerOptions": {
"baseUrl": "./"
},
"include": ["src"]
}
5 changes: 5 additions & 0 deletions e2e/cases/dts/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { join } from 'node:path';
import { expect, test } from 'vitest';
import { buildAndGetJsResults } from '#shared';

test.todo('dts when bundle: false', async () => {});
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.0-alpha.9",
"@rsbuild/core": "1.0.1-beta.3",
"@rslib/core": "workspace:*",
"@rslib/tsconfig": "workspace:*",
"@types/fs-extra": "^11.0.4",
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
"pre-commit": "npx nano-staged"
},
"nano-staged": {
"*.{md,mdx,css,less,scss}": "prettier --write",
"*.{js,jsx,ts,tsx,mjs,cjs,json,jsonc,json5}": ["biome check --write"],
"*.{md,mdx,css,less,scss,json,jsonc,json5}": "prettier --write",
"*.{js,jsx,ts,tsx,mjs,cjs}": [
"biome check --write"
],
"package.json": "pnpm run check-dependency-version"
},
"devDependencies": {
Expand Down
7 changes: 6 additions & 1 deletion packages/core/modern.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { defineConfig, moduleTools } from '@modern-js/module-tools';
import prebundleConfig from './prebundle.config.mjs';

const externals = ['@rsbuild/core', /[\\/]compiled[\\/]/, /node:/];
const externals = [
'@rsbuild/core',
/[\\/]compiled[\\/]/,
/node:/,
'typescript',
];
const define = {
RSLIB_VERSION: require('./package.json').version,
};
Expand Down
8 changes: 6 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@
"prebundle": "prebundle"
},
"dependencies": {
"@rsbuild/core": "1.0.0-alpha.9"
"@rsbuild/core": "1.0.1-beta.3",
"rsbuild-plugin-dts": "workspace:*"
},
"devDependencies": {
"@rslib/tsconfig": "workspace:*",
"@types/fs-extra": "^11.0.4",
"fast-glob": "^3.3.2",
"commander": "^12.1.0",
"fs-extra": "^11.2.0",
"fast-glob": "^3.3.2",
"picocolors": "1.0.1",
"prebundle": "1.1.0",
"rslog": "^1.2.2",
"typescript": "^5.5.3"
},
"peerDependencies": {
"typescript": "^5"
},
"engines": {
"node": ">=16.0.0"
},
Expand Down
23 changes: 22 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import fs from 'node:fs';

import path, { dirname, isAbsolute, join } from 'node:path';
import {
type RsbuildConfig,
Expand All @@ -8,8 +7,10 @@ import {
mergeRsbuildConfig,
} from '@rsbuild/core';
import glob from 'fast-glob';
import { pluginDts } from 'rsbuild-plugin-dts';
import { DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS } from './constant';
import type {
Dts,
Format,
LibConfig,
RslibConfig,
Expand Down Expand Up @@ -291,6 +292,19 @@ const getBundleConfig = (bundle = true): RsbuildConfig => {
};
};

const getDefaultDtsConfig = (dts?: Dts, bundle = true): RsbuildConfig => {
if (dts === false || dts === undefined) return {};

return {
plugins: [
pluginDts({
bundle: dts?.bundle ?? bundle,
distPath: dts?.distPath ?? './dist',
}),
],
};
};

export function convertLibConfigToRsbuildConfig(
libConfig: LibConfig,
configPath: string,
Expand All @@ -305,12 +319,14 @@ export function convertLibConfigToRsbuildConfig(
);
const syntaxConfig = getDefaultSyntaxConfig(libConfig.output?.syntax);
const bundleConfig = getBundleConfig(libConfig.bundle);
const dtsConfig = getDefaultDtsConfig(libConfig.dts, libConfig.bundle);

return mergeRsbuildConfig(
formatConfig,
autoExtensionConfig,
syntaxConfig,
bundleConfig,
dtsConfig,
);
}

Expand Down Expand Up @@ -413,6 +429,11 @@ 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,
),
},
});
}
9 changes: 9 additions & 0 deletions packages/core/src/types/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export type Syntax =
// Support inline browserslist query, like defined in package.json
| string[];

export type Dts =
| {
bundle: boolean;
distPath?: string;
tsconfigPath?: string;
}
| false;

export interface LibConfig extends RsbuildConfig {
bundle?: boolean;
format?: Format;
Expand All @@ -31,6 +39,7 @@ export interface LibConfig extends RsbuildConfig {
/** Support esX and browserslist query */
syntax?: Syntax;
};
dts?: Dts;
}

export interface RslibConfig extends RsbuildConfig {
Expand Down
1 change: 0 additions & 1 deletion packages/core/tests/helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import { calcLongestCommonPath } from '../src/utils/helper';

Expand Down
7 changes: 6 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@
}
},
"include": ["src"],
"exclude": ["**/node_modules"]
"exclude": ["**/node_modules"],
"references": [
{
"path": "../plugin-dts"
}
]
}
36 changes: 36 additions & 0 deletions packages/plugin-dts/modern.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { defineConfig, moduleTools } from '@modern-js/module-tools';

const externals = ['@rsbuild/core', /[\\/]compiled[\\/]/, /node:/];
const define = {
RSLIB_VERSION: require('../core/package.json').version,
};

export default defineConfig({
plugins: [moduleTools()],
buildConfig: [
{
format: 'cjs',
target: 'es2020',
buildType: 'bundle',
autoExtension: true,
externals,
dts: false,
define,
},
{
format: 'esm',
target: 'es2020',
buildType: 'bundle',
autoExtension: true,
externals,
dts: false,
define,
},
{
buildType: 'bundleless',
dts: {
only: true,
},
},
],
});
47 changes: 47 additions & 0 deletions packages/plugin-dts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "rsbuild-plugin-dts",
"version": "0.0.0",
"description": "Dts plugin for Rsbuild",
"homepage": "https://rslib.dev",
"bugs": {
"url": "https://github.com/web-infra-dev/rslib/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/web-infra-dev/rslib",
"directory": "packages/plugin-dts"
},
"license": "MIT",
"type": "module",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"main": "./dist/index.cjs",
"types": "./dist/index.d.ts",
"files": ["dist"],
"scripts": {
"build": "modern build",
"dev": "modern build --watch"
},
"devDependencies": {
"@rsbuild/core": "1.0.1-beta.3",
"@rslib/tsconfig": "workspace:*",
"typescript": "^5.5.3"
},
"peerDependencies": {
"typescript": "^5",
"@rsbuild/core": "workspace:^1.0.1-beta.0"
},
"engines": {
"node": ">=16.0.0"
},
"publishConfig": {
"access": "public",
"provenance": true,
"registry": "https://registry.npmjs.org/"
}
}
Loading

0 comments on commit 32bf58f

Please sign in to comment.