Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
548 views
in Technique[技术] by (71.8m points)

javascript - 使用加载器将SASS文件编译为JavaScript中的CSS字符串(Compiling SASS file into CSS string in JavaScript with loader)

I have a situation where I need to get the CSS string generated from a SASS file which has been compiled in JavaScript.

(我有一种情况,我需要获取从用JavaScript编译的SASS文件生成的CSS字符串。)

I have got a solution working with just CSS, using raw-loader

(我有一个使用raw-loader的仅使用CSS的解决方案)

https://github.com/webpack-contrib/raw-loader

(https://github.com/webpack-contrib/raw-loader)

Using the following code...

(使用以下代码...)

import css from '!!raw-loader!../css/styles.css';
console.log(css);

However for this I need to compile the SCSS manually before I do this step which I don't really want to do.

(但是,为此,在执行此步骤之前,我确实不需要手动编译SCSS。)

I would prefer to do something like this... notice it is an SCSS rather than CSS file

(我宁愿做这样的事情...请注意,这是一个SCSS而不是CSS文件)

import css from '!!raw-loader!../css/styles.scss';
console.log(css);

But this returns some JavaScript instead, I already have a SCSS loader in my webpack config which looks like this.

(但这返回了一些JavaScript,我的webpack配置中已经有一个SCSS加载器,如下所示。)

        {
            test: /.s[ac]ss$/i,
            use: [
                {
                    loader: 'style-loader',
                    options: {
                        insert: 'head', // insert style tag inside of <head>
                        injectType: 'singletonStyleTag' // this is for wrap all your style in just one style tag
                    }
                },
                'css-loader',
                'sass-loader'
            ]
        },
  ask by user3284707 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have figured out how to do this...

(我已经知道该怎么做...)

import css from '!!css-loader!sass-loader!../css/styles.scss';

This puts it into an object which has the CSS as a string inside

(这将其放入一个对象,该对象内部具有CSS作为字符串)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...