-
Notifications
You must be signed in to change notification settings - Fork 355
Disabling Style Injection with Webpack
This is a quick guide to opting-out of the automatic stylesheet injection that happens as a side effect of importing @patternfly/react-core
components in your UI.
Note: null-loader has been deprecated, with the suggestion to instead set resolve.alias.package
to false to inform webpack to ignore a package.
It's actually quite simple. What you need to do is install/configure null-loader
in your webpack config and then manually import the individual patternfly stylesheets you want. Let's do it step by step.
- Install null-loader
yarn add null-loader --dev
- Configure null-loader to discard only patternfly react styles by adding the following module rule to webpack.common.js
{
test: /\.css$/,
include: stylesheet => stylesheet.includes('@patternfly/react-styles/css/'),
use: ["null-loader"]
}
At this point, your pf react components will look "broken" because we've disabled all of those styles, we need to add them back manually.
- Install patternfly "core", the CSS offering.
yarn add @patternfly/patternfly
- Import the individual stylesheets for all the components, utilities, and layouts you may be using.
import '@patternfly/react-core/dist/styles/base.css';
import '@patternfly/patternfly/patternfly-addons.css';
import '@patternfly/patternfly/components/Alert/alert.css';
import '@patternfly/patternfly/components/Page/page.css';
import '@patternfly/patternfly/components/Backdrop/backdrop.css';
import '@patternfly/patternfly/layouts/Bullseye/bullseye.css';
import '@patternfly/patternfly/layouts/Grid/grid.css';
import '@patternfly/patternfly/components/Button/button.css';
import '@patternfly/patternfly/components/ClipboardCopy/clipboard-copy.css';
import '@patternfly/patternfly/components/Toolbar/toolbar.css';
import '@patternfly/patternfly/components/DataList/data-list.css';
// etc.
With all of this in place, the development experience is improved as you'll only see stylesheets that you've explicitly imported into your app, and the source order of those stylesheets is much less likely to change as a result of unrelated refactorings to your JS module architecture.