Нет описания

plugin.js 3.4KB

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