mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 13:01:31 +08:00 
			
		
		
		
	refactor(share): remove experiment to precompile EJS
This commit is contained in:
		
							parent
							
								
									828da2aabf
								
							
						
					
					
						commit
						54ffeeaed9
					
				| @ -1,6 +1,5 @@ | |||||||
| import fs from "node:fs"; | import fs from "node:fs"; | ||||||
| import path from "node:path"; | import path from "node:path"; | ||||||
| import ejsPlugin from "./esbuild-ejs-plugin.js"; |  | ||||||
| // import {fileURLToPath} from "node:url";
 | // import {fileURLToPath} from "node:url";
 | ||||||
| 
 | 
 | ||||||
| import dotenv from "dotenv"; | import dotenv from "dotenv"; | ||||||
| @ -58,9 +57,6 @@ async function runBuild() { | |||||||
|         outdir: path.join(rootDir, "dist"), |         outdir: path.join(rootDir, "dist"), | ||||||
|         format: "cjs", |         format: "cjs", | ||||||
|         target: ["chrome96"], |         target: ["chrome96"], | ||||||
|         plugins: [ |  | ||||||
|             ejsPlugin() |  | ||||||
|         ], |  | ||||||
|         loader: { |         loader: { | ||||||
|             ".png": "dataurl", |             ".png": "dataurl", | ||||||
|             ".gif": "dataurl", |             ".gif": "dataurl", | ||||||
|  | |||||||
| @ -1,85 +0,0 @@ | |||||||
| import { readFile } from 'fs/promises'; |  | ||||||
| import { compile, type Options } from 'ejs'; |  | ||||||
| import { resolve, dirname, basename } from 'path'; |  | ||||||
| 
 |  | ||||||
| export default function esbuildPluginEjs(options = {}) { |  | ||||||
|     return { |  | ||||||
|         name: 'ejs', |  | ||||||
|         setup(build) { |  | ||||||
|             build.onLoad({ filter: /\.ejs$/ }, async args => { |  | ||||||
|                 const ejsOptions: Options = { |  | ||||||
|                     ...options, |  | ||||||
|                     client: true, |  | ||||||
|                     strict: true, |  | ||||||
|                     compileDebug: false |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 const contents: string[] = []; |  | ||||||
|                 contents.push(`const includeMap = {}`); |  | ||||||
|                 contents.push(` |  | ||||||
|                     function __include(name, ...args) { |  | ||||||
|                         return includeMap[name](...args); |  | ||||||
|                     } |  | ||||||
|                 `);
 |  | ||||||
| 
 |  | ||||||
|                 let main; |  | ||||||
| 
 |  | ||||||
|                 // Compile the subtemplates.
 |  | ||||||
|                 const subtemplates = await collectTemplateTree(args.path); |  | ||||||
|                 for (const [ path, subtemplate ] of Object.entries(subtemplates)) { |  | ||||||
|                     const functionName = basename(path).split(".")[0]; |  | ||||||
|                     const isMain = (path === args.path); |  | ||||||
| 
 |  | ||||||
|                     const generator = compile(subtemplate, ejsOptions); |  | ||||||
|                     const functionOutput = generator.toString().split("\n"); |  | ||||||
| 
 |  | ||||||
|                     if (isMain) { |  | ||||||
|                         functionOutput[0] = functionOutput[0].replace(/^function anonymous/, `module.exports = function`); |  | ||||||
|                     } else { |  | ||||||
|                         functionOutput[0] = functionOutput[0].replace(/^function anonymous/, `includeMap["${functionName}"] = function`); |  | ||||||
|                     } |  | ||||||
| 
 |  | ||||||
|                     // Inject include function.
 |  | ||||||
|                     functionOutput[2] = `include = __include;\n${functionOutput[2]}`; |  | ||||||
| 
 |  | ||||||
|                     if (isMain) { |  | ||||||
|                         main = functionOutput.join("\n"); |  | ||||||
|                     } else { |  | ||||||
|                         contents.push(functionOutput.join("\n")); |  | ||||||
|                     } |  | ||||||
|                 } |  | ||||||
| 
 |  | ||||||
|                 // Compile the rest.
 |  | ||||||
|                 if (!main) { |  | ||||||
|                     throw new Error("Missing main entry point"); |  | ||||||
|                 } |  | ||||||
|                 contents.push(main); |  | ||||||
| 
 |  | ||||||
|                 return { contents: contents.join("\n"), loader: 'js' } |  | ||||||
|             }) |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| 
 |  | ||||||
| const includeRegex = /<%-?\s*include\((['"`])(.+?)\1\s*(?:,[^)]+)?\)\s*-?%>/g; |  | ||||||
| 
 |  | ||||||
| async function collectTemplateTree(filePath, seen: Record<string, string> = {}) { |  | ||||||
|     if (seen[filePath]) return; |  | ||||||
| 
 |  | ||||||
|     const source = await readFile(filePath, 'utf8'); |  | ||||||
|     seen[filePath] = source; |  | ||||||
| 
 |  | ||||||
|     const dir = dirname(filePath); |  | ||||||
| 
 |  | ||||||
|     const matches = [...source.matchAll(includeRegex)]; |  | ||||||
|     for (const match of matches) { |  | ||||||
|         const includePath = match[2]; |  | ||||||
| 
 |  | ||||||
|         // Add .ejs extension if needed
 |  | ||||||
|         const resolvedPath = resolve(dir, includePath.endsWith('.ejs') ? includePath : includePath + '.ejs'); |  | ||||||
|         await collectTemplateTree(resolvedPath, seen); |  | ||||||
|     } |  | ||||||
| 
 |  | ||||||
|     return seen; |  | ||||||
| } |  | ||||||
| @ -1,9 +1,5 @@ | |||||||
| <!DOCTYPE html> | <!DOCTYPE html> | ||||||
| <html lang="en"> | <html lang="en"> | ||||||
| <% |  | ||||||
|     const { note, subRoot, assetPath, appPath } = locals; |  | ||||||
|     let content = locals.content; |  | ||||||
| %> |  | ||||||
| <head> | <head> | ||||||
|     <meta charset="utf-8"> |     <meta charset="utf-8"> | ||||||
|     <meta name="viewport" content="width=device-width, initial-scale=1"> |     <meta name="viewport" content="width=device-width, initial-scale=1"> | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Elian Doran
						Elian Doran