Skip to content

Commit

Permalink
chore: format with prettier v3
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 18, 2023
1 parent c338907 commit 40ec1d9
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 94 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"changelogen": "^0.5.4",
"eslint": "^8.45.0",
"eslint-config-unjs": "^0.2.1",
"prettier": "^2.8.8",
"prettier": "^3.0.0",
"vitest": "^0.33.0"
},
"packageManager": "pnpm@8.2.0"
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

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

14 changes: 7 additions & 7 deletions src/auto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ export const autoPreset = definePreset(() => {
chalk.bold(
e.input
.replace(ctx.options.rootDir + "/", "")
.replace(/\/$/, "/*")
)
.replace(/\/$/, "/*"),
),
)
.join(", ")
.join(", "),
),
chalk.gray(
["esm", res.cjs && "cjs", res.dts && "dts"]
.filter(Boolean)
.map((tag) => `[${tag}]`)
.join(" ")
)
.join(" "),
),
);
},
},
Expand All @@ -65,7 +65,7 @@ export const autoPreset = definePreset(() => {
*/
export function inferEntries(
pkg: PackageJson,
sourceFiles: string[]
sourceFiles: string[],
): InferEntriesResult {
const warnings = [];

Expand Down Expand Up @@ -113,7 +113,7 @@ export function inferEntries(
// But we support any file extension here in case user has extended rollup options
const outputSlug = output.file.replace(
/(\*[^/\\]*|\.d\.(m|c)?ts|\.\w+)$/,
""
"",
);
const isDir = outputSlug.endsWith("/");

Expand Down
26 changes: 14 additions & 12 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { mkdistBuild } from "./builder/mkdist";
export async function build(
rootDir: string,
stub: boolean,
inputConfig: BuildConfig = {}
inputConfig: BuildConfig = {},
) {
// Determine rootDir
rootDir = resolve(process.cwd(), rootDir || ".");
Expand All @@ -49,7 +49,7 @@ async function _build(
stub: boolean,
inputConfig: BuildConfig = {},
buildConfig: BuildConfig,
pkg: PackageJson & Record<"unbuild" | "build", BuildConfig>
pkg: PackageJson & Record<"unbuild" | "build", BuildConfig>,
) {
// Resolve preset
const preset = resolvePreset(
Expand All @@ -58,7 +58,7 @@ async function _build(
pkg.build?.preset ||
inputConfig.preset ||
"auto",
rootDir
rootDir,
);

// Merge options
Expand Down Expand Up @@ -110,7 +110,7 @@ async function _build(
respectExternal: true,
},
},
}
},
) as BuildOptions;

// Resolve dirs relative to rootDir
Expand Down Expand Up @@ -142,7 +142,7 @@ async function _build(

// Normalize entries
options.entries = options.entries.map((entry) =>
typeof entry === "string" ? { input: entry } : entry
typeof entry === "string" ? { input: entry } : entry,
);

for (const entry of options.entries) {
Expand Down Expand Up @@ -185,7 +185,7 @@ async function _build(

// Start info
consola.info(
chalk.cyan(`${options.stub ? "Stubbing" : "Building"} ${options.name}`)
chalk.cyan(`${options.stub ? "Stubbing" : "Building"} ${options.name}`),
);
if (process.env.DEBUG) {
consola.info(`${chalk.bold("Root dir:")} ${options.rootDir}
Expand Down Expand Up @@ -271,7 +271,9 @@ async function _build(
return chalk.gray(
" └─ " +
rPath(p) +
chalk.bold(chunk.bytes ? ` (${prettyBytes(chunk?.bytes)})` : "")
chalk.bold(
chunk.bytes ? ` (${prettyBytes(chunk?.bytes)})` : "",
),
);
})
.join("\n");
Expand All @@ -286,7 +288,7 @@ async function _build(
return chalk.gray(
" 📦 " +
rPath(m.id) +
chalk.bold(m.bytes ? ` (${prettyBytes(m.bytes)})` : "")
chalk.bold(m.bytes ? ` (${prettyBytes(m.bytes)})` : ""),
);
})
.join("\n");
Expand All @@ -296,8 +298,8 @@ async function _build(
console.log(
"Σ Total dist size (byte size):",
chalk.cyan(
prettyBytes(ctx.buildEntries.reduce((a, e) => a + (e.bytes || 0), 0))
)
prettyBytes(ctx.buildEntries.reduce((a, e) => a + (e.bytes || 0), 0)),
),
);

// Validate
Expand All @@ -312,11 +314,11 @@ async function _build(
if (ctx.warnings.size > 0) {
consola.warn(
"Build is done with some warnings:\n\n" +
[...ctx.warnings].map((msg) => "- " + msg).join("\n")
[...ctx.warnings].map((msg) => "- " + msg).join("\n"),
);
if (ctx.options.failOnWarn) {
consola.error(
"Exiting with code (1). You can change this behavior by setting `failOnWarn: false` ."
"Exiting with code (1). You can change this behavior by setting `failOnWarn: false` .",
);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
Expand Down
4 changes: 2 additions & 2 deletions src/builder/mkdist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { MkdistBuildEntry, BuildContext } from "../types";

export async function mkdistBuild(ctx: BuildContext) {
const entries = ctx.options.entries.filter(
(e) => e.builder === "mkdist"
(e) => e.builder === "mkdist",
) as MkdistBuildEntry[];
await ctx.hooks.callHook("mkdist:entries", ctx, entries);
for (const entry of entries) {
Expand All @@ -29,7 +29,7 @@ export async function mkdistBuild(ctx: BuildContext) {
"mkdist:entry:options",
ctx,
entry,
mkdistOptions
mkdistOptions,
);
const output = await mkdist(mkdistOptions);
ctx.buildEntries.push({
Expand Down
2 changes: 1 addition & 1 deletion src/builder/plugins/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function esbuild(options: EsbuildOptions): Plugin {
function printWarnings(
id: string,
result: TransformResult,
plugin: PluginContext
plugin: PluginContext,
) {
if (result.warnings) {
for (const warning of result.warnings) {
Expand Down
39 changes: 21 additions & 18 deletions src/builder/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,20 @@ export async function rollupBuild(ctx: BuildContext) {
const jitiPath = await resolvePath("jiti", { url: import.meta.url });

for (const entry of ctx.options.entries.filter(
(entry) => entry.builder === "rollup"
(entry) => entry.builder === "rollup",
)) {
const output = resolve(
ctx.options.rootDir,
ctx.options.outDir,
entry.name!
entry.name!,
);

const resolvedEntry = normalize(
tryResolve(entry.input, ctx.options.rootDir) || entry.input
tryResolve(entry.input, ctx.options.rootDir) || entry.input,
);
const resolvedEntryWithoutExt = resolvedEntry.slice(
0,
Math.max(0, resolvedEntry.length - extname(resolvedEntry).length)
Math.max(0, resolvedEntry.length - extname(resolvedEntry).length),
);
const code = await fsp.readFile(resolvedEntry, "utf8");
const shebang = getShebang(code);
Expand All @@ -63,10 +63,10 @@ export async function rollupBuild(ctx: BuildContext) {
await writeFile(
output + ".cjs",
`${shebang}module.exports = require(${JSON.stringify(
jitiPath
jitiPath,
)})(null, { interopDefault: true, esmResolve: true })(${JSON.stringify(
resolvedEntry
)})`
resolvedEntry,
)})`,
);
}

Expand All @@ -76,7 +76,7 @@ export async function rollupBuild(ctx: BuildContext) {
resolvedEntry,
{
extensions: DEFAULT_EXTENSIONS,
}
},
).catch((error) => {
warn(ctx, `Cannot analyze ${resolvedEntry} for exports:` + error);
return [];
Expand All @@ -92,13 +92,13 @@ export async function rollupBuild(ctx: BuildContext) {
"",
`/** @type {import(${JSON.stringify(resolvedEntryWithoutExt)})} */`,
`const _module = jiti(null, { interopDefault: true, esmResolve: true })(${JSON.stringify(
resolvedEntry
resolvedEntry,
)});`,
hasDefaultExport ? "\nexport default _module;" : "",
...namedExports
.filter((name) => name !== "default")
.map((name) => `export const ${name} = _module.${name};`),
].join("\n")
].join("\n"),
);

// DTS Stub
Expand All @@ -108,10 +108,10 @@ export async function rollupBuild(ctx: BuildContext) {
`export * from ${JSON.stringify(resolvedEntryWithoutExt)};`,
hasDefaultExport
? `export { default } from ${JSON.stringify(
resolvedEntryWithoutExt
resolvedEntryWithoutExt,
)};`
: "",
].join("\n")
].join("\n"),
);

if (shebang) {
Expand All @@ -138,7 +138,7 @@ export async function rollupBuild(ctx: BuildContext) {
const { output } = await buildResult.write(outputOptions);
const chunkFileNames = new Set<string>();
const outputChunks = output.filter(
(e) => e.type === "chunk"
(e) => e.type === "chunk",
) as OutputChunk[];
for (const entry of outputChunks) {
chunkFileNames.add(entry.fileName);
Expand All @@ -148,7 +148,7 @@ export async function rollupBuild(ctx: BuildContext) {
if (entry.isEntry) {
ctx.buildEntries.push({
chunks: entry.imports.filter((i) =>
outputChunks.find((c) => c.fileName === i)
outputChunks.find((c) => c.fileName === i),
),
modules: Object.entries(entry.modules).map(([id, mod]) => ({
id,
Expand All @@ -170,7 +170,7 @@ export async function rollupBuild(ctx: BuildContext) {
rollupOptions.plugins = rollupOptions.plugins || [];
// TODO: Use fresh rollup options
const shebangPlugin: any = rollupOptions.plugins.find(
(p) => p && p.name === "unbuild-shebang"
(p) => p && p.name === "unbuild-shebang",
);
shebangPlugin._options.preserve = false;

Expand Down Expand Up @@ -215,7 +215,7 @@ export async function rollupBuild(ctx: BuildContext) {
const getChunkFilename = (
ctx: BuildContext,
chunk: PreRenderedChunk,
ext: string
ext: string,
) => {
if (chunk.isDynamicEntry) {
return `chunks/[name].${ext}`;
Expand All @@ -229,7 +229,10 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
input: Object.fromEntries(
ctx.options.entries
.filter((entry) => entry.builder === "rollup")
.map((entry) => [entry.name, resolve(ctx.options.rootDir, entry.input)])
.map((entry) => [
entry.name,
resolve(ctx.options.rootDir, entry.input),
]),
),

output: [
Expand Down Expand Up @@ -307,7 +310,7 @@ export function getRollupOptions(ctx: BuildContext): RollupOptions {
? Object.fromEntries(
ctx.options.rollup.alias.entries.map((entry) => {
return [entry.find, entry.replacement];
})
}),
)
: ctx.options.rollup.alias.entries),
},
Expand Down
4 changes: 2 additions & 2 deletions src/builder/untyped.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { BuildContext, UntypedBuildEntry, UntypedOutputs } from "../types";

export async function typesBuild(ctx: BuildContext) {
const entries = ctx.options.entries.filter(
(entry) => entry.builder === "untyped"
(entry) => entry.builder === "untyped",
) as UntypedBuildEntry[];
await ctx.hooks.callHook("untyped:entries", ctx, entries);

Expand Down Expand Up @@ -64,7 +64,7 @@ export async function typesBuild(ctx: BuildContext) {
await writeFile(
resolve(distDir, output.fileName),
output.contents,
"utf8"
"utf8",
);
}
}
Expand Down
Loading

0 comments on commit 40ec1d9

Please sign in to comment.