【问题标题】:How do I configure webpack for scss and bootstrap with ASP.NET project?如何使用 ASP.NET 项目为 scss 和 bootstrap 配置 webpack?
【发布时间】:2020-02-07 00:52:38
【问题描述】:

我已经启动并运行了 webpack,但不知道如何编译 .scss 文件以用于自定义样式(如颜色等)。否则,Bootstrap 可以正常加载。

这是我的 webpack.config.js

const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = (env = {}, argv = {}) => {

    const isProd = argv.mode === 'production';

    const config = {
        mode: argv.mode || 'development', // we default to development when no 'mode' arg is passed

        optimization: {
            minimize: true
        },
        entry: {
            main: './src/main.js'
        },
        output: {
            filename: isProd ? 'bundle-[chunkHash].js' : '[name].js',
            path: path.resolve(__dirname, '../wwwroot/dist'),
            publicPath: "/dist/"
        },
        plugins: [
            new MiniCssExtractPlugin({
                filename: isProd ? 'style-[contenthash].css' : 'style.css'
            }),
            new CompressionPlugin({
                filename: '[path].gz[query]',
                algorithm: 'gzip',
                test: /\.js$|\.css$|\.html$|\.eot?.+$|\.ttf?.+$|\.woff?.+$|\.svg?.+$/,
                threshold: 10240,
                minRatio: 0.8
            }),
            new HtmlWebpackPlugin({
                template: '_LayoutTemplate.cshtml',
                filename: '../../Views/Shared/_Layout.cshtml', //the output root here is /wwwroot/dist so we ../../      
                inject: false
            })
        ],
        module: {
            rules: [
                {
                    test: /\.js$/,
                    exclude: /node_modules/,
                    use: {
                        loader: "babel-loader"
                    }
                },
                {
                    test: /\.(sa|sc|c)ss$/,
                    use: [
                        'style-loader',
                        MiniCssExtractPlugin.loader,
                        'css-loader',
                        'sass-loader'
                    ]
                },
                {
                    test: /\.(png|jpg|gif|woff|woff2|eot|ttf|svg)$/,
                    loader: 'file-loader',
                    options: {
                        name: '[name].[hash].[ext]',
                        outputPath: 'assets/'
                    }
                }
            ]
        }
    };
    return config;
};

我安装了我认为可以编译的 node-sass?只是不知道如何进行。

谢谢,

【问题讨论】:

  • 当你构建时,它会编译到你的 dist 文件夹吗?它只是无法在您的开发服务器上运行吗?
  • 不是没有,我不完全确定在哪里定义scss文件,所以这可能是问题的一部分。

标签: javascript asp.net twitter-bootstrap webpack


【解决方案1】:

您需要将 CSS 导入您的入口点。所以在main.js,像这样导入你的scss文件...

import './path-to/file.scss

【讨论】:

    猜你喜欢
    • 2017-11-10
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 2019-10-12
    • 2020-03-18
    相关资源
    最近更新 更多