mirror of
https://github.com/QwenLM/qwen-code.git
synced 2026-04-29 04:00:36 +00:00
refactor(export): built-in package for assets export
1. use built-in assets package to support html export 2. improve markdown export
This commit is contained in:
parent
a4630d39e4
commit
86a43618a7
16 changed files with 819 additions and 405 deletions
71
packages/cli/assets/export-html/build.mjs
Normal file
71
packages/cli/assets/export-html/build.mjs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
import { readFile, writeFile, mkdir } from 'node:fs/promises';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { dirname, join } from 'node:path';
|
||||
import { build } from 'esbuild';
|
||||
import { buildConfig } from './esbuild.config.mjs';
|
||||
|
||||
const assetsDir = dirname(fileURLToPath(import.meta.url));
|
||||
const srcDir = join(assetsDir, 'src');
|
||||
const assetsDistDir = join(assetsDir, 'dist');
|
||||
const packageDistDir = join(
|
||||
assetsDir,
|
||||
'..',
|
||||
'..',
|
||||
'dist',
|
||||
'assets',
|
||||
'export-html',
|
||||
);
|
||||
const templateModulePath = join(
|
||||
assetsDir,
|
||||
'..',
|
||||
'..',
|
||||
'src',
|
||||
'ui',
|
||||
'utils',
|
||||
'export',
|
||||
'formatters',
|
||||
'htmlTemplate.ts',
|
||||
);
|
||||
|
||||
await mkdir(assetsDistDir, { recursive: true });
|
||||
await mkdir(packageDistDir, { recursive: true });
|
||||
|
||||
const buildResult = await build(buildConfig);
|
||||
|
||||
const jsBundle = buildResult.outputFiles.find((file) =>
|
||||
file.path.endsWith('.js'),
|
||||
);
|
||||
const cssBundle = buildResult.outputFiles.find((file) =>
|
||||
file.path.endsWith('.css'),
|
||||
);
|
||||
if (!jsBundle) {
|
||||
throw new Error('Failed to generate inline script bundle.');
|
||||
}
|
||||
|
||||
const css = cssBundle
|
||||
? cssBundle.text
|
||||
: await readFile(join(srcDir, 'styles.css'), 'utf8');
|
||||
const htmlTemplate = await readFile(join(srcDir, 'index.html'), 'utf8');
|
||||
const faviconSvg = await readFile(join(srcDir, 'favicon.svg'), 'utf8');
|
||||
const faviconData = encodeURIComponent(faviconSvg.trim());
|
||||
|
||||
const htmlOutput = htmlTemplate
|
||||
.replace('__INLINE_CSS__', css.trim())
|
||||
.replace('__INLINE_SCRIPT__', jsBundle.text.trim())
|
||||
.replace('__FAVICON_SVG__', faviconSvg.trim())
|
||||
.replace('__FAVICON_DATA__', faviconData);
|
||||
|
||||
const templateModule = `/**
|
||||
* @license
|
||||
* Copyright 2025 Qwen Team
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* This HTML template is code-generated; do not edit manually.
|
||||
*/
|
||||
|
||||
export const HTML_TEMPLATE = ${JSON.stringify(htmlOutput)};
|
||||
`;
|
||||
|
||||
await writeFile(join(assetsDistDir, 'index.html'), htmlOutput);
|
||||
await writeFile(join(packageDistDir, 'index.html'), htmlOutput);
|
||||
await writeFile(templateModulePath, templateModule);
|
||||
Loading…
Add table
Add a link
Reference in a new issue