File size: 1,717 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
/**
 * For server-side CSS imports, we need to ignore the actual module content but
 * still trigger the hot-reloading diff mechanism. So here we put the content
 * inside a comment.
 */

import crypto from 'crypto'
import type webpack from 'webpack'

type NextServerCSSLoaderOptions = {
  cssModules: boolean
}

const NextServerCSSLoader: webpack.LoaderDefinitionFunction<NextServerCSSLoaderOptions> =
  function (content) {
    this.cacheable && this.cacheable()
    const options = this.getOptions()
    let isCSSModule = options.cssModules

    // Only add the checksum during development.
    if (process.env.NODE_ENV !== 'production') {
      // This check is only for backwards compatibility.
      // TODO: Remove this in the next major version (next 14)
      if (isCSSModule === undefined) {
        this.emitWarning(
          new Error(
            "No 'cssModules' option was found for the next-flight-css-loader plugin."
          )
        )
        isCSSModule =
          this.resourcePath.match(/\.module\.(css|sass|scss)$/) !== null
      }
      const checksum = crypto
        .createHash('sha1')
        .update(typeof content === 'string' ? Buffer.from(content) : content)
        .digest()
        .toString('hex')
        .substring(0, 12)

      if (isCSSModule) {
        return `\
${content}
module.exports.__checksum = ${JSON.stringify(checksum)}
`
      }

      // Server CSS imports are always available for HMR, so we attach
      // `module.hot.accept()` to the generated module.
      const hmrCode = 'if (module.hot) { module.hot.accept() }'

      return `\
export default ${JSON.stringify(checksum)}
${hmrCode}
`
    }

    return content
  }

export default NextServerCSSLoader