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

[BUG] "Can't resolve..." error when import RuleTable component from rule-components #1206

Closed
gkarat opened this issue Jul 13, 2021 · 9 comments · Fixed by #1246
Closed

[BUG] "Can't resolve..." error when import RuleTable component from rule-components #1206

gkarat opened this issue Jul 13, 2021 · 9 comments · Fixed by #1246
Labels
bug Something isn't working

Comments

@gkarat
Copy link
Contributor

gkarat commented Jul 13, 2021

Describe the bug
When you follow the example of RuleTable component usage here https://clouddot.pages.redhat.com/frontend-components/dev/rule-components/index.html and import RuleTable to your app, you will run into the build error. The issue is found for the rule-components version = 3.2.1 (latest).

To Reproduce
Steps to reproduce the behavior:

  1. Import to any of your project modules via import RuleTable from '@redhat-cloud-services/rule-components/RuleTable';
  2. Build your project (npm run build / yarn build)
  3. You will get the error Module not found: Error: Can't resolve './index.scss' in '/home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable'. See below for the full error stack.

Expected behavior
There is no build error related to the rule-components library.

Full error stack

ERROR in ./node_modules/@redhat-cloud-services/rule-components/esm/RuleTable/RuleTable.js 40:0-48
Module not found: Error: Can't resolve './index.scss' in '/home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable'
resolve './index.scss' in '/home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable'
  using description file: /home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/package.json (relative path: ./esm/RuleTable)
    Field 'browser' doesn't contain a valid alias configuration
    using description file: /home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/package.json (relative path: ./esm/RuleTable/index.scss)
      no extension
        Field 'browser' doesn't contain a valid alias configuration
        /home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable/index.scss doesn't exist
      .ts
        Field 'browser' doesn't contain a valid alias configuration
        /home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable/index.scss.ts doesn't exist
      .tsx
        Field 'browser' doesn't contain a valid alias configuration
        /home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable/index.scss.tsx doesn't exist
      .mjs
        Field 'browser' doesn't contain a valid alias configuration
        /home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable/index.scss.mjs doesn't exist
      .js
        Field 'browser' doesn't contain a valid alias configuration
        /home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable/index.scss.js doesn't exist
      .scss
        Field 'browser' doesn't contain a valid alias configuration
        /home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable/index.scss.scss doesn't exist
      as directory
        /home/gkaratae/ccx/ocp-advisor/node_modules/@redhat-cloud-services/rule-components/esm/RuleTable/index.scss doesn't exist
 @ ./node_modules/@redhat-cloud-services/rule-components/esm/RuleTable/index.js 1:0-45 1:0-45 1:0-45
 @ ./src/PresentationalComponents/ClusterRecommendations/ClusterRecommendations.js 4:0-73 22:182-191
 @ ./src/PresentationalComponents/Clusters/ClusterDetails.js 13:0-86 44:38-60
 @ ./src/SmartComponents/Clusters/Clusters.js 6:9-8:59
 @ ./src/Routes.js 5:9-7:40
 @ ./src/App.js 4:0-34 25:158-164
 @ ./src/AppEntry.js 5:0-24 20:38-41
 @ container entry ./RootApp[0]
@gkarat gkarat added the bug Something isn't working label Jul 13, 2021
@gkarat
Copy link
Contributor Author

gkarat commented Jul 13, 2021

The bug is fairly easy to solve by using import RuleTable from '@redhat-cloud-services/rule-components/RuleTable/RuleTable'; instead. Do we need to update the docs instead?

@akofink
Copy link
Contributor

akofink commented Jul 14, 2021

@bastilian any thoughts on this one?

@rvsia
Copy link
Contributor

rvsia commented Aug 5, 2021

@gkarat This import works because ESM should use shared style files with CJS, however, our babel plugin for translating scss imports work only with ... imports. And the RuleTable component includes a scss re-export for some reason. So it is a bug in the component, not a documentation change. 😏 By using the import, you actually use a wrong module version of the component (CJS modules are not intended for modern browsers) and it could introduce some unexpected errors (as you mix CJS/ESM components in your app) and bigger bundle size (some components will be bundled in both CJS&ESM versions.)

I made a fix that adds export translator: #1228 However, I am not sure if we want it and if we should not just import the scss file instead of re-exporting, but I am not absolutely sure why this change https://github.com/RedHatInsights/frontend-components/pull/390/files was ever made and if really need it now. @karelhala cc @Hyperkid123

@Hyperkid123
Copy link
Contributor

Well the thing is, the build component should not even resolve the .scss files but it should import only the .css files. 🤔 maybe we forgot to add the custom loader for the rule-component package? Gimme a sec to debug it.

@Hyperkid123
Copy link
Contributor

@gkarat well I have found the issue. The RuleTable component is re-exporting the scss file as style. I am not but I am 100% sure it won't work with CSS modules which is the only thing I can imagine this would be useful.

This fixes the problem

diff --git a/packages/rule-components/src/RuleTable/RuleTable.js b/packages/rule-components/src/RuleTable/RuleTable.js
index 760e6e7f..003e24c0 100644
--- a/packages/rule-components/src/RuleTable/RuleTable.js
+++ b/packages/rule-components/src/RuleTable/RuleTable.js
@@ -11,7 +11,7 @@ import flatten from 'lodash/flatten';
 import debounce from 'lodash/debounce';
 import isEqual from 'lodash/isEqual';
 import { calculateMeta, calculateActiveFilters, createRows } from './helpers';
-export { default as style } from './index.scss';
+import './index.scss';
 
 class RuleTable extends Component {
     state = {

Before I apply it @akofink @gkarat is there any reason why the styles are re-exported?

@rvsia
Copy link
Contributor

rvsia commented Aug 5, 2021

@Hyperkid123
Copy link
Contributor

Hyperkid123 commented Aug 5, 2021

Yeah, I saw it but it won't really help with the problem. The rule component is using CSS modules syntax but the component nor the library is set up to use/build CSS modules. If you want to consume the scss, you can import it into the project and go from there. SCSS files are available.

@gkarat
Copy link
Contributor Author

gkarat commented Aug 10, 2021

@Hyperkid123, @rvsia, thanks a lot for the clarification. I cannot comment anyhow on the necessity of re-export in this case.

@Hyperkid123
Copy link
Contributor

I believe it is not necessary. I will fix the import so we should not need the workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants