javascript - 带有 Webpack 的 sass-loader 不会生成 CSS

标签 javascript css express webpack sass-loader

如何使用 sass-loader 从 scss 生成 css 文件并使用express.js 将其嵌入到 html 中。我也在使用react-hot-loader

下面是我的配置文件

var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var HTMLWebpackPlugin = require('html-webpack-plugin')
var HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
            template: __dirname + '/app/index.html',
            filename: 'index.html',
            inject: 'body'
        });


const path = require('path');

module.exports = {

    entry: ['react-hot-loader/patch',
            'webpack-hot-middleware/client?path=http://localhost:3000/__what',
            'webpack/hot/only-dev-server', //<- doesn’t reload the browser upon syntax errors. This is recommended for React apps because it keeps the state
            //'webpack/hot/dev-server', //<- To perform HMR in the browser reloads if HMR update fails.
            __dirname + '/app/index.js'],

    context: __dirname,
    module:{
        loaders:[
            {
                test:/\.js$/,
                exclude: /node_modules/,
                loader:'babel-loader',
                query:{
                    presets:["react","es2015","stage-2"],
                    plugins: ["react-hot-loader/babel"]
                }
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                loader: ExtractTextPlugin.extract({fallback:'style-loader' ,use:['css-loader','sass-loader']}),
                include:path.resolve(__dirname, '/app/scss') 
            }
        ]
    },
    output:{
        filename:'payload-min.js',
        path: __dirname + '/build',
        publicPath: __dirname + '/build'
    },
    plugins:[HTMLWebpackPluginConfig ,
             new ExtractTextPlugin(__dirname + '/build/payload.css', {
                                  allChunks: true
             }),
             new webpack.HotModuleReplacementPlugin(),
             new webpack.NoEmitOnErrorsPlugin()]

};

index.js

...
require('css-loader!sass-loader!./scss/payload.scss');

我从来没有看到下面的插件在运行

new ExtractTextPlugin(__dirname + '/build/payload.css', {
                                      allChunks: true
                 }),

执行

webpack -d 或 webpack 不会在/build 下生成 Payload.css 文件

index.html

<link rel="stylesheet" type="text/css" href="build/payload.css">

我的目录结构

.
├── app
│   ├── actions
│   │   └── index.js
│   ├── common
│   │   └── constants.js
│   ├── components
│   │   ├── App.js
│   │   ├── DetailsSection.js
│   │   ├── Device.js
│   │   ├── FirmwareImageDetails.js
│   │   ├── Menu.js
│   │   ├── Terminal.js
│   │   └── ViewJson.js
│   ├── index2.html
│   ├── index.html
│   ├── index.js
│   ├── reducers
│   │   ├── index.js
│   │   └── localStorage.js
│   └── scss
│       └── payload.scss
├── build
├── mock_payload.json
├── package.json
├── package-lock.json
├── payload_schema.json
├── README.md
├── server.js
├── webpack.config.js
└── webpack.server.config.js

最佳答案

问题在于

include:path.resolve(__dirname, '/app/scss') 

在加载器中删除此行生成了 css 文件。

关于javascript - 带有 Webpack 的 sass-loader 不会生成 CSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45941172/

相关文章:

php - 在文本区域输入字段中显示换行符

html - 删除应用的 CSS 转换

node.js - 获取 JSON

javascript - 使 div 可点击并跟随子 anchor 元素

JavaScript 换行符

javascript - 访问 Twilio SMS 元数据?

javascript - JS : querySelectorAll with respect to z-index

node.js - Socket.io app ...我应该使用emit还是使用常规http请求并用express处理它?

javascript - Promise 中的异常处理

javascript - 如何通过<th>值获取表<td>内容