Skip to content

Commit

Permalink
precompile eta
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn authored and modmuss50 committed Apr 11, 2024
1 parent 153b1f5 commit 3590b44
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 62 deletions.
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"remoteUser": "node",
"build": { "dockerfile": "../Dockerfile" },
"forwardPorts": [
4000,
Expand Down
8 changes: 6 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
FROM node:18-bookworm

ENV DEBIAN_FRONTEND=noninteractive
ENV PATH="${PATH}:$HOME/gems/bin"
ENV GEM_HOME="$HOME/gems"

RUN apt update && apt install -y ruby-full build-essential zlib1g-dev

USER node
ENV HOME="/home/node"

ENV PATH="${PATH}:$HOME/gems/bin"
ENV GEM_HOME="$HOME/gems"
RUN gem install jekyll bundler

ENV DENO_INSTALL="$HOME/.deno"
Expand Down
41 changes: 21 additions & 20 deletions scripts/package-lock.json

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

5 changes: 3 additions & 2 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"dev": "vite build --watch -m development",
"build": "npm run check && vite build",
"buildLib": "BUILD_LIB=true vite build",
"check": "svelte-check --tsconfig ./tsconfig.json"
"check": "svelte-check --tsconfig ./tsconfig.json",
"postinstall": "patch-package"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "2.4.1",
Expand All @@ -20,7 +21,7 @@
"vite": "^4.3.9"
},
"dependencies": {
"eta": "^3.0.3",
"eta": "^3.4.0",
"file-saver": "^2.0.5",
"jszip": "^3.10.1",
"vite-plugin-dts": "^2.3.0"
Expand Down
10 changes: 3 additions & 7 deletions scripts/src/lib/template/eta.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Eta } from "eta"
import { Eta } from "eta";

const eta = new Eta({
export const eta = new Eta({
autoTrim: false
})

export function renderTemplate(template: string, options: object): string {
return eta.renderString(template, options);
}
});
10 changes: 5 additions & 5 deletions scripts/src/lib/template/gradlegroovy.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import type { ComputedConfiguration, TemplateWriter } from './template';
import { renderTemplate } from './eta';

import gradlePropertiesTemplate from './templates/gradle/groovy/gradle.properties.eta?raw';
import buildGradleTemplate from './templates/gradle/groovy/build.gradle.eta?raw';
import gradlePropertiesTemplate from './templates/gradle/groovy/gradle.properties.eta';
import buildGradleTemplate from './templates/gradle/groovy/build.gradle.eta';
import settingsGradle from './templates/gradle/groovy/settings.gradle?raw';
import { getJavaVersion } from './java';
import { eta } from './eta';

export async function addGroovyGradle(writer: TemplateWriter, config: ComputedConfiguration) {
await writer.write('gradle.properties', renderTemplate(gradlePropertiesTemplate, config));
await writer.write('build.gradle', renderTemplate(buildGradleTemplate, {...config, java: getJavaVersion(config.minecraftVersion)}));
await writer.write('gradle.properties', eta.render(gradlePropertiesTemplate, config));
await writer.write('build.gradle', eta.render(buildGradleTemplate, {...config, java: getJavaVersion(config.minecraftVersion)}));
await writer.write('settings.gradle', settingsGradle);
}
10 changes: 5 additions & 5 deletions scripts/src/lib/template/mixin.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { renderTemplate } from "./eta";
import type { ComputedConfiguration, TemplateWriter } from "./template";

import mixinTemplate from './templates/mixin/Mixin.java.eta?raw';
import mixinClientTemplate from './templates/mixin/ClientMixin.java.eta?raw';
import mixinTemplate from './templates/mixin/Mixin.java.eta';
import mixinClientTemplate from './templates/mixin/ClientMixin.java.eta';
import { getJavaVersion } from "./java";
import { eta } from "./eta";

export async function generateMixin(writer: TemplateWriter, options: ComputedConfiguration) : Promise<unknown[]> {
const packageName = options.packageName + ".mixin";
Expand All @@ -23,7 +23,7 @@ export async function generateMixin(writer: TemplateWriter, options: ComputedCon

const mixinJsonName = `${options.modid}.mixins.json`;
await writer.write(`src/main/resources/${mixinJsonName}`, JSON.stringify(mixinJson, null, "\t"));
await writer.write(`src/main/java/${packageName.replaceAll("\.", "/")}/${className}.java`, renderTemplate(mixinTemplate, {
await writer.write(`src/main/java/${packageName.replaceAll("\.", "/")}/${className}.java`, eta.render(mixinTemplate, {
className,
packageName
}));
Expand All @@ -49,7 +49,7 @@ export async function generateClientMixin(writer: TemplateWriter, options: Compu

const mixinJsonName = `${options.modid}.client.mixins.json`;
await writer.write(`src/client/resources/${mixinJsonName}`, JSON.stringify(mixinJson, null, "\t"));
await writer.write(`src/client/java/${packageName.replaceAll("\.", "/")}/${className}.java`, renderTemplate(mixinClientTemplate, {
await writer.write(`src/client/java/${packageName.replaceAll("\.", "/")}/${className}.java`, eta.render(mixinClientTemplate, {
className,
packageName
}));
Expand Down
26 changes: 13 additions & 13 deletions scripts/src/lib/template/modentrypoint.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { renderTemplate } from "./eta";
import type { ComputedConfiguration, TemplateWriter } from "./template";

import javaEntrypointTemplate from './templates/entrypoint/Entrypoint.java.eta?raw';
import kotlinEntrypointTemplate from './templates/entrypoint/Entrypoint.kt.eta?raw';
import javaEntrypointClientTemplate from './templates/entrypoint/ClientEntrypoint.java.eta?raw';
import kotlinEntrypointClientTemplate from './templates/entrypoint/ClientEntrypoint.kt.eta?raw';
import javaEntrypointDataGeneratorTemplate from './templates/entrypoint/DataGeneratorEntrypoint.java.eta?raw';
import kotlinEntrypointDataGeneratorTemplate from './templates/entrypoint/DataGeneratorEntrypoint.kt.eta?raw';
import javaEntrypointTemplate from './templates/entrypoint/Entrypoint.java.eta';
import kotlinEntrypointTemplate from './templates/entrypoint/Entrypoint.kt.eta';
import javaEntrypointClientTemplate from './templates/entrypoint/ClientEntrypoint.java.eta';
import kotlinEntrypointClientTemplate from './templates/entrypoint/ClientEntrypoint.kt.eta';
import javaEntrypointDataGeneratorTemplate from './templates/entrypoint/DataGeneratorEntrypoint.java.eta';
import kotlinEntrypointDataGeneratorTemplate from './templates/entrypoint/DataGeneratorEntrypoint.kt.eta';
import { getMinorMinecraftVersion } from "./minecraft";
import { eta } from "./eta";

interface ClassOptions {
package: string, // com.example
Expand Down Expand Up @@ -55,10 +55,10 @@ async function generateJavaEntrypoint(writer: TemplateWriter, options: ClassOpti
]
};

await writer.write(`src/main/java/${options.path}.java`, renderTemplate(javaEntrypointTemplate, options))
await writer.write(`src/main/java/${options.path}.java`, eta.render(javaEntrypointTemplate, options))

if (options.clientEntrypoint) {
await writer.write(`src/client/java/${options.path}Client.java`, renderTemplate(javaEntrypointClientTemplate, {...options, className: options.className + "Client"}));
await writer.write(`src/client/java/${options.path}Client.java`, eta.render(javaEntrypointClientTemplate, {...options, className: options.className + "Client"}));

entrypoints = {
...entrypoints,
Expand All @@ -69,7 +69,7 @@ async function generateJavaEntrypoint(writer: TemplateWriter, options: ClassOpti
}

if (options.dataEntrypoint) {
await writer.write(`src/main/java/${options.path}DataGenerator.java`, renderTemplate(javaEntrypointDataGeneratorTemplate, {...options, className: options.className + "DataGenerator"}));
await writer.write(`src/main/java/${options.path}DataGenerator.java`, eta.render(javaEntrypointDataGeneratorTemplate, {...options, className: options.className + "DataGenerator"}));

entrypoints = {
...entrypoints,
Expand All @@ -92,10 +92,10 @@ async function generateKotlinEntrypoint(writer: TemplateWriter, options: ClassOp
]
};

await writer.write(`src/main/kotlin/${options.path}.kt`, renderTemplate(kotlinEntrypointTemplate, options))
await writer.write(`src/main/kotlin/${options.path}.kt`, eta.render(kotlinEntrypointTemplate, options))

if (options.clientEntrypoint) {
await writer.write(`src/client/kotlin/${options.path}Client.kt`, renderTemplate(kotlinEntrypointClientTemplate, {...options, className: options.className + "Client"}))
await writer.write(`src/client/kotlin/${options.path}Client.kt`, eta.render(kotlinEntrypointClientTemplate, {...options, className: options.className + "Client"}))

entrypoints = {
...entrypoints,
Expand All @@ -109,7 +109,7 @@ async function generateKotlinEntrypoint(writer: TemplateWriter, options: ClassOp
}

if (options.dataEntrypoint) {
await writer.write(`src/main/kotlin/${options.path}DataGenerator.kt`, renderTemplate(kotlinEntrypointDataGeneratorTemplate, {...options, className: options.className + "DataGenerator"}))
await writer.write(`src/main/kotlin/${options.path}DataGenerator.kt`, eta.render(kotlinEntrypointDataGeneratorTemplate, {...options, className: options.className + "DataGenerator"}))

entrypoints = {
...entrypoints,
Expand Down
5 changes: 5 additions & 0 deletions scripts/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
/// <reference types="svelte" />
/// <reference types="vite/client" />

declare module "*.eta" {
const template: import("eta/dist/types/compile").TemplateFunction;
export default template;
}
38 changes: 32 additions & 6 deletions scripts/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import dts from 'vite-plugin-dts'
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import dts from 'vite-plugin-dts';
import { Eta } from 'eta';

const buildLib = process.env.BUILD_LIB;

/**
* @returns {import('vite').Plugin}
*/
function eta() {
const eta = new Eta({
autoTrim: false
});

return {
transform(template, id) {
if (id.match(/.eta$/)) return {
code: `
export default ${eta.compile(template)};
`
};
}
};
}

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => buildLib ? ({
// Library for CLI
plugins: [dts({ rollupTypes: true })],
plugins: [
eta(),
dts({ rollupTypes: true })
],
build: {
sourcemap: false,
minify: false,
minify: true,
outDir: "./dist",
emptyOutDir: true,
lib: {
Expand All @@ -22,7 +45,10 @@ export default defineConfig(({ mode }) => buildLib ? ({
}
}) : ({
// Web build
plugins: [svelte()],
plugins: [
eta(),
svelte()
],
build: {
sourcemap: mode === "development",
// Build directly into the Jekyll output directory
Expand Down
1 change: 1 addition & 0 deletions template-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.0.0",
"private": true,
"scripts": {
"buildLib": "cd ../scripts && npm install && npm run buildLib",
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev",
Expand Down
4 changes: 2 additions & 2 deletions template-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ app.get(
})
)

app.post('/v1/generate', async (c) => c.json(await generateTemplate(c.req.json())))
app.get('/v1/generate', async (c) => c.json(await generateTemplate({})))
app.post('/v1/generate', async (c) => await generateTemplate(c.req.json()))
app.get('/v1/generate', async () => await generateTemplate({}))
app.get('/v1/minecraftVersions', async (c) => c.json(await getMinecraftVersions()))

export default app

0 comments on commit 3590b44

Please sign in to comment.