Skip to content
Merged
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
3 changes: 0 additions & 3 deletions src/browser/client-scripts/.eslintrc.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/browser/client-scripts/browser-utils/implementation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import * as lib from "@lib";

export function resetZoom(): void {
let meta = lib.queryFirst('meta[name="viewport"]') as HTMLMetaElement;
if (!meta) {
meta = document.createElement("meta");
meta.name = "viewport";

const head = lib.queryFirst("head");
head && head.appendChild(meta);
}
meta.content = "width=device-width,initial-scale=1.0,user-scalable=no";
}
15 changes: 15 additions & 0 deletions src/browser/client-scripts/browser-utils/inject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as implementation from "./implementation";

declare global {
// eslint-disable-next-line no-var
var __geminiCore: Record<string, unknown> | undefined;
// eslint-disable-next-line no-var
var __geminiNamespace: string;
}

const globalObj = typeof window === "undefined" ? globalThis : window;

if (!globalObj.__geminiCore) {
globalObj.__geminiCore = {};
}
globalObj.__geminiCore[__geminiNamespace] = implementation;
8 changes: 8 additions & 0 deletions src/browser/client-scripts/browser-utils/tsconfig.compat.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.compat.common.json",
"include": [".", "../../isomorphic", "../shared/"],
"exclude": ["./tsc-out", "../shared/lib.native.ts"],
"compilerOptions": {
"outDir": "./tsc-out"
}
}
8 changes: 8 additions & 0 deletions src/browser/client-scripts/browser-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../tsconfig.native.common.json",
"include": [".", "../../isomorphic", "../shared"],
"exclude": ["./tsc-out"],
"compilerOptions": {
"outDir": "./tsc-out"
}
}
63 changes: 50 additions & 13 deletions src/browser/client-scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
/* global process, __dirname */
const path = require("path");
const childProcess = require("node:child_process");
const browserify = require("browserify");
const uglifyify = require("uglifyify");
const aliasify = require("aliasify");
const fs = require("fs-extra");

const compileTypescript = async (targetDir, tsConfigName = "tsconfig.json") => {
const tsConfigPath = path.join(targetDir, tsConfigName);

if (!(await fs.pathExists(tsConfigPath))) {
throw new Error(`Could not find tsconfig: ${tsConfigPath}`);
}

childProcess.spawnSync(process.execPath, [require.resolve("typescript/bin/tsc"), "--project", tsConfigPath], {
cwd: targetDir,
stdio: "inherit"
});
};

/**
* @param {object} opts
* @param {boolean} opts.needsCompatLib
* @param {string} opts.entryFilePath
* @param {string} opts.libPath
* @returns {Promise<Buffer>}
*/
const bundleScript = async opts => {
const lib = opts.needsCompatLib ? "./lib.compat.js" : "./lib.native.js";
const basedir = path.dirname(opts.entryFilePath);

const script = browserify({
entries: "./index.js",
basedir: __dirname
entries: [opts.entryFilePath],
basedir
});

script.transform(
Expand All @@ -31,7 +47,8 @@ const bundleScript = async opts => {
script.transform(
{
aliases: {
"./lib": { relative: lib }
"@lib": opts.libPath,
"@isomorphic": opts.isomorphicPath
},
verbose: false
},
Expand All @@ -41,26 +58,46 @@ const bundleScript = async opts => {
return new Promise((resolve, reject) => {
script.bundle((err, buffer) => {
if (err) {
console.error(err);
reject(err);
}

resolve(buffer);
const resultingScript = `(function (__geminiNamespace) { ${buffer.toString()} })(arguments[0])`;

resolve(resultingScript);
});
});
};

async function main() {
const targetDir = path.join("build", path.relative(process.cwd(), __dirname));
const targetDir = path.resolve(process.argv[2]);
Comment thread
shadowusr marked this conversation as resolved.

if (!(await fs.pathExists(targetDir))) {
throw new Error(`Target directory does not exist: ${targetDir}`);
}

const tscOutDir = path.join(targetDir, "tsc-out");

await fs.ensureDir(targetDir);
const compatLibPath =
"./" + path.relative(process.cwd(), path.join(tscOutDir, "client-scripts", "shared", "lib.compat.js"));
const nativeLibPath =
"./" + path.relative(process.cwd(), path.join(tscOutDir, "client-scripts", "shared", "lib.native.js"));

await Promise.all(
[
{ needsCompatLib: true, fileName: "bundle.compat.js" },
{ needsCompatLib: false, fileName: "bundle.native.js" }
].map(async ({ needsCompatLib, fileName }) => {
const buffer = await bundleScript({ needsCompatLib });
const filePath = path.join(targetDir, fileName);
{ needsCompatLib: true, fileName: "bundle.compat.js", libPath: compatLibPath },
{ needsCompatLib: false, fileName: "bundle.native.js", libPath: nativeLibPath }
].map(async ({ needsCompatLib, fileName, libPath }) => {
await compileTypescript(targetDir, needsCompatLib ? "tsconfig.compat.json" : "tsconfig.json");

const projectDirName = path.basename(targetDir);
const entryFilePath = path.join(tscOutDir, "client-scripts", projectDirName, "inject.js");
const isomorphicPath = path.join(tscOutDir, "isomorphic", "index.js");
const buffer = await bundleScript({ needsCompatLib, entryFilePath, libPath, isomorphicPath });

const buildDir = path.join(targetDir, "build");
await fs.ensureDir(buildDir);
const filePath = path.join(buildDir, fileName);

await fs.writeFile(filePath, buffer);
})
Expand Down
7 changes: 0 additions & 7 deletions src/browser/client-scripts/ignore-areas.js

This file was deleted.

Loading
Loading