簡體   English   中英

webpack css loader不能正常工作:沒有輸出css文件

[英]webpack css loader not work: no output css file

我正在開發opensource項目,現在我需要為前端設置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文件。

在此輸入圖像描述

這是我的項目文件夾

在此輸入圖像描述

難道你沒有忘記在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文檔中的本指南

剛剛完成@denysdovhan:

當使用webpack 4+時,你需要在package.json中提供屬性sideEffect:

{“name”:“your-project”,“sideEffects”:false}

你可以在這里看到解釋: https//webpack.js.org/guides/tree-shaking/#mark-the-file-as-side-effect-free

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM