File size: 3,314 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
declare const __turbopack_external_require__: (
  id: string,
  thunk: () => any,
  esm?: boolean
) => any

import type { Processor } from 'postcss'

// @ts-ignore
import postcss from '@vercel/turbopack/postcss'
// @ts-ignore
import importedConfig from 'CONFIG'
import { getReadEnvVariables, toPath, type TransformIpc } from './transforms'

let processor: Processor | undefined

export const init = async (ipc: TransformIpc) => {
  let config = importedConfig
  if (typeof config === 'function') {
    config = await config({ env: 'development' })
  }
  if (typeof config === 'undefined') {
    throw new Error(
      'PostCSS config is undefined (make sure to export an function or object from config file)'
    )
  }
  let plugins: any[]
  if (Array.isArray(config.plugins)) {
    plugins = config.plugins.map((plugin: [string, any] | string | any) => {
      if (Array.isArray(plugin)) {
        return plugin
      } else if (typeof plugin === 'string') {
        return [plugin, {}]
      } else {
        return plugin
      }
    })
  } else if (typeof config.plugins === 'object') {
    plugins = Object.entries(config.plugins).filter(([, options]) => options)
  } else {
    plugins = []
  }
  const loadedPlugins = plugins.map((plugin) => {
    if (Array.isArray(plugin)) {
      const [arg, options] = plugin
      let pluginFactory = arg

      if (typeof pluginFactory === 'string') {
        pluginFactory = require(/* turbopackIgnore: true */ pluginFactory)
      }

      if (pluginFactory.default) {
        pluginFactory = pluginFactory.default
      }

      return pluginFactory(options)
    }
    return plugin
  })

  processor = postcss(loadedPlugins)
}

export default async function transform(
  ipc: TransformIpc,
  cssContent: string,
  name: string,
  sourceMap: boolean
) {
  const { css, map, messages } = await processor!.process(cssContent, {
    from: name,
    to: name,
    map: sourceMap
      ? {
          inline: false,
          annotation: false,
        }
      : undefined,
  })

  const assets = []
  const filePaths: string[] = []
  const buildFilePaths: string[] = []
  const directories: Array<[string, string]> = []

  for (const msg of messages) {
    switch (msg.type) {
      case 'asset':
        assets.push({
          file: msg.file,
          content: msg.content,
          sourceMap: !sourceMap
            ? undefined
            : typeof msg.sourceMap === 'string'
              ? msg.sourceMap
              : JSON.stringify(msg.sourceMap),
          // There is also an info field, which we currently ignore
        })
        break
      case 'dependency':
      case 'missing-dependency':
        filePaths.push(toPath(msg.file))
        break
      case 'build-dependency':
        buildFilePaths.push(toPath(msg.file))
        break
      case 'dir-dependency':
        directories.push([toPath(msg.dir), msg.glob])
        break
      case 'context-dependency':
        directories.push([toPath(msg.dir), '**'])
        break
      default:
        // TODO: do we need to do anything here?
        break
    }
  }
  ipc.sendInfo({
    type: 'dependencies',
    filePaths,
    directories,
    buildFilePaths,
    envVariables: getReadEnvVariables(),
  })
  return {
    css,
    map: sourceMap ? JSON.stringify(map) : undefined,
    assets,
  }
}