|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { describe, test, beforeAll, afterAll } from "vitest"; |
| 4 | +import esbuild from "esbuild"; |
| 5 | +import cssPlugin from "../src"; |
| 6 | +import glob from "tiny-glob"; |
| 7 | + |
| 8 | +describe("Test plugin with esbuild", async () => { |
| 9 | + const exampleBuildDir = path.resolve(process.cwd(), "test-build1"); |
| 10 | + |
| 11 | + beforeAll(async () => { |
| 12 | + await esbuild.build({ |
| 13 | + format: "cjs", |
| 14 | + target: "es2019", |
| 15 | + sourcemap: false, |
| 16 | + bundle: true, |
| 17 | + minify: true, |
| 18 | + plugins: [cssPlugin()], |
| 19 | + entryPoints: await glob("../esbuild-plugin-react18-css-example/src/**/*.*"), |
| 20 | + external: ["react", "react-dom"], |
| 21 | + outdir: "./test-build1", |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + test(`Test CSS Class Hash`, ({ expect }) => { |
| 26 | + const text = fs.readFileSync(path.resolve(exampleBuildDir, "server", "index.js"), "utf-8"); |
| 27 | + expect(/{fork:["'][^"']*fork[^"']*["']/.test(text)).toBe(true); |
| 28 | + }); |
| 29 | + |
| 30 | + test("Should contain -moz-", ({ expect }) => { |
| 31 | + const test = fs.readFileSync(path.resolve(exampleBuildDir, "server", "index.css"), "utf-8"); |
| 32 | + expect(/-moz-/.test(test)).toBe(true); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe("Test plugin with esbuild and options", async () => { |
| 37 | + const exampleBuildDir = path.resolve(process.cwd(), "test-build"); |
| 38 | + |
| 39 | + beforeAll(async () => { |
| 40 | + await esbuild.build({ |
| 41 | + format: "cjs", |
| 42 | + target: "es2019", |
| 43 | + sourcemap: false, |
| 44 | + bundle: true, |
| 45 | + minify: true, |
| 46 | + plugins: [ |
| 47 | + cssPlugin({ |
| 48 | + skipAutoPrefixer: true, |
| 49 | + generateScopedName: "[name]__[local]___[hash:base64:5]", |
| 50 | + }), |
| 51 | + ], |
| 52 | + entryPoints: await glob("../esbuild-plugin-react18-css-example/src/**/*.*"), |
| 53 | + publicPath: "https://my.domain/static/", |
| 54 | + external: ["react", "react-dom"], |
| 55 | + outdir: "./test-build", |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + test(`Test CSS Class Hash`, ({ expect }) => { |
| 60 | + const text = fs.readFileSync(path.resolve(exampleBuildDir, "server", "index.js"), "utf-8"); |
| 61 | + expect(/{fork:["'][^"']*fork[^"']*["']/.test(text)).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + test("Should not contain -moz-", ({ expect }) => { |
| 65 | + const test = fs.readFileSync(path.resolve(exampleBuildDir, "index.css"), "utf-8"); |
| 66 | + expect(/-moz-/.test(test)).toBe(false); |
| 67 | + }); |
| 68 | +}); |
0 commit comments