Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Webpack workaround no longer working from 1.4.6 #1071

Closed
robmaas opened this issue Oct 6, 2023 · 14 comments
Closed

Webpack workaround no longer working from 1.4.6 #1071

robmaas opened this issue Oct 6, 2023 · 14 comments
Assignees
Labels
wontfix This will not be worked on

Comments

@robmaas
Copy link

robmaas commented Oct 6, 2023

The workaround for Webpack warnings described in the REAMDE.md no longer seems to be working for projects using html-react-parser version 1.4.6 and later.
(described in #213 )

We use the workaround in our webpack.config:

module.exports = {
  ...
  resolve: {
    ..
    mainFields: ['main', 'module'],
  }
}

When upgrading the html-react-parser package from 1.4.5 to 1.4.6 the same error comes up again:

export 'default' (imported as 'parse') was not found in 'html-react-parser' (module has no exports)

I guess it is caused by the index.mjs export change in 03bb4d9

This issue persist up to the latest version 4.2.2.

Expected Behavior

Build completes succesfully.

Actual Behavior

Webpack builds fails with an error.

Steps to Reproduce

Reproducible Demo

Our project is very complex, it is not very easy to create a repro. If that is necessary I'll see what I can do.

Environment

  • Version: 1.4.6
  • Platform: Node18, webpack 5.88.2
  • Browser: -
  • OS: Mac/Windows

Keywords

@robmaas robmaas added the bug Something isn't working label Oct 6, 2023
@remarkablemark
Copy link
Owner

Thanks for opening this issue @robmaas! Do you have a suggestion on what the fix would be?

@robmaas
Copy link
Author

robmaas commented Oct 6, 2023

@remarkablemark not really, I'm not very familiar with ES exports etc.
I'll try to take a look at it in the next couple of days, bit I'm a bit busy so might take a while.
Since nobody reported this earlier I don't think a lot of people encounter this issue so might not have the highest priority.

@remarkablemark
Copy link
Owner

@robmaas Thanks for the update! I'll also try to see if I can reproduce this locally in my spare time.

@MaxTwentythree
Copy link

I encountered a Probably similar issue:
"Cannot read properties of undefined (reading 'default')"
I had to downgrade from version 4.2.5 to 4.2.1 (npm) for it to work again

@robmaas
Copy link
Author

robmaas commented Oct 18, 2023

@MaxTwentythree That's probably an unrelated other issue. My original problem also occurs on 4.2.1 with the same error I described earlier. It's not the same error you're encountering.

@robmaas
Copy link
Author

robmaas commented Dec 1, 2023

@MaxTwentythree Sorry for the delayed response, been very busy lately.
Will try to take a look at it soon, or ask one of my colleague's to do so.

@remarkablemark
Copy link
Owner

All good @robmaas, thanks for the update

@MaxTwentythree
Copy link

@robmaas: same :)
Should I open a new issue with the problem I'm encountering?

@remarkablemark
Copy link
Owner

@MaxTwentythree can you provide a reproducible example?

@robmaas
Copy link
Author

robmaas commented Dec 28, 2023

Hi @remarkablemark, the issue is definitely related the exports config introduced in #434

When removing the exports config from package.json the warnings dissapear even in 5.0.11 (which has a more advanced exports config).
I'm not very familiar with webpack and module exports etc. so it's a bit hard for me to understand what is really going on here.

I think this is somewhat related, because we do use moduleResolution: node.

@remarkablemark
Copy link
Owner

@robmaas Thanks for the update. Unfortunately, I can't remove exports from package.json since it will break ESM support. I wonder if there's something that can be done on Webpack side.

@robmaas
Copy link
Author

robmaas commented Jan 5, 2024

@remarkablemark Obviously, just reporting that it is related.

Been struggling a bit to isolate the issue to a reproducible project, but will try again sometime soon (not really a big priority at the moment).

@robmaas
Copy link
Author

robmaas commented Jan 5, 2024

@remarkablemark Finally got it.
I was able to reproduce the issue in a small project by gradually adding configuration.

Turns out we have some webpack loader configuration with patterns that incorrectly match for *.mjs files.

For example:

{
  // anything in node_modules that isn't js,
  // we load as null - e.g. imported css from a module,
  // that is not needed for SSR
  test: /\.(?!js|html$)[^.]+$/,
  include: /node_modules/,
    use: {
      loader: 'null-loader'
    }
  }

This pattern allows *.js, *.jsx etc. but not *.mjs.
Obviously that strips the files completely, so errors start appearing.

Before #434 our solution used the exported index.js file, which was fine.
Bit of a stupid issue after all, but even in our complex solution never ran into this before.

Updating the regex to include *.mjs/*.cjs files seems to fix our issue.
Feel free to close this one. You can consider creating a FAQ for this, but it is not at all related to your package (apart from *.mjs files not being used that much out there).

Repro

Filename: package.json

{
  "name": "html-react-parser-repo-1071",
  "private": true,
  "version": "1.0.0",
  "type": "module",
  "dependencies": {
    "html-react-parser": "1.4.6",
    "null-loader": "4.0.1",
    "webpack": "5.76.1",
    "webpack-cli": "5.0.1"
  },
  "scripts": {
    "build": "webpack --config webpack.config.cjs",
    "start": "cd src && node index"
  }
}

Filename: webpack.config.cjs

module.exports = {
  mode: 'development',
  resolve: {
    mainFields: ['main', 'module']
  },
  module: {
    rules: [
      {
        test: /\.(?!js|html$)[^.]+$/,
        include: /node_modules/,
        use: {
          loader: 'null-loader',
        }
      }
    ]
  }
};

Filename: src/index.js

import parse from 'html-react-parser';

console.log('parsed: ' + parse('<p>test</p>'));

Output

ERROR in ./src/index.js 3:25-30
export 'default' (imported as 'parse') was not found in 'html-react-parser' (module has no exports)

@remarkablemark remarkablemark added wontfix This will not be worked on and removed bug Something isn't working labels Jan 5, 2024
@remarkablemark
Copy link
Owner

Amazing @robmaas, thanks for the reproduction! If you'd like, feel free to open a PR to improve the README.md. Otherwise, I can close this issue and convert it to a discussion.

Repository owner locked and limited conversation to collaborators Jan 10, 2024
@remarkablemark remarkablemark converted this issue into discussion #1262 Jan 10, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants