简体   繁体   English

使用webpack包含引导程序

[英]Include bootstrap using webpack

I'm developping a chrome extension. 我正在开发chrome扩展程序。 I use boostrap 3 for the UI. 我将boostrap 3用于UI。

doctype html

html
  head
    meta(charset='UTF-8')
    title (Boilerplate Popup)
    link(rel="stylesheet", href="https://maxcdn.bootstrapcdn.com/bootstrap/latest/css/bootstrap.min.css")
    style.
      body { width: 500px; }

  body
    #root
    script(src=env == 'prod' ? '/js/ext.bundle.js' : 'http://localhost:3000/js/ext.bundle.js')

Here is how I was used to include boostrap. 这就是我用来包括boostrap的方式。
localhost:3000 is a server created with webpack web server. localhost:3000是使用webpack Web服务器创建的服务器。

At this point everything works well and here is a screenshot : 现在一切正常,这是屏幕截图:

在此处输入图片说明

But I don't want my chrome extension to be network dependant, so I decided to download boostrap using : 但是我不希望自己的chrome扩展依赖于网络,因此我决定使用以下命令下载boostrap:

npm install boostrap which have downloaded boostrap 3. I also decided to use webpack to load boostrap.min.css. npm install boostrap已经下载了boostrap3。我还决定使用webpack加载boostrap.min.css。

  module: {
    loaders: [{
      test: /\.js$/,
      loader: 'babel',
      exclude: /node_modules/,
      query: {
        presets: ['react-hmre']
      }
    }, {
      test: /\.css$/,
      loaders: [
        'style',
        'css?modules&sourceMap&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]',
        'postcss'
      ]
    },
    {test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff'},
    {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
    {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
    {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'}]
  }

I didn't change the css loader (which comes from a boilerplate) but I added the loader for the web font and svg. 我没有更改css加载器(来自样板),但添加了Web字体和svg的加载器。

Finally, I've included boostrap.min.css in the javascript entry point : import 'bootstrap/dist/css/bootstrap.min.css'; 最后,我在javascript入口点中包含了boostrap.min.css:import'bootstrap import 'bootstrap/dist/css/bootstrap.min.css';

Now, it's how my extension look like : 现在,这就是我的扩展名:

在此处输入图片说明

I think a part of boostrap is loaded (because the link in the second version looks like the same as the links in the first version. But obviously, the other components are not loaded. 我认为Boostrap的一部分已加载(因为第二个版本中的链接看起来与第一个版本中的链接相同。但是显然,其他组件未加载。

I also use react-boostrap. 我也使用react-boostrap。

Thanks 谢谢

My idea in the comments is right, css?module locally load boostrap. 我在评论中的想法是正确的,css?module本地加载boostrap。 I changed the import with : 我更改了导入:

import '!style!css!bootstrap/dist/css/bootstrap.min.css';

and it works 它有效

For WEBPACK 2 , (ie after all the necessary dependency installs) you can include bootstrap as follows:- 对于WEBPACK 2 ,(即在安装了所有必需的依赖项之后),您可以包括如下所示的引导程序:

  1. Add css loaders in webpack.config.js 在webpack.config.js中添加CSS加载程序

     { test: /\\.css$/, use: [ { loader: 'style-loader' }, { loader: 'css-loader', options: { importLoaders: 1 } } ] } 
  2. Then, import bootstrap in the entry point script (ie index.js or app.js, index.js in my project) 然后,在入口点脚本(即,我的项目中的index.js或app.js,index.js)中导入引导程序

     import '!style-loader!css-loader!bootstrap/dist/css/bootstrap.css'; 

Need details!!! 需要详细信息!!! More details here 在这里更多细节

npm install bootstrap ;

And : 和:

Adjust css loader as following : 调整CSS加载器,如下所示:

       {
            test: /\.css$/,
            loaders: ['style', 'css'],
        }

And, import bootstrap : 并且,导入bootstrap:

import Bootstrap from 'bootstrap/dist/css/bootstrap.css';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM