Skip to content

Commit 0aa82fc

Browse files
committed
done
1 parent f421aba commit 0aa82fc

File tree

9 files changed

+165
-20
lines changed

9 files changed

+165
-20
lines changed

rollup.config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { getEntryFiles } from "./src/rollup/entry";
2-
import { rollupNode } from "./src/rollup/node";
1+
import { rollupOptions } from "./src/rollup";
32

4-
export default Promise.all([
5-
//
6-
rollupNode({ input: getEntryFiles({ patterns: 2 }) }),
7-
]);
3+
export default rollupOptions({
4+
node: true,
5+
dts: true,
6+
inputPatterns: 2,
7+
});

src/rollup/bundle.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from "../util/resolvable";
1414
import { genOutputOptions, GenOutputOptions } from "./output";
1515
import { inferSingleEntry } from "./entry";
16+
import { joinPath } from "../util/path";
1617

1718
const COMMON_OUTPUT_OPTIONS: OutputOptions = {
1819
sourcemap: true,
@@ -73,6 +74,8 @@ export interface RollupBundleOptions
7374
extends Partial<ResolvableDict<RollupBundleOutputConfig>> {
7475
input?: Resolvable<string | undefined>;
7576
output?: Resolvable<GenOutputOptions, [RollupBundleOutputConfig]>;
77+
inputBaseDir?: string;
78+
outputBaseDir?: string;
7679
}
7780

7881
export function genBundleOutputOptions(
@@ -82,9 +85,11 @@ export function genBundleOutputOptions(
8285
}
8386

8487
export async function rollupBundle({
85-
input = inferSingleEntry,
88+
inputBaseDir = "src",
89+
outputBaseDir = "dist",
90+
input,
8691
output: _output = genBundleOutputOptions,
87-
outputRootDir = "dist/bundle",
92+
outputRootDir = "bundle",
8893
min = false,
8994
globalNamespace = inferGlobalNamespace,
9095
}: RollupBundleOptions = {}): Promise<RollupOptions> {
@@ -95,12 +100,15 @@ export async function rollupBundle({
95100
globalNamespace,
96101
});
97102

98-
const genOutput = await resolve(_output, conf);
103+
const genOutput = await resolve(_output, {
104+
...conf,
105+
outputRootDir: joinPath(outputBaseDir, conf.outputRootDir),
106+
});
99107

100108
const output = genOutputOptions(genOutput, COMMON_OUTPUT_OPTIONS);
101109

102110
return {
103-
input: inputFile ?? (await inferSingleEntry()),
111+
input: inputFile ?? (await inferSingleEntry({ baseDir: inputBaseDir })),
104112
output,
105113
plugins: [
106114
//

src/rollup/dts.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import type { InputOption, RollupOptions } from "rollup";
2+
import { chunkFileNames } from "../util/common";
3+
import { getEntryFiles } from "./entry";
4+
import { Resolvable, resolve } from "../util/resolvable";
5+
6+
export interface RollupDtsOptions {
7+
inputBaseDir?: string;
8+
outputBaseDir?: string;
9+
input?: Resolvable<InputOption | undefined>;
10+
}
11+
12+
export async function rollupDts({
13+
inputBaseDir = "src",
14+
outputBaseDir = "dist",
15+
input,
16+
}: RollupDtsOptions = {}): Promise<RollupOptions> {
17+
const dts = (await import("rollup-plugin-dts")).default;
18+
19+
const inputOption =
20+
(await resolve(input)) ?? (await getEntryFiles({ baseDir: inputBaseDir }));
21+
22+
return {
23+
input: inputOption,
24+
output: {
25+
dir: outputBaseDir,
26+
format: "es",
27+
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
28+
chunkFileNames: (info) => {
29+
const name = info.name.replace(/\.d$/, "");
30+
return chunkFileNames
31+
.replace(/\[name\]/g, name)
32+
.replace(/\.js$/, ".d.ts");
33+
},
34+
},
35+
plugins: [dts()],
36+
};
37+
}

src/rollup/entry.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ export type EntryFileFormatter = (
2424
| Promise<ReturnType<EntryFileFormatterSync>>
2525
| ReturnType<EntryFileFormatterSync>;
2626

27+
export type MatchFilesPatterns = string | string[] | number;
28+
2729
export interface MatchFilesOptions {
28-
patterns: string | string[] | number;
30+
patterns: MatchFilesPatterns;
2931
baseDir: string;
3032
ignore: string | string[];
3133
}
@@ -260,8 +262,16 @@ export async function getEntryFiles({
260262
return entryFiles;
261263
}
262264

263-
export async function inferSingleEntry(): Promise<string> {
264-
const files = await glob(`./src/index.{${DEFAULT_PATTERNS_EXT}}`);
265+
export interface InferSingleEntryOptions {
266+
baseDir?: string;
267+
}
268+
269+
export async function inferSingleEntry({
270+
baseDir,
271+
}: InferSingleEntryOptions = {}): Promise<string> {
272+
const files = await glob(`./index.{${DEFAULT_PATTERNS_EXT}}`, {
273+
cwd: baseDir,
274+
});
265275

266276
if (!files || files.length === 0) {
267277
throw new Error("failed to infer entry file");
@@ -275,5 +285,6 @@ export async function inferSingleEntry(): Promise<string> {
275285
);
276286
}
277287

278-
return files[0];
288+
const file = files[0];
289+
return baseDir ? path.posix.join(baseDir, file) : file;
279290
}

src/rollup/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
export { rollupNode } from "./node";
2-
export { rollupBundle } from "./bundle";
1+
export * from "./bundle";
2+
export * from "./dts";
3+
export * from "./node";
4+
5+
export * from "./options";

src/rollup/node.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { compilePlugins } from "./common-plugins";
44
import { getPkgJsonBaseContents } from "./gen-pkg";
55
import { chunkFileNames } from "../util/common";
66
import type { InputOption, OutputOptions, RollupOptions } from "rollup";
7-
import { getEntryFiles } from "./entry";
7+
import { getEntryFiles, MatchFilesPatterns } from "./entry";
88
import { Resolvable, resolve } from "../util/resolvable";
99
import { genOutputOptions, GenOutputOptions } from "./output";
10+
import { joinPath } from "../util/path";
1011

1112
const commonOutputOptions: OutputOptions = {
1213
exports: "auto",
@@ -15,7 +16,10 @@ const commonOutputOptions: OutputOptions = {
1516
};
1617

1718
export interface RollupNodeOptions {
19+
inputBaseDir?: string;
20+
inputPatterns?: MatchFilesPatterns;
1821
input?: Resolvable<InputOption | undefined>;
22+
outputBaseDir?: string;
1923
output?: Resolvable<
2024
GenOutputOptions | undefined,
2125
[{ outputRootDir: string }]
@@ -24,11 +28,21 @@ export interface RollupNodeOptions {
2428
}
2529

2630
export async function rollupNode({
27-
input = getEntryFiles,
31+
inputBaseDir = "src",
32+
inputPatterns,
33+
input,
34+
outputBaseDir = "dist",
2835
output,
29-
outputRootDir = "dist",
36+
outputRootDir,
3037
}: RollupNodeOptions = {}): Promise<RollupOptions> {
31-
const inputFiles = (await resolve(input)) ?? (await getEntryFiles());
38+
const inputFiles =
39+
(await resolve(input)) ??
40+
(await getEntryFiles({
41+
baseDir: inputBaseDir,
42+
patterns: inputPatterns,
43+
}));
44+
45+
outputRootDir = joinPath(outputBaseDir, outputRootDir || "");
3246

3347
return {
3448
input: inputFiles,

src/rollup/options.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { InputOption, RollupOptions } from "rollup";
2+
import { rollupBundle, rollupNode } from ".";
3+
import { RollupBundleOptions } from "./bundle";
4+
import { rollupDts, RollupDtsOptions } from "./dts";
5+
import { getEntryFiles, MatchFilesPatterns } from "./entry";
6+
import { RollupNodeOptions } from "./node";
7+
8+
export interface GenRollupOptions {
9+
inputBaseDir?: string;
10+
inputPatterns?: MatchFilesPatterns;
11+
outputBaseDir?: string;
12+
bundle?: boolean | RollupBundleOptions;
13+
node?: boolean | RollupNodeOptions;
14+
dts?: boolean | RollupDtsOptions;
15+
}
16+
17+
export async function rollupOptions({
18+
inputBaseDir,
19+
outputBaseDir,
20+
inputPatterns,
21+
bundle,
22+
node,
23+
dts,
24+
}: GenRollupOptions = {}): Promise<RollupOptions[]> {
25+
const bundleOptions: RollupBundleOptions | null = bundle
26+
? { outputBaseDir, inputBaseDir, ...(bundle === true ? null : bundle) }
27+
: null;
28+
29+
let cachedInput: Promise<InputOption> | null = null;
30+
const input = () => {
31+
if (!cachedInput)
32+
cachedInput = getEntryFiles({
33+
baseDir: inputBaseDir,
34+
patterns: inputPatterns,
35+
});
36+
37+
return cachedInput;
38+
};
39+
40+
const nodeOptions: RollupNodeOptions | null = node
41+
? {
42+
input,
43+
inputBaseDir,
44+
inputPatterns,
45+
outputBaseDir,
46+
...(node === true ? null : node),
47+
}
48+
: null;
49+
50+
const dtsOptions: RollupDtsOptions | null = dts
51+
? {
52+
input,
53+
inputBaseDir,
54+
outputBaseDir,
55+
...(dts === true ? null : dts),
56+
}
57+
: null;
58+
59+
const options = [
60+
bundleOptions && rollupBundle(bundleOptions),
61+
nodeOptions && rollupNode(nodeOptions),
62+
dtsOptions && rollupDts(dtsOptions),
63+
].filter((v): v is Promise<RollupOptions> => !!v);
64+
65+
return Promise.all(options);
66+
}

src/util/path.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { posix as pp } from "path";
2+
3+
export function joinPath(...paths: string[]): string {
4+
return pp.join(...paths);
5+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"forceConsistentCasingInFileNames": true
1111
},
1212
"include": ["**/*.ts"],
13+
"exclude": ["node_modules", "dist"],
1314
"ts-node": {
1415
"compilerOptions": {
1516
"skipLibCheck": true,

0 commit comments

Comments
 (0)