admin管理员组

文章数量:1434409

I'm having a bit of trouble getting Webpack in bination with the postcss-loader to watch imported css files. They're being processed on the first run, but webpack does not repile when I modify these files.

E.g.

I have my main css file where I import all css modules:

...
/* Base imports */
@import "base/base-imports";
...

In base imports I have for the sake of example applied a color to the body:

body {
  background: tomato;
}

I now set the background to another color tho debug whether the css file is reloaded, but isn't.

This is my webpack config:

var webpack           = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin')

var autoprefixer  = require('autoprefixer');
var precss        = require('precss');
var fontMagician  = require('postcss-font-magician');
var atImport      = require('postcss-import');



module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname + '/dist',
    publicPath: '/',
    filename: 'bundle.js',
  },
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: "style-loader!css-loader!postcss-loader"
      }
    ],
  },
  postcss: function(webpack) {
    return [
      autoprefixer({ browsers: ['last 2 versions'] }),
      precss,
      fontMagician,
      atImport({
        path: './src/styles/*.css',
        addDependencyTo: webpack
      }),
    ];
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Custom template',
      template: 'src/index.html', // Load a custom template
      inject: 'body' // Inject all scripts into the body
    })
  ],
  devtool: 'source-map',
  devServer: {
    contentBase: './dist',
    hot: true
  },
}

I'm having a bit of trouble getting Webpack in bination with the postcss-loader to watch imported css files. They're being processed on the first run, but webpack does not repile when I modify these files.

E.g.

I have my main css file where I import all css modules:

...
/* Base imports */
@import "base/base-imports";
...

In base imports I have for the sake of example applied a color to the body:

body {
  background: tomato;
}

I now set the background to another color tho debug whether the css file is reloaded, but isn't.

This is my webpack config:

var webpack           = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin')

var autoprefixer  = require('autoprefixer');
var precss        = require('precss');
var fontMagician  = require('postcss-font-magician');
var atImport      = require('postcss-import');



module.exports = {
  entry: [
    './src/index.js'
  ],
  output: {
    path: __dirname + '/dist',
    publicPath: '/',
    filename: 'bundle.js',
  },
  module: {
    loaders: [
      {
        test: /\.css$/,
        loader: "style-loader!css-loader!postcss-loader"
      }
    ],
  },
  postcss: function(webpack) {
    return [
      autoprefixer({ browsers: ['last 2 versions'] }),
      precss,
      fontMagician,
      atImport({
        path: './src/styles/*.css',
        addDependencyTo: webpack
      }),
    ];
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Custom template',
      template: 'src/index.html', // Load a custom template
      inject: 'body' // Inject all scripts into the body
    })
  ],
  devtool: 'source-map',
  devServer: {
    contentBase: './dist',
    hot: true
  },
}
Share Improve this question asked Jan 15, 2016 at 7:54 HoetmaaiersHoetmaaiers 3,5032 gold badges23 silver badges33 bronze badges 1
  • 3 Could you try to move atImport before precss? I guess that your imports is processed by precss instead of postcss-import. – Kreozot Commented Jan 15, 2016 at 13:18
Add a ment  | 

1 Answer 1

Reset to default 5

Okay,

This question had 2 things wrong, so the solution is:

  1. Do atImport before precss, thank you @Kreozot.
  2. Reference to the exact css filename. With this I mean is: @import "base/_base-imports" instead of @import "base/base-imports" if the filename is "_base-imports". It is not resolved like a sass partial.

本文标签: javascriptWatch imported css files in webpackpostcssloaderStack Overflow