|
| 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