Skip to content

Commit

Permalink
use directories as entry and output for result
Browse files Browse the repository at this point in the history
  • Loading branch information
JayaKrishnaNamburu committed Oct 23, 2023
1 parent 9bf0aaa commit 283ad0e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
19 changes: 6 additions & 13 deletions src/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import path from "node:path";
import process from "node:process";
import fs from "node:fs/promises";
import { type RollupOptions, rollup } from "rollup";

import { JspmError, exists } from "../utils";
import type { Flags } from "../types";
import { RollupImportmapPlugin } from "./rollup-importmap-plugin";

export default async function build(entry: string, options: Flags) {
if (!entry && !options.buildConfig) {
if (!entry && !options.config) {
throw new JspmError(`Please provide entry for the build`);
}

let buildConfig: RollupOptions;
let outputOptions: RollupOptions["output"];

if (entry) {
if (!options.buildOutput) {
if (!options.output) {
throw new JspmError(`Build output is required when entry is provided`);
}

Expand All @@ -30,12 +29,12 @@ export default async function build(entry: string, options: Flags) {
};

outputOptions = {
file: path.join(process.cwd(), options.buildOutput),
dir: path.join(process.cwd(), options.output),
};
}

if (options.buildConfig) {
const buildConfigPath = path.join(process.cwd(), options.buildConfig);
if (options.config) {
const buildConfigPath = path.join(process.cwd(), options.config);
if ((await exists(buildConfigPath)) === false) {
throw new JspmError(
`Build config file does not exist: ${buildConfigPath}`
Expand All @@ -61,11 +60,5 @@ export default async function build(entry: string, options: Flags) {
}

const builder = await rollup(buildConfig);
const result = await builder.generate({ format: "esm", ...outputOptions });

for (const file of result.output) {
const outputPath = path.join(process.cwd(), file.fileName);
const content = file.type === "asset" ? file.source : file.code;
await fs.writeFile(outputPath, content, "utf-8");
}
await builder.write({ format: "esm", ...outputOptions });
}
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ const freezeOpt: opt = [
];
const silentOpt: opt = ["--silent", "Silence all output", { default: false }];
const buildConfigOpt: opt = [
"--build-config <file>",
"--config <file>",
"Path to a rollup config file",
{},
];
const buildOutputOpt: opt = [
"--build-output <file>",
"Path to the rollup output file",
"--output <dir>",
"Path to the rollup output directory",
{},
];

Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface Flags extends BuildFlags {
export interface BuildFlags {
entry?: string;
outdir?: string;
buildConfig?: string;
config: string;
buildOutput?: string;
}

Expand Down

0 comments on commit 283ad0e

Please sign in to comment.