最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Watch imported css files in webpack + postcss-loader - Stack Overflow

matteradmin5PV0评论

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.
Post a comment

comment list (0)

  1. No comments so far