Skip to content

Commit

Permalink
style: format with prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jan 13, 2023
1 parent 3d1b976 commit 818ced7
Show file tree
Hide file tree
Showing 25 changed files with 844 additions and 443 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
"unicorn/no-null": 0,
"unicorn/prevent-abbreviations": 0,
"unicorn/prefer-module": 0,
"unicorn/prefer-top-level-await": 0
"unicorn/prefer-top-level-await": 0,
"@typescript-eslint/no-non-null-assertion": 0,
"@typescript-eslint/no-unused-vars": 0
}
}
Empty file added .prettierrc
Empty file.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"scripts": {
"build": "pnpm unbuild",
"dev": "pnpm unbuild test/fixture",
"lint": "eslint --ext .ts,.js .",
"lint": "eslint --ext .ts,.js . && prettier -c src test",
"prepack": "pnpm unbuild",
"release": "vitest run && changelogen --release && git push --follow-tags && npm publish",
"stub": "pnpm unbuild --stub",
Expand Down Expand Up @@ -65,6 +65,7 @@
"changelogen": "^0.4.0",
"eslint": "^8.31.0",
"eslint-config-unjs": "^0.0.3",
"prettier": "^2.8.2",
"vitest": "^0.27.1"
},
"packageManager": "pnpm@7.18.2"
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

66 changes: 50 additions & 16 deletions src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@ import type { PackageJson } from "pkg-types";
import { extractExportFilenames, listRecursively, warn } from "./utils";
import { BuildEntry, definePreset, MkdistBuildEntry } from "./types";

type InferEntriesResult = { entries: BuildEntry[], cjs?: boolean, dts?: boolean, warnings: string[] }
type InferEntriesResult = {
entries: BuildEntry[];
cjs?: boolean;
dts?: boolean;
warnings: string[];
};

export const autoPreset = definePreset(() => {
return {
hooks: {
"build:prepare" (ctx) {
"build:prepare"(ctx) {
// Disable auto if entries already provided of pkg not available
if (!ctx.pkg || ctx.options.entries.length > 0) {
return;
Expand All @@ -29,11 +34,26 @@ export const autoPreset = definePreset(() => {
}
consola.info(
"Automatically detected entries:",
chalk.cyan(ctx.options.entries.map(e => chalk.bold(e.input.replace(ctx.options.rootDir + "/", "").replace(/\/$/, "/*"))).join(", ")),
chalk.gray(["esm", res.cjs && "cjs", res.dts && "dts"].filter(Boolean).map(tag => `[${tag}]`).join(" "))
chalk.cyan(
ctx.options.entries
.map((e) =>
chalk.bold(
e.input
.replace(ctx.options.rootDir + "/", "")
.replace(/\/$/, "/*")
)
)
.join(", ")
),
chalk.gray(
["esm", res.cjs && "cjs", res.dts && "dts"]
.filter(Boolean)
.map((tag) => `[${tag}]`)
.join(" ")
)
);
}
}
},
},
};
});

Expand All @@ -43,7 +63,10 @@ export const autoPreset = definePreset(() => {
* - if string, `<source>/src` will be scanned for possible source files.
* - if an array of source files, these will be used directly instead of accessing fs.
*/
export function inferEntries (pkg: PackageJson, sourceFiles: string[]): InferEntriesResult {
export function inferEntries(
pkg: PackageJson,
sourceFiles: string[]
): InferEntriesResult {
const warnings = [];

// Sort files so least-nested files are first
Expand All @@ -53,7 +76,8 @@ export function inferEntries (pkg: PackageJson, sourceFiles: string[]): InferEnt
const outputs = extractExportFilenames(pkg.exports);

if (pkg.bin) {
const binaries = typeof pkg.bin === "string" ? [pkg.bin] : Object.values(pkg.bin);
const binaries =
typeof pkg.bin === "string" ? [pkg.bin] : Object.values(pkg.bin);
for (const file of binaries) {
outputs.push({ file });
}
Expand All @@ -70,7 +94,7 @@ export function inferEntries (pkg: PackageJson, sourceFiles: string[]): InferEnt

// Try to detect output types
const isESMPkg = pkg.type === "module";
for (const output of outputs.filter(o => !o.type)) {
for (const output of outputs.filter((o) => !o.type)) {
const isJS = output.file.endsWith(".js");
if ((isESMPkg && isJS) || output.file.endsWith(".mjs")) {
output.type = "esm";
Expand All @@ -91,14 +115,20 @@ export function inferEntries (pkg: PackageJson, sourceFiles: string[]): InferEnt
const isDir = outputSlug.endsWith("/");

// Skip top level directory
if (isDir && ["./", "/"].includes(outputSlug)) { continue; }
if (isDir && ["./", "/"].includes(outputSlug)) {
continue;
}

const possiblePaths = getEntrypointPaths(outputSlug);
// eslint-disable-next-line unicorn/no-array-reduce
const input = possiblePaths.reduce<string | undefined>((source, d) => {
if (source) { return source; }
if (source) {
return source;
}
const SOURCE_RE = new RegExp(`${d}${isDir ? "" : "\\.\\w+"}$`);
return sourceFiles.find(i => i.match(SOURCE_RE))?.replace(/(\.d\.ts|\.\w+)$/, "");
return sourceFiles
.find((i) => i.match(SOURCE_RE))
?.replace(/(\.d\.ts|\.\w+)$/, "");
}, undefined as any);

if (!input) {
Expand All @@ -110,15 +140,17 @@ export function inferEntries (pkg: PackageJson, sourceFiles: string[]): InferEnt
cjs = true;
}

const entry = entries.find(i => i.input === input) || entries[entries.push({ input }) - 1];
const entry =
entries.find((i) => i.input === input) ||
entries[entries.push({ input }) - 1];

if (output.file.endsWith(".d.ts")) {
dts = true;
}

if (isDir) {
entry.outDir = outputSlug
;(entry as MkdistBuildEntry).format = output.type;
entry.outDir = outputSlug;
(entry as MkdistBuildEntry).format = output.type;
}
}

Expand All @@ -127,5 +159,7 @@ export function inferEntries (pkg: PackageJson, sourceFiles: string[]): InferEnt

export const getEntrypointPaths = (path: string) => {
const segments = normalize(path).split("/");
return segments.map((_, index) => segments.slice(index).join("/")).filter(Boolean);
return segments
.map((_, index) => segments.slice(index).join("/"))
.filter(Boolean);
};
Loading

0 comments on commit 818ced7

Please sign in to comment.