简体   繁体   English

使用Webpack和angular 5错误加载了SCSS样式

[英]SCSS styles incorrectly loaded using webpack and angular 5

I'm trying to load my scss styles during AOT compilation with Angular 5. Without AOT all works as expected but with AOT my css file looks almost the same as scss file. 我正在尝试使用Angular 5在AOT编译期间加载我的scss样式。如果没有AOT,所有的工作都可以按预期进行,但是使用AOT时,我的css文件看起来几乎与scss文件相同。

scss file: scss文件:

@import "bootstrap/bootstrap";
@import "rtl";
@import "responsive";
@import "formInvalid";
$fa-font-path: "~font-awesome/fonts/";
@import "../../node_modules/font-awesome/scss/font-awesome";

.app-content {
    margin-left: 235px;
    margin-right: 0;
    padding: 100px 20px;
    overflow-y: auto;

    .list-group-item {
        cursor: pointer;
    }

    .w-100p {
        width: 100%;
    }
}

loaded file: 加载的文件:

.app-content {
    margin-left: 235px;
    margin-right: 0;
    padding: 100px 20px;
    overflow-y: auto; }
    .app-content .list-group-item {
    cursor: pointer; }
    .app-content .w-100p {
    width: 100%; }

my webpack configuration: 我的webpack配置:

var webpack = require('webpack');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var helpers = require('./helpers.js');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
var path = require('path');

const extractSass = new ExtractTextPlugin({
    filename: "../../css/bundle.css"
});

module.exports = {
    entry: {
        'app': './src/app/main.aot.ts'
    },

    output: {
        path: path.resolve(__dirname + '/public/js/app'),
        filename: 'bundle.js',
        publicPath: '/js/app/',
        chunkFilename: '[id].[hash].chunk.js'
    },

    resolve: {
        extensions: ['.js', '.ts']
    },

    module: {
        rules: [
            {
                test: /\.ts$/,
                use: [
                'awesome-typescript-loader',
                'angular2-template-loader',
                'angular-router-loader?aot=true'
                ]
            },
            {
                test: /\.html$/,
                use: [{ loader: 'html-loader' }]
            },
            {
                test: /\.(jpe|jpg|png|woff|woff2|eot|ttf|svg)(\?.*$|$)/,
                use: [{ loader: 'file-loader' }]
            },
            {
                test: /\.css$/,
                loaders: ['exports-loader?module.exports.toString()', 
'css-loader']
            },
            {
                test: /\.scss$/,
                use: extractSass.extract({
                    use: [{
                        loader: "css-loader"
                    }, {
                        loader: "sass-loader"
                    }],
                    fallback: "style-loader"
                })

            }
        ],
    },

    plugins: [
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: false
        }),
        extractSass
    ]};

I am importing component styles like this: 我正在导入这样的组件样式:

styleUrls: ['./signin.component.scss'],

And global scss file wit other dependecies imported like this: 全局scss文件具有其他依赖项,如下所示:

import "../styles/app.scss"; 

in my app.component.ts file 在我的app.component.ts文件中

The problem occures also in global style and in component styles. 在全局样式和组件样式中也会出现此问题。 I have tried all possible webpack configurations with and without extract-text-plugin but nothing works. 我尝试了所有可能的webpack配置,无论是否带有extract-text-plugin,但没有任何效果。

I am not using angular cli. 我没有使用角度cli。

Thanks for your help. 谢谢你的帮助。

加载的CSS文件看起来完全没有被格式化的部分,但是,如果您将加载的文件的内容放入IDE并进行格式化,则会发现CSS实际上是您在SCSS中编写的内容的绝对正确的表示形式,只需将其转换为CSS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM