简体   繁体   English

react-bootstrap + purgeCss + next.js

[英]react-bootstrap + purgeCss + next.js

PurgeCss removes react-bootstrap css classes used in my project. PurgeCss 删除了我的项目中使用的 react-bootstrap css 类。 I am using Next.js framework.我正在使用 Next.js 框架。

_app.js: _app.js:

import '../styles/style.scss';
import React from 'react';
import PropTypes from 'prop-types';
import 'bootstrap/dist/css/bootstrap.min.css';

// This default export is required in a new `pages/_app.js` file.
export default function MyApp({Component, pageProps}) {
  return <Component {...pageProps} />;
}

MyApp.propTypes = {
  Component: PropTypes.node,
  pageProps: PropTypes.node,
};

component.js:组件.js:

import React from 'react';
import {Container, Row, Col} from 'react-bootstrap';

const MyTest = function () {
  return (
    <Container>
      <Row>                   // .row class is missing in the final CSS!
        <Col>1 of 2</Col>
        <Col>2 of 2</Col>
      </Row>
      <Row>
        <Col>1 of 3</Col>
        <Col>2 of 3</Col>
        <Col>3 of 3</Col>
      </Row>
    </Container>
  );
};

export default MyTest;

next.config.js: next.config.js:

// next.config.js
const withSass = require('@zeit/next-sass');
const withCss = require('@zeit/next-css');
const withPurgeCss = require('next-purgecss');

module.exports = withCss(
  withSass(
    withPurgeCss({
      generateBuildId: async () => {
        // You can, for example, get the latest git commit hash here
        return 'my-build-id3';
      },
      distDir: 'build',
      purgeCssPaths: [
        './src/pages/*.js',
        './src/pages/**/*.js',
        './src/components/*.js',
        './src/components/**/*.js',
        './src/layouts/*.js',
        './src/layouts/**/*.js',
      ],
    })
  )
);

When I build the app, .row classes and .col classes are completely missing in the CSS file.当我构建应用程序时,CSS 文件中完全缺少.row类和.col类。 When disabling PurgeCSS everything works fine.禁用 PurgeCSS 时一切正常。

UPDATE 1#:更新 1#:

Purging of other classes works fine.清除其他类工作正常。

Defining Bootstrap classes inside <div> work fine.:<div>定义 Bootstrap 类工作正常。:

 <button className="btn btn-primary">     // this works fine!
          My Button
 </button>

Other React-bootstrap components dont work either.其他 React-bootstrap 组件也不起作用。 This doesnt work!这不起作用!

import React from 'react';
import {Button} from 'react-bootstrap';

const MyTest = function () {
  return <Button variant="outline-warning">Primary button</Button>;    // doesnt work
};

export default MyTest;

您应该在配置文件中将引导 CSS 文件的路径包含到您的 purgeCSSpath 中。

'./node_modules/react-bootstrap/**/*.js',

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

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