Webpack CSS加载器不起作用:没有输出CSS文件。

6
我正在开发一个开源项目,现在需要为前端设置webpack。我尝试了下面的webpack配置,JS可以正常工作,但CSS不行,没有CSS输出文件。我不知道为什么会这样,请帮忙解决。
以下是我的webpack配置JS文件:
const path = require("path");
const webpack = require('webpack');
const ExtractTextPlugin = require("extract-text-webpack-plugin");

const node_dir = __dirname + '/node_modules';

const sassLoaders = [
  'css-loader',
  'sass-loader?indentedSyntax=sass&includePaths[]=' + path.resolve(__dirname, './public/stylesheets/')
]

const config = {
    addVendor: function (name, path) {
        this.resolve.alias[name] = path;
        this.module.noParse.push(new RegExp('^' + name + '$'));
    },

    entry: {
        app: './public/javascripts/app.js',
    vendors: ['jquery','underscore']
    },

    output: {
        path: path.join(__dirname, process.env.NODE_ENV === 'production' ? './public/dist' : './public/dev'),
        filename: process.env.NODE_ENV === 'production' ? '[name].[hash].js' : '[name].js',
        publicPath: 'public/dev'
    },

    module:{
        noParse: [],
        loaders:[
            {
                test: /\.scss$/,
                loader: ExtractTextPlugin.extract('style-loader', sassLoaders.join('!'))
            },
            {
                test: /\.js$/,
                exclude: /node_modules/,
                loader:'babel?presets[]=es2015'
            }
        ]
    },

    resolve: {
        alias: {},
        extensions: ['', '.js', '.scss'],
    },

    plugins: [
        new webpack.NoErrorsPlugin(),
        new ExtractTextPlugin("qor.css", {
            allChunks: true
        }),
        new webpack.optimize.CommonsChunkPlugin('vendors', 'vendors.bundle.js'),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            "window.jQuery": "jquery"
        }),
        new webpack.ProvidePlugin({
          _: "underscore",
        })
    ]
};

config.addVendor('jquery', node_dir + '/jquery/dist/jquery.js');
config.addVendor('underscore', node_dir + '/underscore/underscore.js');

module.exports = config;

当我运行webpack -d --watch --progress --colors时,没有CSS文件输出,只有JS文件。

enter image description here

这是我的项目文件夹

enter image description here

2个回答

8
你是否忘记在js入口点使用require引入你的样式表了呢?
// in your modules just require the stylesheet
// This has the side effect that a <style>-tag is added to the DOM.
require("./stylesheet.css");

如果您想生成本地的CSS输出文件,请查看webpack文档中的此指南

1

我将成为一个有用的助手,协助您进行翻译。

当使用webpack4+时,您需要在package.json中提供sideEffect属性:

{
  "name": "your-project",
  "sideEffects": false
}

你可以在这里查看解释:链接

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