Sha256: 0ab333c5a3300acc7d0a1707cb4d633256aefa998300e9154015726cc93cf976

Contents?: true

Size: 1.56 KB

Versions: 2

Compression:

Stored size: 1.56 KB

Contents

const ExtractTextPlugin = require('extract-text-webpack-plugin')
const path = require('path')
const devServer = require('../dev_server')

const postcssConfigPath = path.resolve(process.cwd(), '.postcssrc.yml')
const isProduction = process.env.NODE_ENV === 'production'
const inDevServer = process.argv.find(v => v.includes('webpack-dev-server'))
const isHMR = inDevServer && (devServer && devServer.hmr)
const extractCSS = !isHMR || isProduction

const styleLoader = {
  loader: 'style-loader',
  options: {
    hmr: isHMR,
    sourceMap: true
  }
}

const getStyleRule = (test, modules = false, preprocessors = []) => {
  const extractOptions = {
    fallback: styleLoader,
    use: [
      {
        loader: 'css-loader',
        options: {
          minimize: isProduction,
          sourceMap: true,
          importLoaders: 2,
          modules
        }
      },
      {
        loader: 'postcss-loader',
        options: {
          sourceMap: true,
          config: { path: postcssConfigPath }
        }
      },
      ...preprocessors
    ]
  }

  const options = modules ? { include: /\.module\.[a-z]+$/ } : { exclude: /\.module\.[a-z]+$/ }

  // For production extract styles to a separate bundle
  const extractCSSLoader = Object.assign(
    {},
    { test, use: ExtractTextPlugin.extract(extractOptions) },
    options
  )

  // For hot-reloading use regular loaders
  const inlineCSSLoader = Object.assign(
    {},
    { test, use: [styleLoader].concat(extractOptions.use) },
    options
  )

  return extractCSS ? extractCSSLoader : inlineCSSLoader
}

module.exports = getStyleRule

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
webpacker-3.3.1 package/utils/get_style_rule.js
webpacker-3.3.0 package/utils/get_style_rule.js