Home  >  Q&A  >  body text

javascript - Use raw-loader to read css files into strings, but the reading is unsuccessful

Because the css file needs to be imported, it is processed in the form of a string in js.

Use raw-loader to test and read txt, css, and js files. Only css reading failed. Please solve it. Only the background-color of the body is set in the css file.

import str from 'raw-loader!./style.css';
console.log('read css file-------------------------------------');
console.log(typeof str, str);

Configured in webpack.base.conf.js

曾经蜡笔没有小新曾经蜡笔没有小新2732 days ago658

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-05-19 10:13:59

    webpack configuration

    entry:{
            "case7":__dirname+'/index.js',
        },
        output:{
            path: __dirname+'/dist',
            filename:'[name].js',
        },
        module: {
            rules: [
                {
                    test: /\.css$/,
                    use: 'raw-loader'
                }
            ]
        }

    Import css files into index.js

    import txt from './file.css'
    console.log(txt);

    Run results

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-05-19 10:13:59

    Correct answer upstairs

    Just import is followed by the css file name, and webpack will automatically use raw-loader to process it according to the suffix name of the file

    reply
    0
  • Cancelreply