Convert to vite and clean up build
This commit is contained in:
@ -1,180 +1,180 @@
|
||||
/* tslint:disable:typedef space-before-function-paren */
|
||||
import { expect } from "chai";
|
||||
import * as sinon from "sinon";
|
||||
// /* tslint:disable:typedef space-before-function-paren */
|
||||
// import { expect } from "chai";
|
||||
// import * as sinon from "sinon";
|
||||
|
||||
import { File } from "@file/file";
|
||||
import { Footer, Header } from "@file/header";
|
||||
import { ImageRun, Paragraph } from "@file/paragraph";
|
||||
import * as convenienceFunctions from "@util/convenience-functions";
|
||||
// import { File } from "@file/file";
|
||||
// import { Footer, Header } from "@file/header";
|
||||
// import { ImageRun, Paragraph } from "@file/paragraph";
|
||||
// import * as convenienceFunctions from "@util/convenience-functions";
|
||||
|
||||
import { Compiler } from "./next-compiler";
|
||||
// import { Compiler } from "./next-compiler";
|
||||
|
||||
describe("Compiler", () => {
|
||||
let compiler: Compiler;
|
||||
// describe("Compiler", () => {
|
||||
// let compiler: Compiler;
|
||||
|
||||
beforeEach(() => {
|
||||
compiler = new Compiler();
|
||||
});
|
||||
// beforeEach(() => {
|
||||
// compiler = new Compiler();
|
||||
// });
|
||||
|
||||
before(() => {
|
||||
sinon.stub(convenienceFunctions, "uniqueId").callsFake(() => "test");
|
||||
});
|
||||
// before(() => {
|
||||
// sinon.stub(convenienceFunctions, "uniqueId").callsFake(() => "test");
|
||||
// });
|
||||
|
||||
after(() => {
|
||||
(convenienceFunctions.uniqueId as sinon.SinonStub).restore();
|
||||
});
|
||||
// after(() => {
|
||||
// (convenienceFunctions.uniqueId as sinon.SinonStub).restore();
|
||||
// });
|
||||
|
||||
describe("#compile()", () => {
|
||||
it("should pack all the content", function () {
|
||||
this.timeout(99999999);
|
||||
const file = new File({
|
||||
sections: [],
|
||||
comments: {
|
||||
children: [],
|
||||
},
|
||||
});
|
||||
const zipFile = compiler.compile(file);
|
||||
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
|
||||
// describe("#compile()", () => {
|
||||
// it("should pack all the content", async function () {
|
||||
// this.timeout(99999999);
|
||||
// const file = new File({
|
||||
// sections: [],
|
||||
// comments: {
|
||||
// children: [],
|
||||
// },
|
||||
// });
|
||||
// const zipFile = await compiler.compile(file);
|
||||
// const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
|
||||
|
||||
expect(fileNames).is.an.instanceof(Array);
|
||||
expect(fileNames).has.length(17);
|
||||
expect(fileNames).to.include("word/document.xml");
|
||||
expect(fileNames).to.include("word/styles.xml");
|
||||
expect(fileNames).to.include("docProps/core.xml");
|
||||
expect(fileNames).to.include("docProps/custom.xml");
|
||||
expect(fileNames).to.include("docProps/app.xml");
|
||||
expect(fileNames).to.include("word/numbering.xml");
|
||||
expect(fileNames).to.include("word/footnotes.xml");
|
||||
expect(fileNames).to.include("word/_rels/footnotes.xml.rels");
|
||||
expect(fileNames).to.include("word/settings.xml");
|
||||
expect(fileNames).to.include("word/comments.xml");
|
||||
expect(fileNames).to.include("word/_rels/document.xml.rels");
|
||||
expect(fileNames).to.include("[Content_Types].xml");
|
||||
expect(fileNames).to.include("_rels/.rels");
|
||||
});
|
||||
// expect(fileNames).is.an.instanceof(Array);
|
||||
// expect(fileNames).has.length(17);
|
||||
// expect(fileNames).to.include("word/document.xml");
|
||||
// expect(fileNames).to.include("word/styles.xml");
|
||||
// expect(fileNames).to.include("docProps/core.xml");
|
||||
// expect(fileNames).to.include("docProps/custom.xml");
|
||||
// expect(fileNames).to.include("docProps/app.xml");
|
||||
// expect(fileNames).to.include("word/numbering.xml");
|
||||
// expect(fileNames).to.include("word/footnotes.xml");
|
||||
// expect(fileNames).to.include("word/_rels/footnotes.xml.rels");
|
||||
// expect(fileNames).to.include("word/settings.xml");
|
||||
// expect(fileNames).to.include("word/comments.xml");
|
||||
// expect(fileNames).to.include("word/_rels/document.xml.rels");
|
||||
// expect(fileNames).to.include("[Content_Types].xml");
|
||||
// expect(fileNames).to.include("_rels/.rels");
|
||||
// });
|
||||
|
||||
it("should pack all additional headers and footers", function () {
|
||||
const file = new File({
|
||||
sections: [
|
||||
{
|
||||
headers: {
|
||||
default: new Header({
|
||||
children: [new Paragraph("test")],
|
||||
}),
|
||||
},
|
||||
footers: {
|
||||
default: new Footer({
|
||||
children: [new Paragraph("test")],
|
||||
}),
|
||||
},
|
||||
children: [],
|
||||
},
|
||||
{
|
||||
headers: {
|
||||
default: new Header({
|
||||
children: [new Paragraph("test")],
|
||||
}),
|
||||
},
|
||||
footers: {
|
||||
default: new Footer({
|
||||
children: [new Paragraph("test")],
|
||||
}),
|
||||
},
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
});
|
||||
// it("should pack all additional headers and footers", function () {
|
||||
// const file = new File({
|
||||
// sections: [
|
||||
// {
|
||||
// headers: {
|
||||
// default: new Header({
|
||||
// children: [new Paragraph("test")],
|
||||
// }),
|
||||
// },
|
||||
// footers: {
|
||||
// default: new Footer({
|
||||
// children: [new Paragraph("test")],
|
||||
// }),
|
||||
// },
|
||||
// children: [],
|
||||
// },
|
||||
// {
|
||||
// headers: {
|
||||
// default: new Header({
|
||||
// children: [new Paragraph("test")],
|
||||
// }),
|
||||
// },
|
||||
// footers: {
|
||||
// default: new Footer({
|
||||
// children: [new Paragraph("test")],
|
||||
// }),
|
||||
// },
|
||||
// children: [],
|
||||
// },
|
||||
// ],
|
||||
// });
|
||||
|
||||
this.timeout(99999999);
|
||||
// this.timeout(99999999);
|
||||
|
||||
const zipFile = compiler.compile(file);
|
||||
const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
|
||||
// const zipFile = compiler.compile(file);
|
||||
// const fileNames = Object.keys(zipFile.files).map((f) => zipFile.files[f].name);
|
||||
|
||||
expect(fileNames).is.an.instanceof(Array);
|
||||
expect(fileNames).has.length(25);
|
||||
// expect(fileNames).is.an.instanceof(Array);
|
||||
// expect(fileNames).has.length(25);
|
||||
|
||||
expect(fileNames).to.include("word/header1.xml");
|
||||
expect(fileNames).to.include("word/_rels/header1.xml.rels");
|
||||
expect(fileNames).to.include("word/header2.xml");
|
||||
expect(fileNames).to.include("word/_rels/header2.xml.rels");
|
||||
expect(fileNames).to.include("word/footer1.xml");
|
||||
expect(fileNames).to.include("word/_rels/footer1.xml.rels");
|
||||
expect(fileNames).to.include("word/footer2.xml");
|
||||
expect(fileNames).to.include("word/_rels/footer2.xml.rels");
|
||||
});
|
||||
// expect(fileNames).to.include("word/header1.xml");
|
||||
// expect(fileNames).to.include("word/_rels/header1.xml.rels");
|
||||
// expect(fileNames).to.include("word/header2.xml");
|
||||
// expect(fileNames).to.include("word/_rels/header2.xml.rels");
|
||||
// expect(fileNames).to.include("word/footer1.xml");
|
||||
// expect(fileNames).to.include("word/_rels/footer1.xml.rels");
|
||||
// expect(fileNames).to.include("word/footer2.xml");
|
||||
// expect(fileNames).to.include("word/_rels/footer2.xml.rels");
|
||||
// });
|
||||
|
||||
it("should call the format method X times equalling X files to be formatted", () => {
|
||||
// This test is required because before, there was a case where Document was formatted twice, which was inefficient
|
||||
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
|
||||
const paragraph = new Paragraph("");
|
||||
const file = new File({
|
||||
sections: [
|
||||
{
|
||||
properties: {},
|
||||
children: [paragraph],
|
||||
},
|
||||
],
|
||||
});
|
||||
// it("should call the format method X times equalling X files to be formatted", () => {
|
||||
// // This test is required because before, there was a case where Document was formatted twice, which was inefficient
|
||||
// // This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
|
||||
// const paragraph = new Paragraph("");
|
||||
// const file = new File({
|
||||
// sections: [
|
||||
// {
|
||||
// properties: {},
|
||||
// children: [paragraph],
|
||||
// },
|
||||
// ],
|
||||
// });
|
||||
|
||||
// tslint:disable-next-line: no-string-literal
|
||||
const spy = sinon.spy(compiler["formatter"], "format");
|
||||
// // tslint:disable-next-line: no-string-literal
|
||||
// const spy = sinon.spy(compiler["formatter"], "format");
|
||||
|
||||
compiler.compile(file);
|
||||
expect(spy.callCount).to.equal(13);
|
||||
});
|
||||
// compiler.compile(file);
|
||||
// expect(spy.callCount).to.equal(13);
|
||||
// });
|
||||
|
||||
it("should work with media datas", () => {
|
||||
// This test is required because before, there was a case where Document was formatted twice, which was inefficient
|
||||
// This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
|
||||
const file = new File({
|
||||
sections: [
|
||||
{
|
||||
headers: {
|
||||
default: new Header({
|
||||
children: [new Paragraph("test")],
|
||||
}),
|
||||
},
|
||||
footers: {
|
||||
default: new Footer({
|
||||
children: [new Paragraph("test")],
|
||||
}),
|
||||
},
|
||||
children: [
|
||||
new Paragraph({
|
||||
children: [
|
||||
new ImageRun({
|
||||
data: Buffer.from("", "base64"),
|
||||
transformation: {
|
||||
width: 100,
|
||||
height: 100,
|
||||
},
|
||||
}),
|
||||
],
|
||||
}),
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
// it("should work with media datas", () => {
|
||||
// // This test is required because before, there was a case where Document was formatted twice, which was inefficient
|
||||
// // This also caused issues such as running prepForXml multiple times as format() was ran multiple times.
|
||||
// const file = new File({
|
||||
// sections: [
|
||||
// {
|
||||
// headers: {
|
||||
// default: new Header({
|
||||
// children: [new Paragraph("test")],
|
||||
// }),
|
||||
// },
|
||||
// footers: {
|
||||
// default: new Footer({
|
||||
// children: [new Paragraph("test")],
|
||||
// }),
|
||||
// },
|
||||
// children: [
|
||||
// new Paragraph({
|
||||
// children: [
|
||||
// new ImageRun({
|
||||
// data: Buffer.from("", "base64"),
|
||||
// transformation: {
|
||||
// width: 100,
|
||||
// height: 100,
|
||||
// },
|
||||
// }),
|
||||
// ],
|
||||
// }),
|
||||
// ],
|
||||
// },
|
||||
// ],
|
||||
// });
|
||||
|
||||
// tslint:disable-next-line: no-string-literal
|
||||
sinon.stub(compiler["imageReplacer"], "getMediaData").returns([
|
||||
{
|
||||
stream: Buffer.from(""),
|
||||
fileName: "test",
|
||||
transformation: {
|
||||
pixels: {
|
||||
x: 100,
|
||||
y: 100,
|
||||
},
|
||||
emus: {
|
||||
x: 100,
|
||||
y: 100,
|
||||
},
|
||||
},
|
||||
},
|
||||
]);
|
||||
// // tslint:disable-next-line: no-string-literal
|
||||
// sinon.stub(compiler["imageReplacer"], "getMediaData").returns([
|
||||
// {
|
||||
// stream: Buffer.from(""),
|
||||
// fileName: "test",
|
||||
// transformation: {
|
||||
// pixels: {
|
||||
// x: 100,
|
||||
// y: 100,
|
||||
// },
|
||||
// emus: {
|
||||
// x: 100,
|
||||
// y: 100,
|
||||
// },
|
||||
// },
|
||||
// },
|
||||
// ]);
|
||||
|
||||
compiler.compile(file);
|
||||
});
|
||||
});
|
||||
});
|
||||
// compiler.compile(file);
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
|
@ -1,4 +1,4 @@
|
||||
import JSZip from "jszip";
|
||||
import { strToU8, zip } from "fflate";
|
||||
import xml from "xml";
|
||||
|
||||
import { File } from "@file/file";
|
||||
@ -44,26 +44,39 @@ export class Compiler {
|
||||
this.numberingReplacer = new NumberingReplacer();
|
||||
}
|
||||
|
||||
public compile(file: File, prettifyXml?: PrettifyType): JSZip {
|
||||
const zip = new JSZip();
|
||||
public compile(file: File, prettifyXml?: PrettifyType): Promise<Uint8Array> {
|
||||
const xmlifiedFileMapping = this.xmlifyFile(file, prettifyXml);
|
||||
const map = new Map<string, IXmlifyedFile | readonly IXmlifyedFile[]>(Object.entries(xmlifiedFileMapping));
|
||||
|
||||
for (const [, obj] of map) {
|
||||
if (Array.isArray(obj)) {
|
||||
for (const subFile of obj as readonly IXmlifyedFile[]) {
|
||||
zip.file(subFile.path, subFile.data);
|
||||
}
|
||||
} else {
|
||||
zip.file((obj as IXmlifyedFile).path, (obj as IXmlifyedFile).data);
|
||||
}
|
||||
}
|
||||
return new Promise<Uint8Array>((resolve, reject) => {
|
||||
zip(
|
||||
{
|
||||
...Array.from(map.entries()).reduce((acc, [, obj]) => {
|
||||
if (Array.isArray(obj)) {
|
||||
for (const subFile of obj as readonly IXmlifyedFile[]) {
|
||||
// eslint-disable-next-line functional/immutable-data
|
||||
acc[subFile.path] = strToU8(subFile.data);
|
||||
}
|
||||
} else {
|
||||
// eslint-disable-next-line functional/immutable-data
|
||||
acc[(obj as IXmlifyedFile).path] = strToU8((obj as IXmlifyedFile).data);
|
||||
}
|
||||
|
||||
for (const { stream, fileName } of file.Media.Array) {
|
||||
zip.file(`word/media/${fileName}`, stream);
|
||||
}
|
||||
|
||||
return zip;
|
||||
return acc;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
}, {} as any),
|
||||
...file.Media.Array.reduce((acc, { stream, fileName }) => ({ ...acc, [`word/media/${fileName}`]: stream }), {}),
|
||||
},
|
||||
{},
|
||||
(err, data) => {
|
||||
if (err) {
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(data);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
private xmlifyFile(file: File, prettify?: PrettifyType): IXmlifyedFileMapping {
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { Stream } from "stream";
|
||||
import { File } from "@file/file";
|
||||
import { strFromU8 } from "fflate";
|
||||
|
||||
import { Compiler } from "./next-compiler";
|
||||
|
||||
@ -15,59 +16,52 @@ export enum PrettifyType {
|
||||
|
||||
export class Packer {
|
||||
public static async toString(file: File, prettify?: boolean | PrettifyType): Promise<string> {
|
||||
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",
|
||||
compression: "DEFLATE",
|
||||
});
|
||||
|
||||
return zipData;
|
||||
const zip = await this.compiler.compile(
|
||||
file,
|
||||
prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify,
|
||||
);
|
||||
return strFromU8(zip);
|
||||
}
|
||||
|
||||
public static async toBuffer(file: File, prettify?: boolean | PrettifyType): Promise<Buffer> {
|
||||
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",
|
||||
compression: "DEFLATE",
|
||||
});
|
||||
|
||||
return zipData;
|
||||
const zip = await this.compiler.compile(
|
||||
file,
|
||||
prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify,
|
||||
);
|
||||
return Buffer.from(zip.buffer);
|
||||
}
|
||||
|
||||
public static async toBase64String(file: File, prettify?: boolean | PrettifyType): Promise<string> {
|
||||
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",
|
||||
compression: "DEFLATE",
|
||||
});
|
||||
const zip = await this.compiler.compile(
|
||||
file,
|
||||
prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify,
|
||||
);
|
||||
|
||||
return zipData;
|
||||
return Promise.resolve(strFromU8(zip));
|
||||
}
|
||||
|
||||
public static async toBlob(file: File, prettify?: boolean | PrettifyType): Promise<Blob> {
|
||||
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",
|
||||
compression: "DEFLATE",
|
||||
});
|
||||
const zip = await this.compiler.compile(
|
||||
file,
|
||||
prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify,
|
||||
);
|
||||
|
||||
return zipData;
|
||||
return new Blob([zip.buffer]);
|
||||
}
|
||||
|
||||
public static toStream(file: File, prettify?: boolean | PrettifyType): Stream {
|
||||
const zip = this.compiler.compile(file, prettify === true ? PrettifyType.WITH_2_BLANKS : prettify === false ? undefined : prettify);
|
||||
const zipData = zip.generateNodeStream({
|
||||
type: "nodebuffer",
|
||||
streamFiles: true,
|
||||
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
compression: "DEFLATE",
|
||||
});
|
||||
|
||||
return zipData;
|
||||
const stream = new Stream();
|
||||
// eslint-disable-next-line functional/immutable-data
|
||||
stream.pipe = (dest) => {
|
||||
zip.then((z) => {
|
||||
dest.write(z);
|
||||
});
|
||||
return dest;
|
||||
};
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
private static readonly compiler = new Compiler();
|
||||
|
Reference in New Issue
Block a user