如何在html中导入sass和scss文件(不使用ruby)?

时间:2017-11-23 15:02:42

标签: javascript html sass compass-sass

我尝试在html中导入sass和scss 我找到了所有答案,但所有答案都是导入sass.js文件 所以我试着像这样

<link rel="stylesheet/sass" type="text/css" href="assets/css/style.sass?<?php echo time();?>" />
<script src="assets/js/sass.min.js"></script>
and
<link rel="stylesheet/sass" type="text/css" href="assets/css/style.scss?<?php echo time();?>" />
<script src="assets/js/sass.min.js"></script>

但它不起作用,我可以在控制台上看到(主要准备前时间:8毫秒) 我想知道如何在没有Ruby的情况下解决这个问题 请帮帮我,谢谢

1 个答案:

答案 0 :(得分:0)

您可以看一下这个示例

const path = require("path");
const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const CompressionPlugin = require("compression-webpack-plugin");
const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
const WebpackShellPlugin = require('webpack-shell-plugin');
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");

process.traceDeprecation = true;

module.exports = {
    entry: {
        admin: "./React/js/App/Modules/Admin/admin.js",
        client: "./React/js/App/Modules/Client/client.js",
        login: "./React/js/EntryPoint/Modules/Admin/Account/Login/Login.js",
        password: "./React/js/EntryPoint/Modules/Admin/Account/Password/Password.js",
        manageaccount: "./React/js/EntryPoint/Modules/Admin/Account/Index.js",
        categories: "./React/js/EntryPoint/Modules/Admin/Categories/Categories.js",
        tag: "./React/js/EntryPoint/Modules/Admin/Tag/Tag.js",
        post: "./React/js/EntryPoint/Modules/Admin/Post/Post.js",
        comment: "./React/js/EntryPoint/Modules/Admin/Comment/Comment.js",
        portal: "./React/js/EntryPoint/Modules/Admin/Portal/PortalIndex.js",
        ClientIndex: "./React/js/EntryPoint/Modules/Client/Index/ClientIndex.js",
        adminSettings: "./React/js/EntryPoint/Modules/Admin/Settings/Settings.js",
        socialLinks: "./React/js/EntryPoint/Modules/Shared/SocialLinks.js"
    },
    output: {
        path: path.resolve(__dirname, "wwwroot/dist"),
        filename: "[name].js",
        publicPath: "/dist/"
    },
    optimization: {
        minimizer: [
            new UglifyJsPlugin({
                cache: true,
                parallel: true,
                sourceMap: false,
                extractComments: 'all',
                uglifyOptions: {
                    compress: true,
                    output: null
                }
            }),
            new OptimizeCSSAssetsPlugin({
                cssProcessorOptions: {
                    safe: true,
                    discardComments: {
                        removeAll: true,
                    },
                },
            })
        ]
    },
    plugins: [
        new webpack.ContextReplacementPlugin(/\.\/locale$/, 'empty-module', false, /jsx$/),
        new webpack.LoaderOptionsPlugin({
            options: {}
        }),
        new MiniCssExtractPlugin({
            filename: "[name].css",
            chunkFilename: "[id].css"
        }),
        new webpack.ProvidePlugin({
            $: "jquery",
            jQuery: "jquery",
            Popper: ['popper.js', 'default']
        }),
        new CompressionPlugin({
            test: /\.(js|css)/
        }),
        new UglifyJsPlugin(),
        new WebpackShellPlugin({
            onBuildStart: ['echo "Starting postcss command"'],
            onBuildEnd: ['postcss --dir wwwroot/dist wwwroot/dist/*.css']
        })
    ],
    resolve: {
        modules: [
            path.resolve('./React/js/App'),
            path.resolve('./React/js/App/Modules/Client'),
            path.resolve('./React/js/App/Modules/Adnmin'),
            path.resolve('./node_modules')
        ]
    },
    module: {
        rules: [{
                test: /\.scss$/,
                use: [
                    'style-loader',
                    MiniCssExtractPlugin.loader,
                    {
                        loader: "css-loader",
                        options: {
                            minimize: true,
                            sourceMap: true
                        }
                    },
                    {
                        loader: "sass-loader"
                    }
                ]
            },
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: ["babel-loader", "eslint-loader"]
            },
            {
                test: /\.(jpe?g|png|gif)$/i,
                loader: "file-loader"
            },
            {
                test: /\.(woff|ttf|otf|eot|woff2|svg)$/i,
                loader: "file-loader"
            }
        ]
    }
};

在模块/规则部分中,我使用sass加载器,css加载器和样式加载器将scss转换为css文件,然后将其输出到dist文件夹。您将使用MiniCssExtractPlugin将CSS文件提取到admin.css

然后在我的js文件中,我将这样导入scss文件

import "../../../../css/Admin/admin.scss";

我的源代码可以找到here