Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[react app] Imported CSS files in JS are not extracted to the index.css bundled file #2905

Closed
KKoukiou opened this issue Feb 9, 2023 · 0 comments

Comments

@KKoukiou
Copy link

KKoukiou commented Feb 9, 2023

Hi @evanw and thanks for the project!
I am currently trying to port cockpit project from webpack to esbuild and I have hit some issue with imported css files not getting extracted in the bundled index.css.

Here is a minimal reproducer:

Install npm dependencies for the reproducer:

[koukiou@sequioa test-ebuild-patternfly]$ npm install --save-dev @patternfly/react-core react react-dom esbuild

[kkoukiou@sequioa test-ebuild-patternfly]$ cat package.json 
{
  "devDependencies": {
    "@patternfly/react-core": "^4.276.6",
    "esbuild": "^0.17.6",
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  }
}

Create a test.jsx file as:

[kkoukiou@sequioa test-ebuild-patternfly]$ cat test.jsx 
import * as React from 'react'
import * as Server from 'react-dom/server'
import  { Button } from '@patternfly/react-core';

let Greet = () => <Button>Test</Button>;

console.log(Server.renderToString(<Greet />));

And finally bundle it:

[kkoukiou@sequioa test-ebuild-patternfly]$ ./node_modules/.bin/esbuild test.jsx --bundle --outdir=dist --external:../../assets/images/*

  dist/test.js  563.8kb

⚡ Done in 191ms

The issue is that the bundled JS file contains imported CSS that the React component i use is importing in turn. But this does not take effect of course. The expected behavior would be for the CSS files to get extracted in a seperate dist/test.css file

[kkoukiou@sequioa test-ebuild-patternfly]$ cat dist/test.js | grep -A3 'button.css'
  // node_modules/@patternfly/react-styles/css/components/Button/button.css
  var require_ = __commonJS({
    "node_modules/@patternfly/react-styles/css/components/Button/button.css"(exports, module) {
      module.exports = {};
    }
  });

A bit more detailed look: the Button react component that we import in our test.jsx, has inside another JS file import for the stylesheets.

[kkoukiou@sequioa test-ebuild-patternfly]cat node_modules/@patternfly/react-core/components/Button/Button.js | grep react-styles
import styles from '@patternfly/react-styles/css/components/Button/button';

And in turn, button.js imported the CSS file that we want to get extracted like this:

[kkoukiou@sequioa test-ebuild-patternfly]$ cat node_modules/@patternfly/react-styles/css/components/Button/button.js | head -3
"use strict";
exports.__esModule = true;
require('./button.css');

Any hints if this is not a bug are greatly appreciated.

KKoukiou added a commit to KKoukiou/cockpit-podman that referenced this issue Feb 9, 2023
TODO:
[ ] cockpit-po-plugin
[ ] fail on warnings eslint (Open eslint issue eslint/eslint#16804)
[ ] string replace loader does not work
[ ] css from Patternfly is not being extracted [1]

[1] evanw/esbuild#2905
@KKoukiou KKoukiou changed the title Imported CSS files in JS are not extracted to the index.css bundled file [react app] Imported CSS files in JS are not extracted to the index.css bundled file Feb 9, 2023
KKoukiou added a commit to KKoukiou/cockpit-podman that referenced this issue Feb 9, 2023
TODO:
[ ] cockpit-po-plugin
[ ] fail on warnings eslint [0]
[ ] string replace loader does not work
[ ] css from Patternfly is not being extracted [1]

[0] eslint/eslint#16804
[1] evanw/esbuild#2905
@evanw evanw closed this as completed in 88e17d8 Feb 9, 2023
KKoukiou added a commit to KKoukiou/cockpit-podman that referenced this issue Feb 12, 2023
TODO:
[ ] cockpit-po-plugin
[ ] fail on warnings eslint [0]
[ ] string replace loader needs to replace fake pths
[x] css from Patternfly is not being extracted [1]

[0] eslint/eslint#16804
[1] evanw/esbuild#2905
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant