Skip to content

Commit

Permalink
chore: remove unnessary plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Apr 2, 2024
1 parent c2f67ff commit f6048ab
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 228 deletions.
140 changes: 0 additions & 140 deletions .yarn/patches/esbuild-minify-templates-npm-0.11.0-458ab522a4.patch

This file was deleted.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
"@vitejs/plugin-react": "^4.2.1",
"ava": "^5.3.1",
"c8": "^8.0.1",
"esbuild-minify-templates": "^0.11.0",
"eslint": "^8.49.0",
"eslint-config-kagura": "^2.1.1",
"foxact": "^0.2.29",
Expand Down Expand Up @@ -108,8 +107,7 @@
"unbox-primitive": "npm:@nolyfill/unbox-primitive@latest",
"which-boxed-primitive": "npm:@nolyfill/which-boxed-primitive@latest",
"which-typed-array": "npm:@nolyfill/which-typed-array@latest",
"@types/react": "^18.2.31",
"esbuild-minify-templates@^0.11.0": "patch:esbuild-minify-templates@npm%3A0.11.0#./.yarn/patches/esbuild-minify-templates-npm-0.11.0-458ab522a4.patch"
"@types/react": "^18.2.31"
},
"dependencies": {
"picocolors": "^1.0.0",
Expand Down
44 changes: 34 additions & 10 deletions src/server/render.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
import fsp from 'fs/promises'
import path from 'path'
import { clientAssetsPath, clientPath, injectHTMLTag, readAll } from './shared'
import { clientAssetsPath, clientPath, readAll } from './shared'
import type { DefaultSizes, Foam } from './interface'

export interface RenderOptions {
title: string
mode: DefaultSizes
}

interface Descriptor {
kind: 'script' | 'style',
text: string
}

interface InjectHTMLTagOptions {
html: string
injectTo: 'body' | 'head',
descriptors: Descriptor | Descriptor[]
}

// Refactor this function
export function injectHTMLTag(options: InjectHTMLTagOptions) {
const regExp = options.injectTo === 'head' ? /([ \t]*)<\/head>/i : /([ \t]*)<\/body>/i
options.descriptors = Array.isArray(options.descriptors) ? options.descriptors : [options.descriptors]
const descriptors = options.descriptors.map(d => `<${d.kind}>${d.text}</${d.kind}>`)
return options.html.replace(regExp, (match) => `${descriptors.join('\n')}${match}`)
}

export function generateInjectCode(foamModule: Foam[], mode: string) {
const { stringify } = JSON
return `window.defaultSizes = ${stringify(mode)},window.foamModule = ${stringify(foamModule)};`
}

export async function renderView(foamModule: Foam[], options: RenderOptions) {
const clientAssetsPaths = await readAll(clientAssetsPath)
const clientAssets = await Promise.all(clientAssetsPaths.map(async (p) => {
Expand All @@ -24,16 +48,16 @@ export async function renderView(foamModule: Foam[], options: RenderOptions) {
html = injectHTMLTag({
html,
injectTo: 'head',
descriptors: assets.map(({ fileType, content }) => {
if (fileType === 'js') return `<script>${content}</script>`
return `<style>${content}</style>`
}) })
html = injectHTMLTag({
descriptors: assets.map(({ fileType, content }) =>
({ kind: fileType === 'js' ? 'script' : 'style', text: content }))
})
html = injectHTMLTag({
html,
injectTo: 'body',
descriptors: [`<script>
window.defaultSizes = ${JSON.stringify(options.mode)};\n
window.foamModule = ${JSON.stringify(foamModule)};\n
</script>`] })
descriptors: {
kind: 'script',
text: generateInjectCode(foamModule, options.mode)
}
})
return html
}
11 changes: 6 additions & 5 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import http from 'http'
import fs from 'fs'
import type { AddressInfo } from 'net'
import path from 'path'
import { clientPath, injectHTMLTag } from './shared'
import { generateInjectCode, injectHTMLTag } from './render'
import { clientPath } from './shared'
import type { Foam } from './interface'
import type { RenderOptions } from './render'

Expand All @@ -29,10 +30,10 @@ function createStaticMiddleware(options: RenderOptions, foamModule: Foam[]) {
html = injectHTMLTag({
html,
injectTo: 'body',
descriptors: [`<script>
window.defaultSizes = ${JSON.stringify(options.mode)};\n
window.foamModule = ${JSON.stringify(foamModule)};\n
</script>`]
descriptors: {
kind: 'script',
text: generateInjectCode(foamModule, options.mode)
}
})
res.end(html)
cache.set(filePath, { data: html, mimeType: 'text/html; charset=utf8;' })
Expand Down
11 changes: 0 additions & 11 deletions src/server/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,6 @@ export function slash(path: string) {
return path.replace(/\\/g, '/')
}

interface InjectHTMLTagOptions {
html: string
injectTo: 'body' | 'head',
descriptors: string[]
}

export function injectHTMLTag(options: InjectHTMLTagOptions) {
const regExp = options.injectTo === 'head' ? /([ \t]*)<\/head>/i : /([ \t]*)<\/body>/i
return options.html.replace(regExp, (match) => `${options.descriptors.join('\n')}${match}`)
}

export function stringToByte(b: string | Uint8Array) {
if (typeof b === 'string') return new TextEncoder().encode(b)
return b
Expand Down
4 changes: 1 addition & 3 deletions tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import minify from 'esbuild-minify-templates'
import type { Options } from 'tsup'

export const tsup: Options = {
entry: ['src/server/index.ts'],
dts: true,
format: ['esm', 'cjs'],
shims: true,
minify: true,
esbuildPlugins: [minify.minifyTemplates(), minify.writeFiles()]
minify: true
}
57 changes: 1 addition & 56 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ __metadata:
languageName: node
linkType: hard

"@ampproject/remapping@npm:^2.2.0, @ampproject/remapping@npm:^2.2.1":
"@ampproject/remapping@npm:^2.2.0":
version: 2.2.1
resolution: "@ampproject/remapping@npm:2.2.1"
dependencies:
Expand Down Expand Up @@ -1190,13 +1190,6 @@ __metadata:
languageName: node
linkType: hard

"@types/estree@npm:^0.0.45":
version: 0.0.45
resolution: "@types/estree@npm:0.0.45"
checksum: 82c3eed5ab5b20ecd58a6d19176b9be0523c00a794db11f8ed08faee851066f09a959b88a1983ad52313f750e1cfe6b9565904ad2f4f7cf5c1f96300cde04ca0
languageName: node
linkType: hard

"@types/estree@npm:^1.0.0":
version: 1.0.3
resolution: "@types/estree@npm:1.0.3"
Expand Down Expand Up @@ -1903,18 +1896,6 @@ __metadata:
languageName: node
linkType: hard

"astray@npm:^1.1.1":
version: 1.1.1
resolution: "astray@npm:1.1.1"
dependencies:
"@types/estree": ^0.0.45
dependenciesMeta:
"@types/estree":
optional: true
checksum: 53ad4ac04d168f12a9274e057435bed9f02e6cfac2ef572b1966793a1b47625468726d625ef7e98d852da0e10a31ae71c8b59a39bd39a101487cf3d97d3cf527
languageName: node
linkType: hard

"ava@npm:^5.3.1":
version: 5.3.1
resolution: "ava@npm:5.3.1"
Expand Down Expand Up @@ -2618,34 +2599,6 @@ __metadata:
languageName: node
linkType: hard

"esbuild-minify-templates@npm:0.11.0":
version: 0.11.0
resolution: "esbuild-minify-templates@npm:0.11.0"
dependencies:
"@ampproject/remapping": ^2.2.1
astray: ^1.1.1
magic-string: ^0.30.0
meriyah: ^4.3.7
peerDependencies:
esbuild: ">=0.13.0 <1.0.0"
checksum: 22c653bb279ae190e5ff411b2c8fc1e38c65285426ac340d5b8f1c28ef57e9366040f7dd395dec0d8b499f704d1fd31c65e1c6cc6006b74203cf75ba8408def4
languageName: node
linkType: hard

"esbuild-minify-templates@patch:esbuild-minify-templates@npm%3A0.11.0#./.yarn/patches/esbuild-minify-templates-npm-0.11.0-458ab522a4.patch::locator=vite-bundle-analyzer%40workspace%3A.":
version: 0.11.0
resolution: "esbuild-minify-templates@patch:esbuild-minify-templates@npm%3A0.11.0#./.yarn/patches/esbuild-minify-templates-npm-0.11.0-458ab522a4.patch::version=0.11.0&hash=9894e6&locator=vite-bundle-analyzer%40workspace%3A."
dependencies:
"@ampproject/remapping": ^2.2.1
astray: ^1.1.1
magic-string: ^0.30.0
meriyah: ^4.3.7
peerDependencies:
esbuild: ">=0.13.0 <1.0.0"
checksum: cb64aaae1e7e605b3971c79ff9a4195ac9714a9fa86a531cd0c04deae46254b55f88fbf44e2739e6ef2cf3fb019dd556563a5a20cea45f436b3fe01b13e8cc47
languageName: node
linkType: hard

"esbuild@npm:^0.18.10, esbuild@npm:^0.18.2, esbuild@npm:~0.18.20":
version: 0.18.20
resolution: "esbuild@npm:0.18.20"
Expand Down Expand Up @@ -4076,13 +4029,6 @@ __metadata:
languageName: node
linkType: hard

"meriyah@npm:^4.3.7":
version: 4.3.9
resolution: "meriyah@npm:4.3.9"
checksum: eb43c4ca2295cbdaa6f4d13412f42fa97c968cbb3171899b1afcf5419715433b2906558aaabd08660592c5731d80341d81ce7bea7fd562942677c15a7bcbea4a
languageName: node
linkType: hard

"micromatch@npm:^4.0.4":
version: 4.0.5
resolution: "micromatch@npm:4.0.5"
Expand Down Expand Up @@ -5723,7 +5669,6 @@ __metadata:
"@vitejs/plugin-react": ^4.2.1
ava: ^5.3.1
c8: ^8.0.1
esbuild-minify-templates: ^0.11.0
eslint: ^8.49.0
eslint-config-kagura: ^2.1.1
foxact: ^0.2.29
Expand Down

0 comments on commit f6048ab

Please sign in to comment.