Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
build(nodejs): introduce rollup to nodejs build system for requiring …
Browse files Browse the repository at this point in the history
…raw files(mustache files) (#135)

* build(nodejs): introduce rollup to nodejs build system for requiring raw files(mustache files)

* chore(nodejs): bump version to v0.0.2-beta.1

* build(nodejs): fix mustache codegen
  • Loading branch information
Darmody authored Nov 8, 2023
1 parent af46c0b commit 34d449e
Show file tree
Hide file tree
Showing 15 changed files with 328 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ tmp/
var/**
!var/.gitkeep
.i18n-temp.json
.rollup.cache
3 changes: 2 additions & 1 deletion examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"ts-node": "^10.9.1"
},
"devDependencies": {
"@roarr/cli": "^5.12.3"
"@roarr/cli": "^5.12.3",
"tsconfig-paths": "^4.2.0"
}
}
20 changes: 20 additions & 0 deletions examples/nodejs/raw-loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path')
const fs = require('fs')
const REQUIRE_PATH_TEST = /(?:\?|&)raw(?:&|$)/

function register() {
const Module = require('module')
const orginalLoad = Module._load
const cwd = process.cwd()
Module._load = function _load(request, _parent) {
if (request.match(REQUIRE_PATH_TEST)) {
return fs.readFileSync(path.join(path.dirname(_parent ? _parent.filename : cwd), request.split('?')[0]), 'utf8')
}
return orginalLoad.apply(this, arguments)
}

return () => {
Module._load = orginalLoad
}
}
register()
5 changes: 4 additions & 1 deletion examples/nodejs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "../../tools/typescript/tsconfig.cjs.json"
"extends": "../../tools/typescript/tsconfig.cjs.json",
"ts-node": {
"require": ["tsconfig-paths/register", "./raw-loader.js"]
}
}
9 changes: 6 additions & 3 deletions packages/nodejs/bin/prepublish
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/bin/bash

npm pkg set 'exports['.'].types'='./dist/index.d.ts'
npm pkg set 'exports['.'].default'='./dist/index.js'
npm pkg set 'main'='./dist/index.js'
npm pkg set 'types'='./dist/index.d.ts'
npm pkg set 'exports['.'].require'='./dist/index.cjs.js'
npm pkg set 'exports['.'].import'='./dist/index.esm.js'
npm pkg set 'main'='./dist/index.cjs.js'
npm pkg set 'module'='./dist/index.esm.js'
npm pkg set 'types'='./dist/index.d.ts'
npm pkg set 'typings'='./dist/index.d.ts'
7 changes: 7 additions & 0 deletions packages/nodejs/externals.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
declare module '@roarr/middleware-serialize-error'

declare module 'rollup-plugin-string'

declare module '*?raw' {
const content: string
export default content
}
12 changes: 8 additions & 4 deletions packages/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
{
"name": "@pleisto/node-flappy",
"version": "0.0.2-beta.0",
"version": "0.0.2-beta.1",
"license": "Apache-2.0",
"exports": {
".": {
"types": "./src/index.ts",
"default": "./src/index.ts"
"require": "./src/index.ts",
"import": "./src/index.ts"
}
},
"main": "./src/index.ts",
"types": "./src/index.ts",
"typings": "./src/index.ts",
"module": "./src/index.ts",
"files": [
"README.md",
"dist"
],
"scripts": {
"build": "yarn clean && yarn run -T tsc -p tsconfig.build.json",
"build": "yarn clean && yarn rollup --config rollup.config.ts --configPlugin typescript",
"clean": "rm -rf ./dist",
"test": "vitest",
"lint": "yarn lint:eslint && yarn lint:type",
Expand All @@ -27,7 +30,6 @@
"dependencies": {
"@pleisto/flappy-nodejs-bindings": "next",
"@roarr/middleware-serialize-error": "^1.0.0",
"glob": "^10.3.3",
"mustache": "^4.2.0",
"radash": "^11.0.0",
"roarr": "^7.15.1",
Expand All @@ -40,9 +42,11 @@
"openai": "^4.12.4"
},
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.5",
"@types/mustache": "^4",
"@vitest/coverage-v8": "^0.34.6",
"openai": "^4.12.4",
"rollup": "^4.3.0",
"vitest": "^0.34.6"
}
}
29 changes: 29 additions & 0 deletions packages/nodejs/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import typescript from '@rollup/plugin-typescript'
import { raw } from './rollup/raw'

const config = [
{
input: 'src/index.ts',
output: [
{
file: './dist/index.esm.js',
format: 'es',
sourcemap: true
},
{
file: './dist/index.cjs.js',
format: 'cjs',
sourcemap: true,
interop: 'auto'
}
],
plugins: [
typescript({
tsconfig: './tsconfig.build.json',
sourceMap: true
}),
raw({ extensions: ['mustache'] })
]
}
]
export default config
31 changes: 31 additions & 0 deletions packages/nodejs/rollup/raw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const rawRE = /(?:\?|&)raw(?:&|$)/

export function raw({ extensions }) {
return {
name: 'raw',

resolveId(source) {
if (rawRE.test(source)) {
return source.split('?')[0]
}
return null
},

load(id) {
if (rawRE.test(id)) {
return id.split('?')[0]
}

return null
},

transform(code, id) {
if (extensions.includes(id.split('.').pop())) {
return {
code: `export default ${JSON.stringify(code)};`,
map: { mappings: '' }
}
}
}
}
}
17 changes: 1 addition & 16 deletions packages/nodejs/src/renderer/renderer.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
import { type TemplateMap } from './mustacheTypes'
import { globSync } from 'glob'
import path from 'path'
import fs from 'fs'
import Mustache from 'mustache'

const TemplateFolderPath = './templates'
const MUSTACHE_EXTENSION = '.mustache'

const files = globSync(path.join(TemplateFolderPath, `**/*${MUSTACHE_EXTENSION}`))

if (files.length === 0) throw new Error('template not found')

const contents = files.map(f => fs.readFileSync(f, { encoding: 'utf8' }))

const templates = Object.fromEntries(
files.map((f, i) => [path.relative(TemplateFolderPath, f).replace(MUSTACHE_EXTENSION, ''), contents[i]])
)
import { templates } from './templates'

export type TemplateRenderer = <K extends keyof TemplateMap>(templateName: K, params: TemplateMap[K]) => string

Expand Down
20 changes: 20 additions & 0 deletions packages/nodejs/src/renderer/templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import agentSystemMessage from '../../templates/agent/systemMessage.mustache?raw'
import agentUserMessage from '../../templates/agent/userMessage.mustache?raw'
import errorRetry from '../../templates/error/retry.mustache?raw'
import featuresCodeInterpreterDescription from '../../templates/features/codeInterpreter/description.mustache?raw'
import featuresCodeInterpreterEvalCode from '../../templates/features/codeInterpreter/evalCode.mustache?raw'
import featuresSynthesizedSystemMessage from '../../templates/features/synthesized/systemMessage.mustache?raw'
import featuresSynthesizedUserMessage from '../../templates/features/synthesized/userMessage.mustache?raw'
import testPing from '../../templates/test/ping.mustache?raw'
import { type TemplateMap } from './mustacheTypes'

export const templates: Record<keyof TemplateMap, string> = {
'agent/systemMessage': agentSystemMessage,
'agent/userMessage': agentUserMessage,
'error/retry': errorRetry,
'features/codeInterpreter/description': featuresCodeInterpreterDescription,
'features/codeInterpreter/evalCode': featuresCodeInterpreterEvalCode,
'features/synthesized/systemMessage': featuresSynthesizedSystemMessage,
'features/synthesized/userMessage': featuresSynthesizedUserMessage,
'test/ping': testPing
}
6 changes: 4 additions & 2 deletions packages/nodejs/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"target": "ESNext",
"module": "ESNext",
"declaration": true,
"noEmit": false,
"outDir": "dist",
"importHelpers": false
"importHelpers": false,
"incremental": false
}
}
3 changes: 2 additions & 1 deletion packages/nodejs/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "../../tools/typescript/tsconfig.cjs.json",
"include": ["./src/**/*", "./externals.d.ts"]
"include": ["./src/**/*", "./templates/**/*", "./externals.d.ts"],
"exclude": ["./src/**/*.test.ts"]
}
4 changes: 2 additions & 2 deletions tools/devkit/src/commands/ts-mustache-codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const handler: CommandModule['handler'] = async (): Promise<void> => {

// Generate typedefs
const declarer = new Declarer(loader)
const types = await declarer.declare()
const data = fs.readFileSync(TargetPath).toString()
const types = (await declarer.declare()).trim()
const data = fs.readFileSync(TargetPath, 'utf-8').toString().trim()

if (isCI) {
if (data !== types) exit(1)
Expand Down
Loading

0 comments on commit 34d449e

Please sign in to comment.