Skip to content

Commit 3523ce1

Browse files
committed
Fixed issues #2 and #1
1 parent b50ee94 commit 3523ce1

File tree

6 files changed

+178
-18
lines changed

6 files changed

+178
-18
lines changed

cjs/index.cjs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
var { Worker } = require("node:worker_threads");
2-
var path = require("node:path");
3-
var fs = require("fs/promises");
4-
var os = require("node:os");
5-
var cpus = os.cpus().length;
1+
var { Worker } = require("node:worker_threads"),
2+
path = require("node:path"),
3+
fs = require("fs/promises"),
4+
os = require("node:os"),
5+
cpus = os.cpus().length;
66

77
async function minifyFolder(inputDir, outputDir, filesToMinify) {
88
await fs.mkdir(outputDir, { recursive: true });
9+
var promises = [];
910
for (let i = 1; i <= cpus && filesToMinify.length; i++)
10-
RegisterWorkersToFiles(inputDir, outputDir, filesToMinify);
11+
promises.push(RegisterWorkersToFiles(inputDir, outputDir, filesToMinify));
12+
return Promise.all(promises);
1113
}
1214
async function RegisterWorkersToFiles(inputDir, outputDir, filesToMinify) {
1315
do {

cjs/worker.cjs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import fs from "node:fs";
2-
import { workerData, parentPort } from "node:worker_threads";
3-
import path from "node:path";
4-
5-
const { input, output } = workerData;
6-
7-
const START_TAG = "/*_START_DEV_*/";
8-
const END_TAG = "/*_END_DEV_*/";
1+
var fs = require("node:fs"),
2+
{ workerData, parentPort } = require("node:worker_threads"),
3+
path = require("node:path"),
4+
{ input, output } = workerData,
5+
START_TAG = "/*_START_DEV_*/",
6+
END_TAG = "/*_END_DEV_*/";
97
(async () => {
108
await fs.promises.mkdir(path.dirname(output), { recursive: true });
119

index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* This function takes all files (in 3rd parameter) and effectively utilizes worker threads to get all of your code prepared as soon as possible.
33
* @param inputDir directory, which is 'compiled'
4-
* @param outputDir directory, to whichfiles are 'compiled'
5-
* @param filesToMinify array of filepaths, relative ot inputDir. will be created in same nested dirs and same names as is in inputDir.
4+
* @param outputDir directory, to which files are 'compiled'
5+
* @param filesToMinify array of filepaths, relative to inputDir. It will be created in the same nested dirs and with same names as is in inputDir.
66
*/
77
export function minifyFolder(
88
inputDir: string,

mjs/index.mjs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ var cpus = os.cpus().length;
66

77
async function minifyFolder(inputDir, outputDir, filesToMinify) {
88
await fs.mkdir(outputDir, { recursive: true });
9+
var promises = [];
910
for (let i = 1; i <= cpus && filesToMinify.length; i++)
10-
RegisterWorkersToFiles(inputDir, outputDir, filesToMinify);
11+
promises.push(RegisterWorkersToFiles(inputDir, outputDir, filesToMinify));
12+
return Promise.all(promises);
1113
}
1214
async function RegisterWorkersToFiles(inputDir, outputDir, filesToMinify) {
1315
do {

tests/minifyFile.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import os from "node:os";
55

66
import { minifyFile } from "../mjs/index.mjs";
77

8-
const tmpDir = path.join(os.tmpdir(), "vitest-devstrip-tests");
8+
const tmpDir = path.join(os.tmpdir(), "vitest-devstrip-tests", "minifyFile");
99
const inputFile = (name: string) => path.join(tmpDir, `input-${name}.js`);
1010
const outputFile = (name: string) => path.join(tmpDir, `output-${name}.js`);
1111

tests/minifyFolder.test.ts

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
2+
import path from "node:path";
3+
import fs from "node:fs/promises";
4+
import os from "node:os";
5+
import { minifyFolder } from "../mjs/index.mjs";
6+
7+
const tmpDir = path.join(os.tmpdir(), "vitest-devstrip-tests", "minifyFolder");
8+
const inputDir = (name: string) => path.join(tmpDir, `input-${name}`);
9+
const outputDir = (name: string) => path.join(tmpDir, `output-${name}`);
10+
11+
beforeEach(async () => {
12+
await fs.mkdir(tmpDir, { recursive: true });
13+
});
14+
15+
afterEach(async () => {
16+
await fs.rm(tmpDir, { recursive: true, force: true });
17+
});
18+
describe("minifyFolder", () => {
19+
it("removes dev blocks from multiple files", async () => {
20+
const input = inputDir("multiple");
21+
const output = outputDir("multiple");
22+
23+
await fs.mkdir(input, { recursive: true });
24+
await fs.writeFile(
25+
path.join(input, "file1.js"),
26+
`console.log("start");/*_START_DEV_*/console.log("dev code");/*_END_DEV_*/console.log("end");`
27+
);
28+
await fs.writeFile(
29+
path.join(input, "file2.js"),
30+
`start /*_START_DEV_*/ dev1 /*_END_DEV_*/middle/*_START_DEV_*/ dev2 /*_END_DEV_*/ end`
31+
);
32+
33+
await minifyFolder(input, output, ["file1.js", "file2.js"]);
34+
35+
const resultFile1 = await fs.readFile(
36+
path.join(output, "file1.js"),
37+
"utf-8"
38+
);
39+
const resultFile2 = await fs.readFile(
40+
path.join(output, "file2.js"),
41+
"utf-8"
42+
);
43+
44+
expect(resultFile1.trim()).toBe(`console.log("start");console.log("end");`);
45+
expect(resultFile2.trim()).toBe(`start middle end`);
46+
});
47+
it("does not modify files if there are no dev blocks", async () => {
48+
const input = inputDir("no-dev");
49+
const output = outputDir("no-dev");
50+
51+
await fs.mkdir(input, { recursive: true });
52+
await fs.writeFile(
53+
path.join(input, "file1.js"),
54+
`console.log("start");console.log("end");`
55+
);
56+
await fs.writeFile(path.join(input, "file2.js"), `start middle end`);
57+
58+
await minifyFolder(input, output, ["file1.js", "file2.js"]);
59+
60+
const resultFile1 = await fs.readFile(
61+
path.join(output, "file1.js"),
62+
"utf-8"
63+
);
64+
const resultFile2 = await fs.readFile(
65+
path.join(output, "file2.js"),
66+
"utf-8"
67+
);
68+
69+
expect(resultFile1.trim()).toBe(`console.log("start");console.log("end");`);
70+
expect(resultFile2.trim()).toBe(`start middle end`);
71+
});
72+
it("removes dev blocks at the start and end of the file", async () => {
73+
const input = inputDir("start-end");
74+
const output = outputDir("start-end");
75+
76+
await fs.mkdir(input, { recursive: true });
77+
await fs.writeFile(
78+
path.join(input, "file1.js"),
79+
`/*_START_DEV_*/devstart/*_END_DEV_*/middle/*_START_DEV_*/devend/*_END_DEV_*/`
80+
);
81+
await fs.writeFile(
82+
path.join(input, "file2.js"),
83+
`/*_START_DEV_*/dev1/*_END_DEV_*/some code`
84+
);
85+
86+
await minifyFolder(input, output, ["file1.js", "file2.js"]);
87+
88+
const resultFile1 = await fs.readFile(
89+
path.join(output, "file1.js"),
90+
"utf-8"
91+
);
92+
const resultFile2 = await fs.readFile(
93+
path.join(output, "file2.js"),
94+
"utf-8"
95+
);
96+
97+
expect(resultFile1.trim()).toBe(`middle`);
98+
expect(resultFile2.trim()).toBe(`some code`);
99+
});
100+
it("preserves folder structure in nested directories", async () => {
101+
const input = inputDir("nested");
102+
const output = outputDir("nested");
103+
await fs.mkdir(path.join(input, "nested"), { recursive: true });
104+
await fs.writeFile(
105+
path.join(input, "file1.js"),
106+
`console.log("start");/*_START_DEV_*/console.log("dev code");/*_END_DEV_*/console.log("end");`
107+
);
108+
await fs.writeFile(
109+
path.join(input, "nested", "file2.js"),
110+
`start /*_START_DEV_*/ dev1 /*_END_DEV_*/ middle /*_START_DEV_*/ dev2 /*_END_DEV_*/ end`
111+
);
112+
113+
await minifyFolder(input, output, ["file1.js", "nested/file2.js"]);
114+
115+
const resultFile1 = await fs.readFile(
116+
path.join(output, "file1.js"),
117+
"utf-8"
118+
);
119+
const resultFile2 = await fs.readFile(
120+
path.join(output, "nested", "file2.js"),
121+
"utf-8"
122+
);
123+
124+
expect(resultFile1.trim()).toBe(`console.log("start");console.log("end");`);
125+
expect(resultFile2.trim()).toBe(`start middle end`);
126+
});
127+
it("handles empty folder gracefully", async () => {
128+
const input = inputDir("empty");
129+
const output = outputDir("empty");
130+
131+
await fs.mkdir(input, { recursive: true });
132+
133+
await minifyFolder(input, output, []);
134+
135+
const filesInOutput = await fs.readdir(output);
136+
expect(filesInOutput).toHaveLength(0);
137+
});
138+
it("does not modify files without dev blocks", async () => {
139+
const input = inputDir("single-no-dev");
140+
const output = outputDir("single-no-dev");
141+
142+
await fs.mkdir(input, { recursive: true });
143+
await fs.writeFile(
144+
path.join(input, "file1.js"),
145+
`const x = 10; const y = 20; console.log(x + y);`
146+
);
147+
148+
await minifyFolder(input, output, ["file1.js"]);
149+
150+
const resultFile1 = await fs.readFile(
151+
path.join(output, "file1.js"),
152+
"utf-8"
153+
);
154+
expect(resultFile1.trim()).toBe(
155+
`const x = 10; const y = 20; console.log(x + y);`
156+
);
157+
});
158+
});

0 commit comments

Comments
 (0)