plugin.js 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. const _setupTrackingContext = /*#__PURE__*/ _interopRequireDefault(require("./lib/setupTrackingContext"));
  6. const _processTailwindFeatures = /*#__PURE__*/ _interopRequireDefault(require("./processTailwindFeatures"));
  7. const _sharedState = require("./lib/sharedState");
  8. const _findAtConfigPath = require("./lib/findAtConfigPath");
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. module.exports = function tailwindcss(configOrPath) {
  15. return {
  16. postcssPlugin: "tailwindcss",
  17. plugins: [
  18. _sharedState.env.DEBUG && function(root) {
  19. console.log("\n");
  20. console.time("JIT TOTAL");
  21. return root;
  22. },
  23. function(root, result) {
  24. var _findAtConfigPath1;
  25. // Use the path for the `@config` directive if it exists, otherwise use the
  26. // path for the file being processed
  27. configOrPath = (_findAtConfigPath1 = (0, _findAtConfigPath.findAtConfigPath)(root, result)) !== null && _findAtConfigPath1 !== void 0 ? _findAtConfigPath1 : configOrPath;
  28. let context = (0, _setupTrackingContext.default)(configOrPath);
  29. if (root.type === "document") {
  30. let roots = root.nodes.filter((node)=>node.type === "root");
  31. for (const root1 of roots){
  32. if (root1.type === "root") {
  33. (0, _processTailwindFeatures.default)(context)(root1, result);
  34. }
  35. }
  36. return;
  37. }
  38. (0, _processTailwindFeatures.default)(context)(root, result);
  39. },
  40. _sharedState.env.OXIDE && function lightningCssPlugin(_root, result) {
  41. let postcss = require("postcss");
  42. let lightningcss = require("lightningcss");
  43. let browserslist = require("browserslist");
  44. try {
  45. let transformed = lightningcss.transform({
  46. filename: result.opts.from,
  47. code: Buffer.from(result.root.toString()),
  48. minify: false,
  49. sourceMap: !!result.map,
  50. inputSourceMap: result.map ? result.map.toString() : undefined,
  51. targets: typeof process !== "undefined" && process.env.JEST_WORKER_ID ? {
  52. chrome: 106 << 16
  53. } : lightningcss.browserslistToTargets(browserslist(require("../package.json").browserslist)),
  54. drafts: {
  55. nesting: true,
  56. customMedia: true
  57. }
  58. });
  59. var _result_map;
  60. result.map = Object.assign((_result_map = result.map) !== null && _result_map !== void 0 ? _result_map : {}, {
  61. toJSON () {
  62. return transformed.map.toJSON();
  63. },
  64. toString () {
  65. return transformed.map.toString();
  66. }
  67. });
  68. result.root = postcss.parse(transformed.code.toString("utf8"));
  69. } catch (err) {
  70. if (typeof process !== "undefined" && process.env.JEST_WORKER_ID) {
  71. let lines = err.source.split("\n");
  72. err = new Error([
  73. "Error formatting using Lightning CSS:",
  74. "",
  75. ...[
  76. "```css",
  77. ...lines.slice(Math.max(err.loc.line - 3, 0), err.loc.line),
  78. " ".repeat(err.loc.column - 1) + "^-- " + err.toString(),
  79. ...lines.slice(err.loc.line, err.loc.line + 2),
  80. "```"
  81. ]
  82. ].join("\n"));
  83. }
  84. if (Error.captureStackTrace) {
  85. Error.captureStackTrace(err, lightningCssPlugin);
  86. }
  87. throw err;
  88. }
  89. },
  90. _sharedState.env.DEBUG && function(root) {
  91. console.timeEnd("JIT TOTAL");
  92. console.log("\n");
  93. return root;
  94. }
  95. ].filter(Boolean)
  96. };
  97. };
  98. module.exports.postcss = true;