Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack 4 - setup MiniCssExtractPlugin from ExtractTextPlugin

Tags:

webpack

I have been trying to get my application to build. I am new to Webpack and when I try to make a production build on the application I was assigned to I get this message:

C:\Users\MAND\workspace\Azure\Kollecto-StandardApp\build\utils.js:41
      return MiniCssExtractPlugin.loader({
                                  ^

TypeError: MiniCssExtractPlugin.loader is not a function
    at generateLoaders (C:\Users\MAND\workspace\Azure\Kollecto-StandardApp\build\utils.js:41:35)
    at Object.exports.cssLoaders (C:\Users\MAND\workspace\Azure\Kollecto-StandardApp\build\utils.js:62:10)
    at Object.<anonymous> (C:\Users\MAND\workspace\Azure\Kollecto-StandardApp\build\vue-loader.conf.js:8:18)
    at Module._compile (internal/modules/cjs/loader.js:971:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1011:10)
    at Module.load (internal/modules/cjs/loader.js:822:32)
    at Function.Module._load (internal/modules/cjs/loader.js:730:14)
    at Module.require (internal/modules/cjs/loader.js:864:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (C:\Users\MAND\workspace\Azure\Kollecto-StandardApp\build\webpack.base.conf.js:6:25)

I have updated A LOT in the application set up and one of the updates was moving from Webpack 3 to 4. This means that the ExtractTextPlugin has been deprecated and MiniCssExtractPlugin is the plugin to use. My build/utils.js file did look like this before the update:

'use strict'

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

exports.assetsPath = function (_path) {
  const assetsSubDirectory = process.env.NODE_ENV === 'production'
    ? config.build.assetsSubDirectory
    : config.dev.assetsSubDirectory
  return path.posix.join(assetsSubDirectory, _path)
}

exports.cssLoaders = function (options) {
  options = options || {}

  const cssLoader = {
    loader: 'css-loader',
    options: {
      minimize: process.env.NODE_ENV === 'production',
      sourceMap: options.sourceMap
    }
  }

  // generate loader string to be used with extract text plugin
  function generateLoaders (loader, loaderOptions) {
    const loaders = [cssLoader]
    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    // if removed the build fails and I have no idea why....
    if (options.extract) {
      return ExtractTextPlugin.loader({
        use: loaders,
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }

  let sassOptions = {
    indentedSyntax: true
  }
  let scssOptions = {
    includePaths: [
      '~src/scss'
    ],
    data: '@import "~src/scss/main.scss";'
  }

  // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  return {
    css: generateLoaders(),
    postcss: generateLoaders(),
    less: generateLoaders('less'),
    // sass: generateLoaders('sass', sassOptions),
    // scss: generateLoaders('sass'),
    stylus: generateLoaders('stylus'),
    styl: generateLoaders('stylus')
  }
}

// Generate loaders for standalone style files (outside of .vue)
exports.styleLoaders = function (options) {
  const output = []
  const loaders = exports.cssLoaders(options)
  for (const extension in loaders) {
    const loader = loaders[extension]
    output.push({
      test: new RegExp('\\.' + extension + '$'),
      use: loader
    })
  }
  return output
}

And all I did was remove ExtractTextWebpackPlugin and inserted MiniCssExtractPlugin:

'use strict'

const path = require('path')
const config = require('../config')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')

exports.assetsPath = function (_path) {
  const assetsSubDirectory = process.env.NODE_ENV === 'production'
    ? config.build.assetsSubDirectory
    : config.dev.assetsSubDirectory
  return path.posix.join(assetsSubDirectory, _path)
}

exports.cssLoaders = function (options) {
  options = options || {}

  const cssLoader = {
    loader: 'css-loader',
    options: {
      minimize: process.env.NODE_ENV === 'production',
      sourceMap: options.sourceMap
    }
  }

  // generate loader string to be used with extract text plugin
  function generateLoaders (loader, loaderOptions) {
    const loaders = [cssLoader]
    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    // if removed the build fails and I have no idea why....
    if (options.extract) {
      return MiniCssExtractPlugin.loader({
        use: loaders,
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }

  let sassOptions = {
    indentedSyntax: true
  }
  let scssOptions = {
    includePaths: [
      '~src/scss'
    ],
    data: '@import "~src/scss/main.scss";'
  }

  // https://vue-loader.vuejs.org/en/configurations/extract-css.html
  return {
    css: generateLoaders(),
    postcss: generateLoaders(),
    less: generateLoaders('less'),
    // sass: generateLoaders('sass', sassOptions),
    // scss: generateLoaders('sass'),
    stylus: generateLoaders('stylus'),
    styl: generateLoaders('stylus')
  }
}

// Generate loaders for standalone style files (outside of .vue)
exports.styleLoaders = function (options) {
  const output = []
  const loaders = exports.cssLoaders(options)
  for (const extension in loaders) {
    const loader = loaders[extension]
    output.push({
      test: new RegExp('\\.' + extension + '$'),
      use: loader
    })
  }
  return output
}

I do get that the compiler complains that extract is not a function on MiniCssExtractPlugin and that this is where the problem lies. But I cannot for the life of me figure out what the function did before that's so important that it breaks my build and f*cks up my css.

I cannot stress this enough. I am new to Webpack. And I have been stuck for three days. Please help.

like image 438
IncrediblePony Avatar asked Oct 20 '25 04:10

IncrediblePony


1 Answers

I was using the plugin incorrectly it seems. I changed by adding this to my build configs

new MiniCssExtractPlugin({
        filename: `assets/css/[name].${dateStamp}.[contentHash].css`
}),

I omitted the ExtractTextPlugin everywhere else and just let the normal/common loaders handle my dev build.

like image 122
IncrediblePony Avatar answered Oct 23 '25 14:10

IncrediblePony