Skip to content

Commit

Permalink
Refactor effect to effection (#27)
Browse files Browse the repository at this point in the history
* doc: add code comment

* chore: error handling
  • Loading branch information
joshamaju authored Dec 15, 2024
1 parent ef094cc commit d945998
Show file tree
Hide file tree
Showing 10 changed files with 1,247 additions and 4,483 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-hornets-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"stack54": minor
---

Refactors from effect to effection, in addition with proper error reporting to the console
2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@
"@tsconfig/svelte": "^5.0.2",
"devalue": "^4.3.2",
"effect": "^3.5.7",
"effection": "^3.0.3",
"es-module-lexer": "^1.5.4",
"fp-ts": "^2.16.9",
"fs-extra": "^11.2.0",
"glob": "^10.3.10",
"kleur": "^4.1.5",
Expand Down
9 changes: 8 additions & 1 deletion packages/core/src/core/build/facade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ export async function prepare(
return visitor(node);
}

// tags that we'll need to move back to their original location
/**
* Users should be able to place script tags anywhere in the document, but vite hoists module script
* tags into the document head.
*
* So we need to wrap them in a html and head tag to make vite keep them in the same location, if not vite
* will hoist the module to the beginning of the document (breaks the build during svelte compilation) or the
* user defined head tag if any.
*/
const moves: Array<string> = [];

// tags that we need to remove temporarily because they break the build
Expand Down
83 changes: 40 additions & 43 deletions packages/core/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,80 +2,77 @@ import * as path from "node:path";

import fs from "fs-extra";

import { Effect } from "effect";
import { call, all } from "effection";
import color from "kleur";

import * as Config from "../config/index.js";
import { defineServerEnv, load, partition } from "../env.js";
import { makeVite } from "../utils/vite.js";
import { buildServer } from "./server.js";
import { buildViews } from "./view.js";
import { makeViteLogger } from "../logger.js";
import {
runBuildEnd,
runBuildStart,
runConfigResolved,
runConfigSetup,
} from "../integrations/hooks.js";
import { makeViteLogger, useLogger } from "../logger.js";
import { makeVite } from "../utils/vite.js";
import { buildServer } from "./server.js";
import { buildViews } from "./view.js";

export function* build() {
const start = process.hrtime.bigint();

const cwd = process.cwd();
const cwd = process.cwd();

export function build() {
return Effect.gen(function* () {
const inline_config = yield* Effect.tryPromise(() => Config.load(cwd));
const logger = useLogger();

const user_config = yield* Config.parse(inline_config);
const inline_config = yield* call(Config.load(cwd));

let merged_config = yield* Effect.promise(() => {
return runConfigSetup(user_config, { command: "build" });
});
const user_config = Config.parse(inline_config);

const resolved_config = yield* Effect.tryPromise(() => {
return Config.preprocess(merged_config, { cwd });
});
let merged_config = yield* call(
runConfigSetup(user_config, { command: "build" })
);

const logger = yield* makeViteLogger();
const resolved_config = yield* call(
Config.preprocess(merged_config, { cwd })
);

const shared_vite_config = makeVite(resolved_config, {
logger,
mode: "build",
});
const vite_logger = makeViteLogger();

const config = { ...resolved_config, vite: shared_vite_config };
const shared_vite_config = makeVite(resolved_config, {
logger: vite_logger,
mode: "build",
});

yield* Effect.tryPromise(() => runConfigResolved(config));
const config = { ...resolved_config, vite: shared_vite_config };

const outDir = path.join(cwd, config.build.outDir);
yield* call(runConfigResolved(config));

const mode = process.env.NODE_ENV ?? "production";
const outDir = path.join(cwd, config.build.outDir);

const env = load(config.env.dir ?? cwd, mode);
const { public: public_ } = partition(env, config.env.publicPrefix);
yield* all([call(fs.remove(outDir)), call(runBuildStart(config))]);

yield* Effect.tryPromise(() => fs.remove(outDir));
const mode = process.env.NODE_ENV ?? "production";

yield* Effect.promise(() => runBuildStart(config));
const env = load(config.env.dir ?? cwd, mode);
const { public: public_ } = partition(env, config.env.publicPrefix);

yield* Effect.log("building views...");
const opts = { cwd, outDir, config: config, env: public_ };

const opts = { cwd, outDir, config: config, env: public_ };
// logger.info("building views...");

const start = performance.now();
const views = yield* buildViews(opts);
const time = performance.now() - start;
const views = yield* buildViews(opts);

yield* Effect.log(`built views in ${Math.round(time)} ${color.dim("ms")}`);
// logger.info("building server...");

yield* Effect.log("building server...");
defineServerEnv(env);

defineServerEnv(env);
yield* call(buildServer(views, { env, outDir, config }));

yield* Effect.promise(() => {
return buildServer(views, { env, outDir, config });
});
yield* call(runBuildEnd(config));

yield* Effect.promise(() => runBuildEnd(config));
const end = process.hrtime.bigint();
const time = Number(end - start) / 1e6;

yield* Effect.log("✔︎ build done");
});
logger.info(`✔︎ build done in ${Math.round(time)} ${color.dim("ms")}`);
}
Loading

0 comments on commit d945998

Please sign in to comment.