summaryrefslogtreecommitdiff
path: root/node_modules/tailwindcss/src/oxide/cli/build/deps.ts
blob: 2b4355b17f8f4447dd9c61e293f301fe58c0231f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import packageJson from '../../../../package.json'
import browserslist from 'browserslist'
import { Result } from 'postcss'

import {
  // @ts-ignore
  lazyPostcss,

  // @ts-ignore
  lazyPostcssImport,

  // @ts-ignore
  lazyCssnano,

  // @ts-ignore
} from '../../../../peers/index'

export function lazyLightningCss() {
  // TODO: Make this lazy/bundled
  return require('lightningcss')
}

let lightningCss

function loadLightningCss() {
  if (lightningCss) {
    return lightningCss
  }

  // Try to load a local version first
  try {
    return (lightningCss = require('lightningcss'))
  } catch {}

  return (lightningCss = lazyLightningCss())
}

export async function lightningcss(shouldMinify: boolean, result: Result) {
  let css = loadLightningCss()

  try {
    let transformed = css.transform({
      filename: result.opts.from || 'input.css',
      code: Buffer.from(result.css, 'utf-8'),
      minify: shouldMinify,
      sourceMap: !!result.map,
      inputSourceMap: result.map ? result.map.toString() : undefined,
      targets: css.browserslistToTargets(browserslist(packageJson.browserslist)),
      drafts: {
        nesting: true,
      },
    })

    return Object.assign(result, {
      css: transformed.code.toString('utf8'),
      map: result.map
        ? Object.assign(result.map, {
            toString() {
              return transformed.map.toString()
            },
          })
        : result.map,
    })
  } catch (err) {
    console.error('Unable to use Lightning CSS. Using raw version instead.')
    console.error(err)

    return result
  }
}

/**
 * @returns {import('postcss')}
 */
export function loadPostcss() {
  // Try to load a local `postcss` version first
  try {
    return require('postcss')
  } catch {}

  return lazyPostcss()
}

export function loadPostcssImport() {
  // Try to load a local `postcss-import` version first
  try {
    return require('postcss-import')
  } catch {}

  return lazyPostcssImport()
}