【问题标题】:Webpack and Precss not reloading on @import file changeWebpack 和 Precss 不会在 @import 文件更改时重新加载
【发布时间】:2016-05-23 07:39:43
【问题描述】:

我正在设置一个基本的 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 时才会刷新。

标签: webpack reload webpack-dev-server postcss webpack-hmr


【解决方案1】:

在网上大量搜索后,可以在以下两个线程中找到答案:

  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 或基于它的解决方案。

【讨论】:

    猜你喜欢
    • 2018-03-03
    • 2017-08-01
    • 2014-12-06
    • 1970-01-01
    • 2020-08-08
    • 2019-04-18
    • 2019-05-14
    • 2018-12-03
    • 1970-01-01
    相关资源
    最近更新 更多