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

Compilation errors when building with webpack 5 #24

Closed
vanbasten17 opened this issue Dec 1, 2020 · 11 comments
Closed

Compilation errors when building with webpack 5 #24

vanbasten17 opened this issue Dec 1, 2020 · 11 comments
Labels

Comments

@vanbasten17
Copy link

vanbasten17 commented Dec 1, 2020

Hi @fernandopasik, thanks for your project, it's pretty useful. We are using your code in order to traverse React trees in runtime and do some stuff to adapt dynamically how a chat must be shown depending on a messaging provider (Facebook, Whatsapp, ...).

Expected behavior

It should work correctly with Webpack 5.9.0. Maybe transpiled lib should preserve the file extension in order to not break with webpack.

Current behavior

We are now trying to migrate our projects to Webpack 5 and we have experienced the following kind of errors:

ERROR in ./node_modules/react-children-utilities/react-children-utilities.js 10:0-38
Module not found: Error: Can't resolve './lib/onlyText' in '/.../.../.../.../botonic-examples/blank/node_modules/react-children-utilities'
Did you mean 'onlyText.js'?
BREAKING CHANGE: The request './lib/onlyText' failed to resolve only because it was resolved as fully specified
(probably because the origin is a '*.mjs' file or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

Steps to reproduce

  1. Init a project with Webpack 5.9.0.
  2. Configure babel (for example):
const babelLoaderConfig = {
  exclude: /node_modules[\/\\](?!(@botonic)[\/\\])/,
  use: {
    loader: 'babel-loader',
    options: {
      cacheDirectory: true,
      presets: ['@babel/preset-env', '@babel/react'],
      plugins: [
        '@babel/plugin-proposal-object-rest-spread',
        '@babel/plugin-proposal-class-properties',
        'babel-plugin-add-module-exports',
        '@babel/plugin-transform-runtime',
      ],
    },
  },
}
  1. Try to compile the project with webpack.

Device information

  • OS: MacOs
  • Version: Node v12.20.0

Possible solution

If not fully necessary, do not specify type module in package.json. Otherwise, publish lib with it's corresponding extensions.
Related issues (solutions proposed didn't work for me): graphql/graphql-js#2721

In fact, changing manually the following code inside node_modules fixed the issue:

import { Children, isValidElement } from 'react';
import hasChildren from './hasChildren';

to

import { Children, isValidElement } from 'react';
import hasChildren from './hasChildren.js';
@vanbasten17 vanbasten17 changed the title Compilation errors when building with webpack Compilation errors when building with webpack 5 Dec 1, 2020
@fernandopasik
Copy link
Owner

interesting, this brings something to my memory microsoft/TypeScript#16577

I think your suggestion hits the nail

@fernandopasik
Copy link
Owner

this one could be related too webpack/webpack#12093

@vanbasten17
Copy link
Author

vanbasten17 commented Dec 3, 2020

Have you tried to just run babel before publishing the lib with this babel plugin? I managed to have this working by just applying this rule, but I think that the code must be shipped in order to be used without having problems.
https://www.npmjs.com/package/babel-plugin-add-import-extension
Example:

const babelLoaderConfig = {
  test: /\.?js/,
  exclude: /node_modules[\/\\](?!(react-children-utilities)[\/\\])/,
  use: {
    loader: 'babel-loader',
    options: {
      cacheDirectory: true,
      presets: ['@babel/preset-env', '@babel/react'],
      plugins: ['babel-plugin-add-import-extension'],
    },
  },
}

@fernandopasik
Copy link
Owner

I actually would like to use the imports with js files as it is recommended in microsoft/TypeScript#16577 (comment)

I'm having a problem with eslint import plugin as it doesn't resolve the imports. I'm looking for a solution with maybe import/alias in eslint config

@vanbasten17
Copy link
Author

okay, thanks for the response @fernandopasik , let me know if you need a helping hand

@stale
Copy link

stale bot commented Dec 25, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Dec 25, 2020
@stale
Copy link

stale bot commented Jan 9, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale
Copy link

stale bot commented Jan 24, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Jan 24, 2021
@stale
Copy link

stale bot commented Feb 8, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Feb 8, 2021
@stale stale bot closed this as completed Feb 15, 2021
@codepunkt
Copy link

This is still a problem. What's the proposed fix?

@fernandopasik fernandopasik reopened this Feb 25, 2021
@stale stale bot removed the wontfix label Feb 25, 2021
@fernandopasik
Copy link
Owner

fernandopasik commented Feb 25, 2021

Sorry for the delayed solution for this. I've added a task to have the .js extensions in the built files. This should work fine with the latest release v2.4.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants