Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject values #96

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/cli/source/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import type { Configuration, Compiler } from "webpack";
import type { Manifest, Session } from "@hypertool/common";

import webpack from "webpack";
import crypto from "crypto";
Expand All @@ -30,6 +31,8 @@ const hash = (data: any) => {
const IMAGE_INLINE_SIZE_LIMIT = 10000;

export const prepare = (
manifest: Manifest,
session: Session,
environment: "production" | "development" | "test",
): Configuration => {
const production = environment === "production";
Expand Down Expand Up @@ -351,7 +354,7 @@ export const prepare = (
].filter(truthy) as any,
},
plugins: [
new DefineHypertoolPlugin(),
new DefineHypertoolPlugin(manifest, session),
/* Generate an index file and inject a script loader. */
new HtmlWebpackPlugin({
inject: true,
Expand Down Expand Up @@ -414,9 +417,11 @@ export const prepare = (
};

export const createCompiler = (
manifest: Manifest,
session: Session,
environment: "production" | "development" | "test",
): Compiler => {
const configuration = prepare(environment);
const configuration = prepare(manifest, session, environment);
try {
const compiler = webpack(configuration);
return compiler;
Expand Down
16 changes: 14 additions & 2 deletions packages/cli/source/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Manifest } from "@hypertool/common";

import chalk from "chalk";
import { Command } from "commander";
import TaskList from "listr";
Expand Down Expand Up @@ -56,7 +58,11 @@ const deploy = async (): Promise<void> => {
* Therefore, we hard code the value where the command is handled.
*/
process.env.NODE_ENV = "production";
const compiler = createCompiler("production");
const compiler = createCompiler(
context.manifest,
context.session,
"production",
);
compiler.run((error, stats) => {
compiler.close((error) => {
if (error) {
Expand Down Expand Up @@ -141,7 +147,13 @@ const start = async (configuration: any): Promise<void> => {
process.env.NODE_ENV = "development";
env.loadEnv();

const compiler = createCompiler("development");
const devManifest = await manifest.compile();
const devSession = await authUtils.loadSession();
const compiler = createCompiler(
<Manifest>devManifest,
devSession,
"development",
);
const server = await createServer(
configuration.port,
configuration.autoPort,
Expand Down
39 changes: 34 additions & 5 deletions packages/cli/source/plugins/DefineHypertoolPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { Manifest, Session } from "@hypertool/common";

import { Compiler } from "webpack";
import path from "path";

import type { Route } from "../types";

import { paths, listFiles } from "../utils";
import { DefinePlugin } from "webpack";

export const findRoutes = async () => {
export const findRoutes = async (): Promise<Route[]> => {
const absolutePaths = await listFiles(
`${paths.SCREENS_DIRECTORY}/**/*.{js,jsx,ts,tsx}`,
);
Expand All @@ -24,9 +28,21 @@ export const findRoutes = async () => {
return routes;
};

const prepareHypertool = (routes: any[]) => {
const stripManifest = (manifest: Manifest) => {
// TODO: Inject manifest.values only.
return {};
};

const prepareHypertool = (
routes: Route[],
manifest: Manifest,
session: Session,
) => {
const strippedManifest = stripManifest(manifest);
const hypertool: any = {
routes,
manifest: strippedManifest,
session,
};
const result = Object.keys(hypertool).reduce(
(carryOver: any, key: string) => {
Expand All @@ -39,17 +55,30 @@ const prepareHypertool = (routes: any[]) => {
};

export default class DefineHypertoolPlugin {
manifest?: Manifest;
session?: Session;

constructor(manifest: Manifest, session: Session) {
this.manifest = manifest;
this.session = session;
}

apply(compiler: Compiler) {
compiler.hooks.beforeCompile.tapAsync(
"DefineHypertoolPlugin",
(_compilation: unknown, callback) => {
findRoutes().then((routes: any) => {
async () => {
const routes = await findRoutes();
const definePlugin = new DefinePlugin({
"window.hypertool": prepareHypertool(routes),
"window.hypertool": prepareHypertool(
routes,
<Manifest>this.manifest,
<Session>this.session,
),
});
definePlugin.apply(compiler);
callback();
});
};
},
);
}
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/source/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Route {
path: string;
uri: string;
}