Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

155 righe
3.8 KiB

  1. const autoprefixer = require("autoprefixer"),
  2. postCssImport = require("postcss-import"),
  3. { build } = require("esbuild"),
  4. postCSS = require("../dist"),
  5. { assert } = require("chai"),
  6. fs = require("fs");
  7. describe("PostCSS esbuild tests", () => {
  8. it("Works with basic CSS imports", (done) => {
  9. test(["tests/basic.ts"])
  10. .then((res) => {
  11. assert(res);
  12. done();
  13. })
  14. .catch(done);
  15. });
  16. it("Works with preprocessors", (done) => {
  17. test(["tests/preprocessors.ts"])
  18. .then((res) => {
  19. assert(res);
  20. done();
  21. })
  22. .catch(done);
  23. });
  24. it("Works with CSS modules", (done) => {
  25. test(["tests/modules.ts"])
  26. .then((res) => {
  27. assert(res);
  28. done();
  29. })
  30. .catch(done);
  31. });
  32. it("Works with CSS as entrypoint", (done) => {
  33. test(["tests/styles.css", "tests/styles2.css"])
  34. .then((res) => {
  35. assert(res);
  36. done();
  37. })
  38. .catch(done);
  39. });
  40. it("Works with node_modules import", (done) => {
  41. test(["tests/node_modules.ts"])
  42. .then((res) => {
  43. assert(res);
  44. done();
  45. })
  46. .catch(done);
  47. });
  48. it("Works while waching css files directly", (done) => {
  49. let notTriggerTimeout = null;
  50. build({
  51. entryPoints: ["tests/watch.ts"],
  52. bundle: true,
  53. outdir: "dist",
  54. watch: {
  55. onRebuild: (error, result) => {
  56. notTriggerTimeout = null;
  57. if (error) return done(error);
  58. assert(result);
  59. done();
  60. }
  61. },
  62. plugins: [
  63. postCSS.default({
  64. plugins: [autoprefixer]
  65. })
  66. ]
  67. })
  68. .then(() => {
  69. // test if modifying the css actually triggers the onRebuild event
  70. const data = `.Test { display: block; }`;
  71. fs.writeFile("./styles/watch.css", data, (err) => {
  72. if (err) return done(err);
  73. notTriggerTimeout = setTimeout(() => {
  74. done("Watch file not triggered!");
  75. }, 1000);
  76. });
  77. })
  78. .catch(() => process.exit(1));
  79. });
  80. it("Works while waching css files through dependencies", (done) => {
  81. let notTriggerTimeout = null;
  82. build({
  83. entryPoints: ["tests/watch2.ts"],
  84. bundle: true,
  85. outdir: "dist",
  86. watch: {
  87. onRebuild: (error, result) => {
  88. notTriggerTimeout = null;
  89. if (error) return done(error);
  90. assert(result);
  91. done();
  92. }
  93. },
  94. plugins: [
  95. postCSS.default({
  96. plugins: [autoprefixer, postCssImport]
  97. })
  98. ]
  99. })
  100. .then(() => {
  101. // test if modifying the css actually triggers the onRebuild event
  102. const data = `.Test { display: block; }`;
  103. fs.writeFile("./styles/watch3.css", data, (err) => {
  104. if (err) return done(err);
  105. notTriggerTimeout = setTimeout(() => {
  106. done("Watch file not triggered!");
  107. }, 1000);
  108. });
  109. })
  110. .catch(() => process.exit(1));
  111. });
  112. it("Works with custom module function", (done) => {
  113. let testFilename = null;
  114. build({
  115. entryPoints: ["tests/basic.ts"],
  116. bundle: true,
  117. outdir: "dist",
  118. plugins: [
  119. postCSS.default({
  120. plugins: [autoprefixer, postCssImport],
  121. modules: true,
  122. fileIsModule: (filename) => {
  123. testFilename = filename;
  124. return false;
  125. }
  126. })
  127. ]
  128. })
  129. .then(() => {
  130. // ensure the proper filename was passed
  131. assert.match(testFilename, /styles\/basic\.css/);
  132. })
  133. .catch((e) => {
  134. console.error(e);
  135. process.exit(1);
  136. });
  137. });
  138. });
  139. function test(entryPoint) {
  140. return build({
  141. entryPoints: entryPoint,
  142. bundle: true,
  143. outdir: "dist",
  144. plugins: [
  145. postCSS.default({
  146. plugins: [autoprefixer]
  147. })
  148. ]
  149. }).catch(() => process.exit(1));
  150. }