Skip to content

Commit

Permalink
Merge pull request #2244 from dolanmiu/feature/build-fixes
Browse files Browse the repository at this point in the history
Fix `vite` build issues
  • Loading branch information
dolanmiu authored Jul 19, 2023
2 parents 4498305 + 0c51082 commit 2ab06ff
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 27 deletions.
2 changes: 1 addition & 1 deletion demo/browser-demo.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<script src="../build/index.umd.cjs"></script>
<script src="../build/index.umd.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
</head>

Expand Down
18 changes: 9 additions & 9 deletions demo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import inquirer from "inquirer";
import { $ } from "execa";

export type Answers = {
type: "list" | "number";
demoNumber?: number;
demoFile?: number;
readonly type: "list" | "number";
readonly demoNumber?: number;
readonly demoFile?: number;
};

const dir = "./demo";
const fileNames = fs.readdirSync(dir);

const keys = fileNames.map((f) => path.parse(f).name);
const getFileNumber = (file: string): number => {
const nameParts = file.split("-");
const firstPart = nameParts[0];
const [firstPart] = file.split("-");

return Number(firstPart);
};
Expand All @@ -35,15 +34,15 @@ const answers = await inquirer.prompt<Answers>([
name: "demoFile",
message: "What demo do you wish to run?",
choices: demoFiles,
filter: (input) => parseInt(input.split("-")[0]),
when: (answers) => answers.type === "list",
filter: (input) => parseInt(input.split("-")[0], 10),
when: (a) => a.type === "list",
},
{
type: "number",
name: "demoNumber",
message: "What demo do you wish to run? (Enter a number)",
default: 1,
when: (answers) => answers.type === "number",
when: (a) => a.type === "number",
},
]);

Expand All @@ -56,6 +55,7 @@ if (files.length === 0) {
const filePath = path.join(dir, files[0]);

console.log(`Running demo ${demoNumber}: ${files[0]}`);
await $`ts-node --project demo/tsconfig.json ${filePath}`;
const { stdout } = await $`ts-node --project demo/tsconfig.json ${filePath}`;
console.log(stdout);
console.log("Successfully created document!");
}
11 changes: 0 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"version": "8.2.0",
"description": "Easily generate .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.",
"type": "module",
"main": "build/index.umd.cjs",
"main": "build/index.umd.js",
"module": "./build/index.js",
"types": "./build/index.d.ts",
"exports": {
".": {
"browser": {
"default": "./build/index.umd.cjs"
"default": "./build/index.umd.js"
},
"require": "./build/index.cjs",
"types": "./build/index.d.ts",
Expand Down Expand Up @@ -58,7 +58,6 @@
],
"dependencies": {
"@types/node": "^20.3.1",
"fflate": "^0.8.0",
"jszip": "^3.10.1",
"nanoid": "^4.0.2",
"xml": "^1.0.1",
Expand Down
4 changes: 2 additions & 2 deletions src/patcher/from-docx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface PatchDocumentOptions {

const imageReplacer = new ImageReplacer();

export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise<Buffer> => {
export const patchDocument = async (data: InputDataType, options: PatchDocumentOptions): Promise<Uint8Array> => {
const zipContent = await JSZip.loadAsync(data);
const contexts = new Map<string, IContext>();
const file = {
Expand Down Expand Up @@ -213,7 +213,7 @@ export const patchDocument = async (data: InputDataType, options: PatchDocumentO
}

return zip.generateAsync({
type: "nodebuffer",
type: "uint8array",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
compression: "DEFLATE",
});
Expand Down
23 changes: 22 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export default defineConfig({
dts(),
nodePolyfills({
exclude: ["fs"],
globals: {
Buffer: false,
},
protocolImports: true,
}),
],
Expand All @@ -26,7 +29,25 @@ export default defineConfig({
lib: {
entry: [resolve(__dirname, "src/index.ts")],
name: "docx",
fileName: "index",
fileName: (d) => {
if (d === "umd") {
return "index.umd.js";
}

if (d === "cjs") {
return "index.cjs";
}

if (d === "es") {
return "index.js";
}

if (d === "iife") {
return "index.iife.js";
}

return "unknown";
},
formats: ["iife", "es", "cjs", "umd"],
},
outDir: resolve(__dirname, "build"),
Expand Down

0 comments on commit 2ab06ff

Please sign in to comment.