Trying to upgrade docx to use Vite

This commit is contained in:
Dolan Miu
2023-05-01 20:37:39 +01:00
parent 18b2e91da2
commit a26292a0fd
20 changed files with 1178 additions and 312 deletions

View File

@ -1,5 +1,5 @@
import * as JSZip from "jszip";
import * as xml from "xml";
import JSZip from "jszip";
import xml from "xml";
import { File } from "@file/file";
@ -44,7 +44,7 @@ export class Compiler {
this.numberingReplacer = new NumberingReplacer();
}
public compile(file: File, prettifyXml?: boolean | PrettifyType): JSZip {
public compile(file: File, prettifyXml?: PrettifyType): JSZip {
const zip = new JSZip();
const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml);
const map = new Map<string, IXmlifyedFile | readonly IXmlifyedFile[]>(Object.entries(xmlifiedFileMapping));
@ -66,7 +66,7 @@ export class Compiler {
return zip;
}
private xmlifyFile(file: File, prettify?: boolean | PrettifyType): IXmlifyedFileMapping {
private xmlifyFile(file: File, prettify?: PrettifyType): IXmlifyedFileMapping {
const documentRelationshipCount = file.Document.Relationships.RelationshipCount + 1;
const documentXmlData = xml(

View File

@ -15,7 +15,7 @@ export enum PrettifyType {
export class Packer {
public static async toString(file: File, prettify?: boolean | PrettifyType): Promise<string> {
const zip = this.compiler.compile(file, prettify);
const zip = this.compiler.compile(file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify);
const zipData = await zip.generateAsync({
type: "string",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -26,7 +26,7 @@ export class Packer {
}
public static async toBuffer(file: File, prettify?: boolean | PrettifyType): Promise<Buffer> {
const zip = this.compiler.compile(file, prettify);
const zip = this.compiler.compile(file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify);
const zipData = await zip.generateAsync({
type: "nodebuffer",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -37,7 +37,7 @@ export class Packer {
}
public static async toBase64String(file: File, prettify?: boolean | PrettifyType): Promise<string> {
const zip = this.compiler.compile(file, prettify);
const zip = this.compiler.compile(file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify);
const zipData = await zip.generateAsync({
type: "base64",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -48,7 +48,7 @@ export class Packer {
}
public static async toBlob(file: File, prettify?: boolean | PrettifyType): Promise<Blob> {
const zip = this.compiler.compile(file, prettify);
const zip = this.compiler.compile(file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify);
const zipData = await zip.generateAsync({
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
@ -59,7 +59,7 @@ export class Packer {
}
public static toStream(file: File, prettify?: boolean | PrettifyType): Stream {
const zip = this.compiler.compile(file, prettify);
const zip = this.compiler.compile(file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify);
const zipData = zip.generateNodeStream({
type: "nodebuffer",
streamFiles: true,

View File

@ -44,7 +44,8 @@ describe("Properties", () => {
expect(Object.keys(tree)).to.deep.equal(["cp:coreProperties"]);
expect(tree["cp:coreProperties"]).to.be.an.instanceof(Array);
const key = (obj) => Object.keys(obj)[0];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const key = (obj: { readonly [key: string]: any }) => Object.keys(obj)[0];
expect(tree["cp:coreProperties"].map(key)).to.include.members([
"_attr",
"cp:keywords",

View File

@ -25,7 +25,8 @@ describe("Numbering", () => {
const tree = new Formatter().format(numbering);
expect(Object.keys(tree)).to.deep.equal(["w:numbering"]);
const abstractNums = tree["w:numbering"].filter((el) => el["w:abstractNum"]);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const abstractNums: readonly any[] = tree["w:numbering"].filter((el) => el["w:abstractNum"]);
expect(abstractNums).to.have.lengthOf(1);
expect(abstractNums[0]["w:abstractNum"]).to.deep.include.members([
{ _attr: { "w:abstractNumId": 0, "w15:restartNumberingAfterBreak": 0 } },

View File

@ -29,7 +29,8 @@ export interface INumberingOptions {
export class Numbering extends XmlComponent {
private readonly abstractNumberingMap = new Map<string, AbstractNumbering>();
private readonly concreteNumberingMap = new Map<string, ConcreteNumbering>();
private readonly referenceConfigMap = new Map<string, object>();
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private readonly referenceConfigMap = new Map<string, Record<string, any>>();
public constructor(options: INumberingOptions) {
super("w:numbering");
@ -231,7 +232,8 @@ export class Numbering extends XmlComponent {
public get ConcreteNumbering(): readonly ConcreteNumbering[] {
return Array.from(this.concreteNumberingMap.values());
}
public get ReferenceConfig(): readonly object[] {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
public get ReferenceConfig(): readonly Record<string, any>[] {
return Array.from(this.referenceConfigMap.values());
}
}

View File

@ -3,7 +3,7 @@ import { expect } from "chai";
import { ExternalStylesFactory } from "./external-styles-factory";
describe("External styles factory", () => {
let externalStyles;
let externalStyles: string;
beforeEach(() => {
externalStyles = `

View File

@ -14,11 +14,14 @@ export abstract class XmlAttributeComponent<T extends object> extends BaseXmlCom
}
public prepForXml(_: IContext): IXmlableObject {
const attrs = {};
// eslint-disable-next-line functional/prefer-readonly-type
const attrs: { [key: string]: string } = {};
Object.keys(this.root).forEach((key) => {
const value = this.root[key];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const value = (this.root as any)[key];
if (value !== undefined) {
const newKey = (this.xmlKeys && this.xmlKeys[key]) || key;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const newKey = (this.xmlKeys && (this.xmlKeys as any)[key]) || key;
// eslint-disable-next-line functional/immutable-data
attrs[newKey] = value;
}

View File

@ -2,7 +2,9 @@ export interface IXmlAttribute {
readonly [key: string]: string | number | boolean;
}
export interface IXmlableObject extends Object {
readonly _attr?: IXmlAttribute;
// readonly _attr?: IXmlAttribute;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
readonly [key: string]: any;
}
// Needed because of: https://github.com/s-panferov/awesome-typescript-loader/issues/432

View File

@ -1,7 +1,7 @@
import * as chai from "chai";
import * as sinon from "sinon";
import * as JSZip from "jszip";
import * as chaiAsPromised from "chai-as-promised";
import JSZip from "jszip";
import chaiAsPromised from "chai-as-promised";
import { ExternalHyperlink, ImageRun, Paragraph, TextRun } from "@file/paragraph";

View File

@ -1,4 +1,4 @@
import * as JSZip from "jszip";
import JSZip from "jszip";
import { Element, js2xml } from "xml-js";
import { ConcreteHyperlink, ExternalHyperlink, ParagraphChild } from "@file/paragraph";

View File

@ -1,5 +1,5 @@
import { Element } from "xml-js";
import * as xml from "xml";
import xml from "xml";
import { Formatter } from "@export/formatter";
import { IContext, XmlComponent } from "@file/xml-components";

View File

@ -1,5 +1,5 @@
import { xml2js, Element } from "xml-js";
import * as xml from "xml";
import xml from "xml";
import { Formatter } from "@export/formatter";
import { Text } from "@file/paragraph/run/run-components/text";