Skip to content

Commit

Permalink
refactor(xml): build support
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Dec 3, 2023
1 parent 672990c commit bbf5e18
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 79 deletions.
2 changes: 1 addition & 1 deletion packages/core/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineBuildConfig } from 'unbuild';

import { UnbuildPreset } from '@epubook/xml';
import { UnbuildPreset } from '@epubook/xml/unbuild';

export default defineBuildConfig({
entries: ['src/index'],
Expand Down
19 changes: 2 additions & 17 deletions packages/core/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
import { defineConfig } from 'vitest/config';

import { transform } from 'esbuild';
import { Epubook } from '@epubook/xml/vite';

export default defineConfig({
plugins: [
{
name: 'epubook:transform-jsx',
enforce: 'pre',
async transform(code, id) {
if (id.endsWith('.tsx')) {
return await transform(code, {
loader: 'tsx',
sourcefile: id,
jsxFactory: 'h',
jsxFragment: 'Fragment'
});
}
}
}
]
plugins: [Epubook()]
});
2 changes: 1 addition & 1 deletion packages/theme-default/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineBuildConfig } from 'unbuild';

import { UnbuildPreset } from '@epubook/xml';
import { UnbuildPreset } from '@epubook/xml/unbuild';

export default defineBuildConfig({
entries: ['src/index'],
Expand Down
4 changes: 2 additions & 2 deletions packages/xml/build.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { defineBuildConfig } from 'unbuild';

export default defineBuildConfig({
entries: ['src/index'],
entries: ['src/index', 'src/unbuild', 'src/vite'],
declaration: true,
clean: true,
externals: ['unbuild', 'rollup'],
externals: ['unbuild', 'rollup', 'vite'],
rollup: {
emitCJS: true
}
Expand Down
19 changes: 18 additions & 1 deletion packages/xml/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"ebook",
"epub",
"epub-generator",
"epubook"
"epubook",
"xml"
],
"homepage": "https://github.com/yjl9903/epubook#readme",
"bugs": {
Expand All @@ -24,11 +25,27 @@
"require": "./dist/index.cjs",
"import": "./dist/index.mjs",
"types": "./dist/index.d.ts"
},
"./unbuild": {
"import": "./dist/unbuild.mjs",
"types": "./dist/unbuild.d.ts"
},
"./vite": {
"import": "./dist/vite.mjs",
"types": "./dist/vite.d.ts"
}
},
"main": "./dist/index.cjs",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"typesVersions": {
"*": {
"*": [
"./dist/*",
"./*"
]
}
},
"files": [
"dist"
],
Expand Down
4 changes: 1 addition & 3 deletions packages/xml/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from './xhtml';

export * from './unbuild';
export * from './xml';
2 changes: 1 addition & 1 deletion packages/xml/src/unbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const UnbuildPreset: (options?: { inject?: boolean }) => BuildConfig = ({
rollup: {
esbuild: {
jsxFactory: inject ? '__epubook_xml.h' : 'h',
jsxFragment: inject ? '__epubook_xml.Fragment' : 'Fragment'
jsxFragment: inject ? '__epubook_xml.Fragment' : 'Fragment',
}
},
hooks: {
Expand Down
16 changes: 16 additions & 0 deletions packages/xml/src/vite.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { Plugin } from 'vite';

export function Epubook(): Plugin {
return {
name: 'epubook:jsx',
config() {
return {
esbuild: {
jsxFactory: '__epubook_xml.h',
jsxFragment: '__epubook_xml.Fragment',
jsxInject: `import * as __epubook_xml from '@epubook/xml'`
}
};
}
};
}
42 changes: 0 additions & 42 deletions packages/xml/src/xhtml/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { randomUUID } from 'node:crypto';
import * as path from 'pathe';
import { XMLBuilder } from 'fast-xml-parser';

import type { XHTMLNode } from './types';
import type { XMLNode } from './types';

import { Fragment } from './render';

Expand All @@ -27,9 +27,9 @@ export class XHTMLBuilder {

private _filename: string;

private _head: XHTMLNode[] = [];
private _head: XMLNode[] = [];

private _body: XHTMLNode[] = [];
private _body: XMLNode[] = [];

private _bodyAttrs: Record<string, string> = {};

Expand Down Expand Up @@ -63,12 +63,12 @@ export class XHTMLBuilder {
return this;
}

public head(...node: XHTMLNode[]) {
public head(...node: XMLNode[]) {
this._head.push(...node);
return this;
}

public body(...node: XHTMLNode[]) {
public body(...node: XMLNode[]) {
this._body.push(...node);
return this;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ export class XHTMLBuilder {
content
};

function build(node: XHTMLNode) {
function build(node: XMLNode) {
const attrs = Object.fromEntries(
Object.entries(node.attrs ?? {}).map(([key, value]) => ['@_' + key, value])
);
Expand All @@ -128,7 +128,7 @@ export class XHTMLBuilder {

if (Array.isArray(node.children)) {
const text = node.children.filter((c): c is string => typeof c === 'string');
const nodes = node.children.filter((c): c is XHTMLNode => typeof c !== 'string');
const nodes = node.children.filter((c): c is XMLNode => typeof c !== 'string');
if (text.length > 0) {
obj['#text'] = text[0];
}
Expand All @@ -139,7 +139,7 @@ export class XHTMLBuilder {
}
}

function list(list: XHTMLNode[]) {
function list(list: XMLNode[]) {
const obj: any = {};
const nodes = list.flatMap((n) => (n.tag === Fragment ? n.children ?? [] : [n]));
for (const c of nodes) {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { XHTMLNode } from './types';
import type { XMLNode } from './types';

export const Fragment = 'Fragment';

export function h(
tag: string,
attrs: Record<string, string> = {},
...children: Array<string | XHTMLNode | Array<string | XHTMLNode>>
...children: Array<string | XMLNode | Array<string | XMLNode>>
) {
const sub = children
.flatMap((c) =>
Expand All @@ -17,7 +17,7 @@ export function h(
tag,
attrs: attrs ?? {},
children: sub
} satisfies XHTMLNode;
} satisfies XMLNode;

return o;
}
58 changes: 58 additions & 0 deletions packages/xml/src/xml/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
declare global {
namespace JSX {
interface IntrinsicElements extends HtmlIntrinsicElements {}
}
}

export interface XMLNode {
tag: string;
attrs?: Record<string, string>;
children?: Array<string | XMLNode>;
}

export interface XHTMLNode extends XMLNode {}

export interface ContainerIntrinsicElements {
containter: {
version: string;
xmlns: string;
};

rootfiles: {};

rootfile: {
fullPath: string;
mediaType: string;
};
}

export interface BaseHtmlElementAttrs {
class?: string;

id?: string;

html?: string;
}

export interface HtmlIntrinsicElements {
div: BaseHtmlElementAttrs & {};

nav: BaseHtmlElementAttrs & {};
ul: BaseHtmlElementAttrs & {};
ol: BaseHtmlElementAttrs & {};
li: BaseHtmlElementAttrs & {};

h1: BaseHtmlElementAttrs & {};
h2: BaseHtmlElementAttrs & {};
h3: BaseHtmlElementAttrs & {};
h4: BaseHtmlElementAttrs & {};
h5: BaseHtmlElementAttrs & {};
h6: BaseHtmlElementAttrs & {};

p: BaseHtmlElementAttrs & {};
pre: BaseHtmlElementAttrs & {};

span: BaseHtmlElementAttrs & {};
code: BaseHtmlElementAttrs & {};
a: BaseHtmlElementAttrs & { href: string };
}
7 changes: 7 additions & 0 deletions packages/xml/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config';

import { Epubook } from './src/vite';

export default defineConfig({
plugins: [Epubook()]
});

0 comments on commit bbf5e18

Please sign in to comment.