简体   繁体   English

如何在 create-react-app 中正确导入 bootstrap 4 sass

[英]How to properly import bootstrap 4 sass in create-react-app

Previosly I imported bootstrap in style.less like that(not using js framework):以前我在style.less导入了 bootstrap(不使用 js 框架):

@import "bootstrap/less/bootstrap.less";
@import "variables.less";
@import "mixins.less";
@import "main.less";
...

Now I use create-react-app and I use separate folder for each component with it's separate sass file(I was advised that it is one of the best ways).现在我使用create-react-app并为每个组件使用单独的文件夹,它有单独的 sass 文件(有人建议我这是最好的方法之一)。

├── components
│   ├── About
│   │   ├── index.jsx
│   │   ├── style.css
│   │   └── style.scss
│   ├── App
│   │   ├── App.test.js
│   │   ├── index.jsx
│   │   ├── style.css
│   │   └── style.scss
│   ├── Bread
│   │   ├── index.jsx
│   │   ├── style.css
│   │   └── style.scss
│   ├── Delivery
│   │   ├── index.jsx
│   │   ├── style.css
│   │   └── style.scss
│   ├── Footer
│   │   ├── index.jsx
│   │   ├── style.css
│   │   └── style.scss
│   ├── Header
│   │   ├── index.jsx
│   │   ├── style.css
│   │   └── style.scss
│   ├── index.js
├── _custom.scss
├── index.js

Create-react-app docs suggest to inlude this lines in scripts in package.json: Create-react-app 文档建议在 package.json 的脚本中包含以下行:

  "scripts": {
    "build-css": "node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/",
    "watch-css": "npm run build-css && node-sass-chokidar --include-path
     ...
  },

I import bootstrap in _custom.scss like that:我像这样在 _custom.scss 中导入引导程序:

@import "bootstrap/scss/bootstrap.scss";

I also have custom variables in that file.我在该文件中也有自定义变量。

The problem is I need to import custom.scss file in every component separately to use bootstrap or custom variables, then a built css file becomes huge.问题是我需要在每个组件中分别导入custom.scss文件以使用引导程序或自定义变量,然后构建的 css 文件变得很大。 I need a solution where I need to import custom.scss file once in App/style.scss .我需要一个解决方案,我需要在App/style.scss导入一次custom.scss文件。 Is it possible?有可能吗? How experienced developers import bootstrap有经验的开发人员如何导入引导程序


I also use reactstrap and don't import bootstrap.css, if it matters.如果重要的话,我也使用 reactstrap 并且不导入 bootstrap.css。

EDIT: I'm in a developer environment, I think that in production it should be ok, since everything is bundled into single css, I didn't checked it though.编辑:我在开发人员环境中,我认为在生产中应该没问题,因为所有内容都捆绑到单个 css 中,但我没有检查它。

EDIT: I can't check it because I can't build with undefined variables.编辑:我无法检查它,因为我无法使用未定义的变量进行构建。

If you use create-react-app, and define a global variable in some css file (in your case custom.scss), import it in your index.js.如果您使用 create-react-app,并在某个 css 文件(在您的情况下为 custom.scss)中定义一个全局变量,请将其导入您的 index.js。 After that, any component of yours has your variables, mixins etc available for use without you having to import custom.scss explicitly in the component's scss.之后,您的任何组件都可以使用您的变量、mixin 等,而无需在组件的 scss 中显式导入 custom.scss。

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

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