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

fix(@size-limit/webpack-css v10.0.1 ): require is not defined in ES module scope when running size-limit #340

Merged
merged 1 commit into from
Nov 1, 2023

Conversation

mendrew
Copy link
Contributor

@mendrew mendrew commented Oct 31, 2023

Good day! Thank you very much for this amazing tool!

Problem

In our project we are using size-limit with @size-limit/webpack-css plugin and with latest version v10.0.1 we are having an issue and see this error running size-limit.

ReferenceError: require is not defined in ES module scope, you can use import instead\nThis file is being treated as an ES module because it has a '.js' file extension
The whole error
{
  "error": "ReferenceError: require is not defined in ES module scope, you can use import instead\nThis file is being treated as an ES module because it has a '.js' file extension and '/Users/andrey.medvedev/src/experiment
/size-limit-experiment/node_modules/@size-limit/webpack-css/package.json' contains \"type\": \"module\". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.\n    at file:///Users/andrey.medvedev/s
rc/experiment/size-limit-experiment/node_modules/@size-limit/webpack-css/index.js:6:9\n    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)\n    at async Promise.all (index 0)\n    at async ESMLoader.import (
node:internal/modules/esm/loader:528:24)\n    at async Promise.all (index 2)\n    at async loadPlugins (file:///Users/andrey.medvedev/src/experiment/size-limit-experiment/node_modules/size-limit/load-plugins.js:24:14)\n
 at async findPlugins (file:///Users/andrey.medvedev/src/experiment/size-limit-experiment/node_modules/size-limit/run.js:28:17)\n    at async default (file:///Users/andrey.medvedev/src/experiment/size-limit-experiment/node_modules/size-limit/run.js:57:19)"
}

Version

size-limit: v10.0.1
node: v18.8.0.

To reproduce

Here is a repo example with reproduction: https://github.com/mendrew/stunning-journey

Proposal

As a solution we use createRequire to require commonjs from esm module like we do here in size-limit in other modules. Works well in my case. Hope it close to a solution.

Changes

Notes

Unfortunately I was not able to reproduce this issue in tests. Any ideas where I can put a test which would be similar to the example with reproduction?
load-plugins.test.js doesn't catch this issue.

When we are using `size-limit` with` @size-limit/webpack-css` plugin `v10.0.1` we are having an issue and see this error running `size-limit` during plugin loading
```
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'src/experiment
/size-limit-experiment/node_modules/@size-limit/webpack-css/package.json' contains \"type\": \"module\".
To treat it as a CommonJS script, rename it to use the '.cjs' file extension.\n
```

As a solution we use createRequire to require commonjs from esm module.

---

Also,
- adding webpack-css plugin as a workspace to global workspace package.json.
- checking that load-plugins() is not throwing an error when we load
  webpack-css
  (https://github.com/mendrew/size-limit/blob/f9e9a78c3f62be7061bfd25efa7e546f02de549c/packages/size-limit/test/load-plugins.test.js#L8-L16)
@ai
Copy link
Owner

ai commented Oct 31, 2023

Thanks. I will try to release it today.

@ai ai merged commit 7caea08 into ai:main Nov 1, 2023
2 checks passed
@ai
Copy link
Owner

ai commented Nov 1, 2023

Released in 10.0.2

@mendrew mendrew deleted the mendrew/fix-webpack-css-esm-plugin-version branch November 1, 2023 15:37
@mendrew
Copy link
Contributor Author

mendrew commented Nov 1, 2023

Great, thank you very much Andrey!

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

Successfully merging this pull request may close these issues.

None yet

2 participants