Webpack 4-从ExtractTextPlugin设置MiniCssExtractPlugin

问题描述 投票:0回答:1

我一直在尝试构建我的应用程序。我是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
}

我确实知道编译器抱怨extract不是MiniCssExtractPlugin上的函数,而这就是问题所在。但是我无法终生弄清楚函数的作用,而在此之前,函数是如此重要,以至于破坏了我的构建并搞砸了我的CSS。

我对此不够强调。我是Webpack的新手。我被困了三天。请帮助。

webpack
1个回答
0
投票

我使用的插件似乎不正确。我通过将其添加到我的构建配置中进行了更改]

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

我在其他任何地方都省略了ExtractTextPlugin,只是让普通/通用加载器处理我的开发构建。

© www.soinside.com 2019 - 2024. All rights reserved.