Webpack和Precss在@import文件更改时没有重新加载

3
我正在设置一个基本的Webpack安装,并希望使用PostCSSPreCSS插件,在@imported文件中自动重新加载预处理的CSS。目前,如果我修改并保存一个@imported文件,浏览器不会刷新(在下面的示例中为body.css)。如果我保存根引用的CSS文件(styles.css),则浏览器将刷新,并反映对@imported文件所做的任何更改。
我已经尝试使用可配置的webpack-dev-server,并使用server.js。我已经尝试过安装热模型重载(HMR)和不安装。
是否有一种方法可以让Webpack监视@imported CSS文件,或者我基本上缺少了什么?

package.json

"dependencies": {
  "react": "^0.14.0",
  "react-dom": "^0.14.0"
},
"devDependencies": {
  "autoprefixer": "^6.3.1",
  "css-loader": "^0.23.1",
  "extract-text-webpack-plugin": "^1.0.1",
  "postcss-loader": "^0.8.0",
  "postcss-scss": "^0.1.3",
  "precss": "^1.4.0",
  "react-hot-loader": "^1.3.0",
  "style-loader": "^0.13.0",
  "webpack": "^1.12.13",
  "webpack-dev-server": "^1.14.1"
},
"scripts": {
  "test": "echo \"Error: no test specified\" && exit 1",
  "start": "node server.js",
  "start-dev-server": "webpack-dev-server 'webpack-dev-server/client?/' --host 0.0.0.0 --port 9090 --progress --colors",
  "build": "echo \"Build hasn't been specified yet\" && exit 1"
},

webpack.config.js

/*global require module __dirname*/
var path = require('path'),
    webpack = require('webpack'),
    ExtractTextPlugin = require('extract-text-webpack-plugin'),
    autoprefixer = require('autoprefixer'),
    precss = require('precss');

module.exports = {
    entry: [
    'webpack-dev-server/client?http://localhost:9090',
    'webpack/hot/only-dev-server',
    './entry.js'
  ],
    output: {
        path: path.join(__dirname, 'dist'),
        filename: 'bundle.js',
        publicPath: '/static/'
    },
    devtool: 'source-map',
    module: {
        loaders: [
            { 
                test: /\.css$/i,
                loaders: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap&modules&importLoaders=1!postcss-loader')
            }
        ]
    },
    postcss: function() {
        return [precss, autoprefixer];
    },
    plugins: [
        // Set the name of the single CSS file here.
        new ExtractTextPlugin('main.css', { allChunks: true }),
        new webpack.HotModuleReplacementPlugin()
    ]
};

index.html

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" type="text/css" href="/static/main.css" />
    </head>
    <body>
        <script src="http://localhost:9090/webpack-dev-server.js"></script>
        <script type="text/javascript" src="/static/bundle.js" charset="utf-8"></script>
    </body>
</html>

entry.js

require("./styles.css");
document.write(require("./content.js"));

styles.css

@import "body.css";

body {
     /*background: yellow; */
    font-size: 30px;
}

div {
    display: flex;
}

body.css

$color: yellow;

body {
    background: $color;
}

div {
    color: white;

    a {
        color: green;
    }
}

div {
    display: flex;
}

另外需要注意的是,如果我使用HMR并保存根样式styles.css,则会收到以下消息:以下模块无法进行热更新:(它们需要完全重新加载!),然后我必须刷新页面。如果我不使用HMR,则可以正常刷新,尽管只有在保存根样式styles.css时才能刷新。 - mummybot
1个回答

5

在互联网上搜索了很久,可以在以下两个主题中找到答案:

  1. https://github.com/postcss/postcss-loader/issues/8
  2. https://github.com/jonathantneal/precss/issues/6

感谢@zzq889提供的示例,使用postcss-import可以解决postcss-partial-import的限制问题:

var postcssImport = require('postcss-import');

...

postcss: function (webpack) {
    return [
        postcssImport({
            addDependencyTo: webpack
        })
    ];
}

希望未来只需使用precss及其依赖库postcss-partial-import,即可实现此功能。这个pull request或基于它的解决方案将有望实现。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接