Webpack inject js/css file content directly to index.html

安稳与你 提交于 2019-12-06 06:38:17

It actually can be accomplish with:

  1. npm i --save-dev css-loader to-string-loader
  2. configure your webpack with

    module: [
        rules: [
          {
            test: /\.css$/,
            use: ['to-string-loader', 'css-loader']
          }
        ]
      }
    
  3. Then in your project you can access the css as string like so

    const cssContent= require('path_to_css/some_css.css'); console.log('your css is: ', cssContent.toString());

    • Or maybe you want inject it manually to the page with:

      const boostrap= require('bootstrap/dist/css/bootstrap.min.css');
      let style= document.createElement('style');
      style.id= 'boostrap';
      style.setAttribute('type', 'text/css');
      style.innerHTML= boostrap.toString();
      document.body.appendChild(style);
      

Reference

  1. Inject CSS stylesheet
  2. css-loader
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!