【问题标题】:Webpack output buffer injected into CSS - how to prevent?注入 CSS 的 Webpack 输出缓冲区 - 如何防止?
【发布时间】:2021-02-03 20:26:00
【问题描述】:

编辑:这个问题现在是空的(至少对我来说)。我发现我的升级故障。

Webpack v4.31.0

我不是大师。我试图升级到 v5,但一切都坏了。我快到最后期限了,所以请尽可能将建议限制在 v4。

给定这个webpack.config.js 来编译一个 SCSS 文件:

// const js removed for simplicity

const css = {
  entry: {
    'frontend.css': `${__dirname}/src/scss/frontend.scss`,
  },
  output: {
    path: `${__dirname}/css`,
    filename: '[name]'
  },
  module: {
    rules: [
      {
        test: /\.scss$/,
        exclude: /node_modules/,
        // module chain executes from last to first?
        use: [
          {
            loader: 'file-loader',
            options: { name: '[name].css', outputPath: '../css/' }
          },
          // how to minimize?
          { loader: "remove-comments-loader" },
          { loader: 'extract-loader' },
          { loader: 'css-loader', options: { url: false, sourceMap: false } },
          { loader: 'resolve-url-loader' },
          { loader: 'sass-loader', options: { sourceMap: false } }
        ]
      },
    ]
  }
};

// Return array of configurations
module.exports = function () {
  return exportModules( [css] );
};

/**
 * Merge filetype configs with shared config and return them as an array of objects.
 * @param objs
 * @return {Array}
 */
const exportModules = ( objs ) => {
  const objArr = [];
  for ( let i = 0; i < objs.length; i++ ) {
    objArr.push( {
      ...config(),
      ...objs[i]
    } );
  }
  return objArr;
};

// Shared config options
const config = function () {
  return {
    mode: 'development',
    stats: {
      // is there a preset that does this?
      colors: true,
      hash: false,
      version: false,
      timings: false,
      assets: false,
      chunks: false,
      modules: false,
      reasons: false,
      children: false,
      source: false,
      errors: false,
      errorDetails: false,
      warnings: false,
      publicPath: false
    }
  }
};

和config根目录下的这个命令:

$ webpack

正在编译和渲染生成的 CSS 文件,其中包含来自 lib/MainTemplate.js 的一堆看似语句的内容

/*** see below ***/

#splash-overlay {
  position: fixed;
  z-index: 25000000;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: #120016;
  display: flex;
  justify-content: center;
  align-items: center;
}

.splash-link {
  text-align: center;
  display: inline-block;
  width: 100%;
}exports, name, { enumerable: true, get: getter });
/******/        }
/******/    };
/******/
/******/    // define __esModule on exports
/******/    __webpack_require__.r = function(exports) {
/******/        if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/            Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/        }
/******/        Object.defineProperty(exports, '__esModule', { value: true });
/******/    };
/******/
/******/    // create a fake namespace object
/******/    // mode & 1: value is a module id, require it
/******/    // mode & 2: merge all properties of value into the ns
/******/    // mode & 4: return value when already ns object
/******/    // mode & 8|1: behave like require
/******/    __webpack_require__.t = function(value, mode) {
/******/        if(mode & 1) value = __webpack_require__(value);
/******/        if(mode & 8) return value;
/******/        if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/        var ns = Object.create(null);
/******/ 

这种行为并不一致,有时会输出不同的语句,有时根本不输出。有没有办法绕过配置对象中的这个输出?我做错了吗?

【问题讨论】:

    标签: javascript css webpack sass webpack-4


    【解决方案1】:

    在我看来,exportModules 函数中可能存在错误。我不确定,尤其是我使用 webpack v5,但它可能无法正确合并,覆盖或复制一些共享配置。

    也许你应该使用 webpack-merge 包来解决这个问题,然后它可能看起来像

    /* Top of the file */
    const { merge } = require('webpack-merge');
    
    modules.export = merge(config(), css, js);
    

    【讨论】:

    • 谢谢 Bart,我最终解决了我的升级问题,现在我正在使用 v5,试图找出拜占庭配置选项,这似乎与 v4 相比发生了显着变化。我现在有一个新问题,就是我的 css 输出目录中出现了一个神秘的 .js 文件。这不是破坏交易;我可以放心地忽略它。无论如何,感谢您抽出宝贵的时间。 Webpack 似乎是一个强大的工具,但试图找到有意义的例子来做我想做的事情证明是困难的。
    • 我知道痛苦。最近,当我使用mini-css-extract-plugin 提取 css 条目时,我处理了 webpack 输出空 js 文件的事实。似乎只有webpack's bug 可以持续数年。 webpack-fix-style-only-entries 有一个解决方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    相关资源
    最近更新 更多