简体   繁体   English

Webpack 没有加载 css

[英]Webpack not loading css

This is my first time trying to set up Webpack, so I'm sure I'm missing something here.这是我第一次尝试设置 Webpack,所以我确定我在这里遗漏了一些东西。

I am trying to load my PostCSS files with Webpack, using the ExtractTextPlugin to generate a css file into "dist".我正在尝试用 Webpack 加载我的 PostCSS 文件,使用 ExtractTextPlugin 将 css 文件生成到“dist”中。 The problem is Webpack does not seem to pick up the css files.问题是 Webpack 似乎没有提取 css 文件。 They are under "client/styles", but I've tried moving them to "shared", with the same result.它们在“客户端/样式”下,但我尝试将它们移动到“共享”,结果相同。

I ran Webpack with the --display-modules option, and verified that no css files display there.我使用 --display-modules 选项运行 Webpack,并验证那里没有显示任何 css 文件。

I have tried running it without the extract text plugin, and the result is the same: no CSS is built into bundle.js.我试过在没有提取文本插件的情况下运行它,结果是一样的:bundle.js 中没有内置 CSS。

Here is my prod config:这是我的产品配置:

var ExtractTextPlugin = require('extract-text-webpack-plugin');
var path = require('path');

module.exports = {
  entry: [
    './client'
  ],
  resolve: {
    modulesDirectories: ['node_modules', 'shared'],
    extensions: ['', '.js', '.jsx', '.css']
  },
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js',
    chunkFilename: '[id].js',
    publicPath: '/'
  },
  module: {
    loaders: [
      {
        test:    /\.jsx?$/,
        exclude: /node_modules/,
        loaders: ['babel']
      },

      {
        test: /\.css?$/,
        loader: ExtractTextPlugin.extract(
          'style-loader',
          'css-loader!postcss-loader'
        ),
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    new ExtractTextPlugin('[name].css')
  ],
  postcss: (webpack) => [
    require('postcss-import')({ addDependencyTo: webpack, path: ['node_modules', 'client'] }),
    require('postcss-url')(),
    require('precss')(),
    require('postcss-fontpath')(),
    require('autoprefixer')({ browsers: [ 'last 2 versions' ] })
  ]
};

And here's an example of my main css file: @import 'normalize.css/normalize';这是我的主要 css 文件的示例:@import 'normalize.css/normalize';

/* Variables */
@import 'variables/colours';

/* Mixins */

/* App */

/* Components */

body {
  background-color: $black;
}

Would anyone have an idea on why this is happening?有人知道为什么会这样吗? Am I missing something?我错过了什么吗?

Thank you谢谢

So, after three hours of hitting my head against the wall, I finally got it.所以,在用头撞墙三个小时后,我终于明白了。 I hope this will help someone in the future.我希望这会在将来帮助某人。

All I needed to do was to add './client/styles/main.css' to the entry points.我所需要做的就是将'./client/styles/main.css'添加到入口点。 Yey.是的。

Since you are using style-loader and css-loader.由于您使用的是 style-loader 和 css-loader。 You can include css in the js file itself.您可以在 js 文件本身中包含 css。 You can just require(style.css) or import 'style.css' (if using ES6) in the javascript file which is using the styles.您可以在使用样式的 javascript 文件中只require(style.css)import 'style.css' (如果使用 ES6)。 No need to provide an entry point to webpack for css.无需为webpack for css 提供入口点。

Hope it helps.希望能帮助到你。

Not directly related to the OP's problem, but here was my problem (my CSS wasn't loading either).与 OP 的问题没有直接关系,但这是我的问题(我的 CSS 也没有加载)。

Personally, I was missing the style-loader package: https://webpack.js.org/guides/hot-module-replacement/#hmr-with-stylesheets就个人而言,我缺少style-loader包: https ://webpack.js.org/guides/hot-module-replacement/#hmr-with-stylesheets

For me, I had "sideEffects": false in my package.json, which caused webpack not to emit CSS.对我来说,我的 package.json 中有"sideEffects": false ,这导致 webpack 不发出 CSS。

In my case it was related with react-hot-loader .在我的例子中,它与react-hot-loader有关。 I was using webpack.HotModuleReplacementPlugin() and after deleting it CSS in my editor started working.我正在使用 webpack.HotModuleReplacementPlugin() 并在我的编辑器中删除它后 CSS 开始工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM