Webpack 4 - 使用MiniCssExtractPlugin设置ExtractTextPlugin

3

我一直在尝试构建我的应用程序。我对Webpack很陌生,当我尝试在分配给我的应用程序上进行生产构建时,出现了以下消息:

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)

我在应用程序设置方面进行了很多更新,其中之一是从Webpack 3升级到4。这意味着ExtractTextPlugin已被弃用,应该使用MiniCssExtractPlugin插件。我的build/utils.js文件在更新之前看起来像这样:

'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
}

我所做的只是删除了ExtractTextWebpackPlugin并添加了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
}

我明白编译器抱怨MiniCssExtractPlugin上没有函数“extract”,这就是问题所在。但我无论如何都无法弄清楚这个函数以前做了什么,它为什么如此重要,导致我的构建出现问题并且损坏了我的CSS。
我再次强调,我是Webpack的新手。而且我已经卡了三天了。请帮帮我。
1个回答

3

看起来我之前使用插件的方式不正确。我通过将这个添加到我的构建配置中进行了更改。

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

我在其他所有地方都省略了ExtractTextPlugin,只让正常/通用的加载器处理我的开发构建。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接