Move fs to exporter and add browser packer

This commit is contained in:
Dolan
2018-04-24 22:56:56 +01:00
parent 06418655c0
commit e8bc7952db
8 changed files with 74 additions and 7 deletions

22
demo/browser-demo.html Normal file
View File

@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<script src="../build/index.web.js"></script>
</head>
<body>
<h1>DOCX browser Word document generation</h1>
<button type="button" onclick="generate()">Click to generate document</button>
<script>
function generate() {
console.log('g');
}
</script>
</body>
</html>

View File

@ -12,6 +12,7 @@
"build": "npm run webpack && npm run fix-types",
"tsc": "rimraf ./build && tsc -p .",
"webpack": "rimraf ./build && webpack",
"build.web": "webpack --config webpack.web.config.js",
"demo": "npm run build && node ./demo",
"typedoc": "typedoc --out docs/ src/ --module commonjs --target ES6 --disableOutputCheck --excludePrivate --externalPattern \"**/*.spec.ts\"",
"style": "prettier -l \"src/**/*.ts\"",

View File

@ -0,0 +1,17 @@
import { Compiler } from "./next-compiler";
import { IPacker } from "./packer";
declare var saveAs;
export class BrowserPacker implements IPacker {
private readonly packer: Compiler;
public async pack(filePath: string): Promise<void> {
filePath = filePath.replace(/.docx$/, "");
const zip = await this.packer.compile();
const zipBlob = await zip.generateAsync({ type: "blob" });
saveAs(zipBlob, `${filePath}.docx`);
}
}

View File

@ -1,5 +1,6 @@
import * as archiver from "archiver";
import * as express from "express";
import * as fs from "fs";
import { Writable } from "stream";
import * as xml from "xml";
@ -89,7 +90,7 @@ export class Compiler {
});
for (const data of this.file.Media.array) {
this.archive.append(data.stream, {
this.archive.append(fs.createReadStream(data.path), {
name: `word/media/${data.fileName}`,
});
}

View File

@ -12,7 +12,6 @@ describe("Drawing", () => {
currentBreak = new Drawing({
fileName: "test.jpg",
referenceId: 1,
stream: fs.createReadStream(path),
path: path,
dimensions: {
pixels: {

View File

@ -1,5 +1,3 @@
import * as fs from "fs";
export interface IMediaDataDimensions {
pixels: {
x: number;
@ -13,7 +11,6 @@ export interface IMediaDataDimensions {
export interface IMediaData {
referenceId: number;
stream: fs.ReadStream;
path: string;
fileName: string;
dimensions: IMediaDataDimensions;

View File

@ -1,4 +1,3 @@
import * as fs from "fs";
import * as sizeOf from "image-size";
import * as path from "path";
@ -27,7 +26,6 @@ export class Media {
const imageData = {
referenceId: this.map.size + relationshipsCount + 1,
stream: fs.createReadStream(filePath),
path: filePath,
fileName: key,
dimensions: {

32
webpack.web.config.js Normal file
View File

@ -0,0 +1,32 @@
const path = require('path');
module.exports = {
entry: './src/index.ts',
output: {
path: path.resolve('build'),
filename: 'index.web.js',
libraryTarget: 'umd'
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
modules: [path.resolve('./src'), "node_modules"]
},
module: {
rules: [
{
test: /\.ts$/,
loaders: ["awesome-typescript-loader"],
}
],
},
node: {
__dirname: true,
fs: "empty",
tls: "empty",
net: "empty"
}
};