vue升级webpack4,抽离css样式打包报错

发布于 2022-09-11 23:47:41 字数 7055 浏览 14 评论 0

image.png报错的变量是我代码里面的css类名(cssmodule写法),run dev命令是正常的,build过程没有问题,浏览器打开才看到报错image.png

webpack配置就是在vue-cli基础上改的,如果vue-loader中我不配置这个MiniCssExtractPlugin直接用vue-style-loader就没有问题image.png

尝试过的方法:使用ExtractTextPlugin的beta版,也有类似的错误

部分代码,其他不贴了 主要是vue-cli上修改

//vue-loader.conf.js
module.exports = {
  cssModules:{
      localIdentName:'[local]-[hash:base64:5]'
    },
  loaders: utils.cssLoaders({
    sourceMap: isProduction
      ? config.build.productionSourceMap
      : config.dev.cssSourceMap,
    extract: isProduction
  }),
  postcss:[assets({
            loadPaths:[path.resolve(__dirname,'../src/assets','img')],
            relative:true
        }),
            px2rem({remUnit:75}),
            autoprefixer(),
        ]
}

//util.js我对sass的配置
 function generateSassResourceLoader() {
    var sassResourcesLoader = {
      loader: 'sass-resources-loader',
      options: {
        // it need a absolute path
        resources: [resolveResouce('vars.scss')]
      }
    }
    var loaders = [
      cssLoader,
      // 'postcss-loader',
      'sass-loader',
      sassResourcesLoader
    ];

    if (options.extract) {
      return [MiniCssExtractPlugin.loader].concat(loaders)
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }
  
  //webpack生产环境配置
  var path = require('path')
var utils = require('./utils')
var webpack = require('webpack')
var config = require('../config')
var merge = require('webpack-merge')
var baseWebpackConfig = require('./webpack.base.conf')
var CopyWebpackPlugin = require('copy-webpack-plugin')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var env = config.build.env

var webpackConfig = merge(baseWebpackConfig, {
  mode: 'production',
  module: {
    rules: utils.styleLoaders({
      sourceMap: config.build.productionSourceMap,
      extract: true
    })
  },
  devtool: config.build.productionSourceMap ? '#source-map' : false,
  output: {
    path: config.build.assetsRoot,
    filename: utils.assetsPath('js/[name].[chunkhash].js'),
    chunkFilename: utils.assetsPath('js/[name].[chunkhash].js')
  },

  plugins: [
    new webpack.ContextReplacementPlugin(/protobufjs/, /^$/),
    new LodashModuleReplacementPlugin,
    // http://vuejs.github.io/vue-loader/en/workflow/production.html
    new webpack.DefinePlugin({
      'process.env': env
    }),

    // extract css into its own file

    new MiniCssExtractPlugin({
      filename: utils.assetsPath('css/[name].[hash].css'),
      // chunkFilename: utils.assetsPath('css/[name].[contenthash:12].css')  // use contenthash *
    }),
    // new ExtractTextPlugin({
    //   filename: utils.assetsPath('css/[name].[hash].css')
    // }),

    // generate dist index.html with correct asset hash for caching.
    // you can customize output by editing /index.html
    // see https://github.com/ampedandwired/html-webpack-plugin
    new HtmlWebpackPlugin({
      filename: config.build.index,
      template: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency',
      favicon: './favicon.ico'
    }),

    // copy custom static assets
    new CopyWebpackPlugin([{
      from: path.resolve(__dirname, '../static'),
      to: config.build.assetsSubDirectory,
      ignore: ['.*']
    }])
  ]
})

if (config.build.productionGzip) {
  var CompressionWebpackPlugin = require('compression-webpack-plugin')

  webpackConfig.plugins.push(
    new CompressionWebpackPlugin({
      asset: '[path].gz[query]',
      algorithm: 'gzip',
      test: new RegExp(
        '\\.(' +
        config.build.productionGzipExtensions.join('|') +
        ')$'
      ),
      threshold: 10240,
      minRatio: 0.8
    })
  )
}

if (config.build.bundleAnalyzerReport) {
  var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
  webpackConfig.plugins.push(new BundleAnalyzerPlugin({
    analyzerPort: 9999
  }))
}

module.exports = webpackConfig
"@babel/core": "^7.0.0",
    "@babel/plugin-proposal-class-properties": "^7.0.0",
    "@babel/plugin-proposal-decorators": "^7.0.0",
    "@babel/plugin-proposal-export-namespace-from": "^7.0.0",
    "@babel/plugin-proposal-function-sent": "^7.0.0",
    "@babel/plugin-proposal-json-strings": "^7.0.0",
    "@babel/plugin-proposal-numeric-separator": "^7.0.0",
    "@babel/plugin-proposal-throw-expressions": "^7.0.0",
    "@babel/plugin-syntax-dynamic-import": "^7.0.0",
    "@babel/plugin-syntax-import-meta": "^7.0.0",
    "@babel/plugin-transform-runtime": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/register": "^7.0.0",
    "autoprefixer": "^6.7.2",
    "babel-eslint": "^9.0.0",
    "babel-loader": "^8.0.0",
    "babel-plugin-lodash": "^3.2.11",
    "chalk": "^1.1.3",
    "compression-webpack-plugin": "^1.1.11",
    "connect-history-api-fallback": "^1.3.0",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.11",
    "eslint": "^3.14.1",
    "eslint-config-airbnb-base": "^11.0.1",
    "eslint-friendly-formatter": "^2.0.7",
    "eslint-import-resolver-webpack": "^0.8.1",
    "eslint-loader": "^1.6.1",
    "eslint-plugin-html": "^2.0.0",
    "eslint-plugin-import": "^2.2.0",
    "eventsource-polyfill": "^0.9.6",
    "express": "^4.14.1",
    "extract-text-webpack-plugin": "^4.0.0-beta.0",
    "file-loader": "^1.1.11",
    "friendly-errors-webpack-plugin": "^1.1.3",
    "function-bind": "^1.1.0",
    "html-webpack-plugin": "^3.2.0",
    "http-proxy-middleware": "^0.17.3",
    "less-loader": "^4.0.5",
    "lodash-webpack-plugin": "^0.11.2",
    "mini-css-extract-plugin": "^0.8.0",
    "node-sass": "^4.0.0",
    "opn": "^4.0.2",
    "ora": "^1.1.0",
    "postcss-assets": "^4.1.0",
    "postcss-px2rem": "^0.3.0",
    "postcss-salad": "^2.0.1",
    "qs": "^6.4.0",
    "rimraf": "^2.6.0",
    "sass-loader": "^6.0.3",
    "sass-resources-loader": "^2.0.1",
    "semver": "^5.3.0",
    "url-loader": "^1.0.1",
    "velocity-animate": "^1.4.3",
    "vue-baidu-map": "^0.1.11",
    "vue-loader": "^14.1.1",
    "vue-style-loader": "^4.0.2",
    "vue-template-compiler": "^2.5.2",
    "vux-loader": "^1.2.9",
    "webpack": "^4.20.0",
    "webpack-bundle-analyzer": "^3.0.0",
    "webpack-cli": "^2.0.15",
    "webpack-dev-middleware": "^3.1.3",
    "webpack-dev-server": "^3.1.3",
    "webpack-hot-middleware": "^2.22.1",
    "webpack-merge": "^2.6.1",
    "yaml-loader": "^0.5.0"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文