|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- const autoprefixer = require("autoprefixer"),
- postCssImport = require("postcss-import"),
- { build } = require("esbuild"),
- postCSS = require("../dist"),
- { assert } = require("chai"),
- fs = require("fs");
-
- describe("PostCSS esbuild tests", () => {
- it("Works with basic CSS imports", (done) => {
- test(["tests/basic.ts"])
- .then((res) => {
- assert(res);
- done();
- })
- .catch(done);
- });
- it("Works with preprocessors", (done) => {
- test(["tests/preprocessors.ts"])
- .then((res) => {
- assert(res);
- done();
- })
- .catch(done);
- });
- it("Works with CSS modules", (done) => {
- test(["tests/modules.ts"])
- .then((res) => {
- assert(res);
- done();
- })
- .catch(done);
- });
- it("Works with CSS as entrypoint", (done) => {
- test(["tests/styles.css", "tests/styles2.css"])
- .then((res) => {
- assert(res);
- done();
- })
- .catch(done);
- });
- it("Works with node_modules import", (done) => {
- test(["tests/node_modules.ts"])
- .then((res) => {
- assert(res);
- done();
- })
- .catch(done);
- });
- it("Works while waching css files directly", (done) => {
- let notTriggerTimeout = null;
- build({
- entryPoints: ["tests/watch.ts"],
- bundle: true,
- outdir: "dist",
- watch: {
- onRebuild: (error, result) => {
- notTriggerTimeout = null;
- if (error) return done(error);
- assert(result);
- done();
- }
- },
- plugins: [
- postCSS.default({
- plugins: [autoprefixer]
- })
- ]
- })
- .then(() => {
- // test if modifying the css actually triggers the onRebuild event
- const data = `.Test { display: block; }`;
- fs.writeFile("./styles/watch.css", data, (err) => {
- if (err) return done(err);
- notTriggerTimeout = setTimeout(() => {
- done("Watch file not triggered!");
- }, 1000);
- });
- })
- .catch(() => process.exit(1));
- });
-
- it("Works while waching css files through dependencies", (done) => {
- let notTriggerTimeout = null;
- build({
- entryPoints: ["tests/watch2.ts"],
- bundle: true,
- outdir: "dist",
- watch: {
- onRebuild: (error, result) => {
- notTriggerTimeout = null;
- if (error) return done(error);
- assert(result);
- done();
- }
- },
- plugins: [
- postCSS.default({
- plugins: [autoprefixer, postCssImport]
- })
- ]
- })
- .then(() => {
- // test if modifying the css actually triggers the onRebuild event
- const data = `.Test { display: block; }`;
- fs.writeFile("./styles/watch3.css", data, (err) => {
- if (err) return done(err);
- notTriggerTimeout = setTimeout(() => {
- done("Watch file not triggered!");
- }, 1000);
- });
- })
- .catch(() => process.exit(1));
- });
-
- it("Works with custom module function", (done) => {
- let testFilename = null;
- build({
- entryPoints: ["tests/basic.ts"],
- bundle: true,
- outdir: "dist",
- plugins: [
- postCSS.default({
- plugins: [autoprefixer, postCssImport],
- modules: true,
- fileIsModule: (filename) => {
- testFilename = filename;
- return false;
- }
- })
- ]
- })
- .then(() => {
- // ensure the proper filename was passed
- assert.match(testFilename, /styles\/basic\.css/);
- })
- .catch((e) => {
- console.error(e);
- process.exit(1);
- });
- });
- });
-
- function test(entryPoint) {
- return build({
- entryPoints: entryPoint,
- bundle: true,
- outdir: "dist",
- plugins: [
- postCSS.default({
- plugins: [autoprefixer]
- })
- ]
- }).catch(() => process.exit(1));
- }
|