Unable to bundle `stylelint` using bundlers
See original GitHub issueWhat steps are needed to reproduce the bug?
Use rollup
to bundle this library. It fails
What Stylelint configuration is needed to reproduce the bug?
None
How did you run Stylelint?
I am trying to bundle stylelint in my project
Which version of Stylelint are you using?
14.14.0
What did you expect to happen?
Stylelint should get bundled, without any errors.
What actually happened?
Getting Could not dynamically require "./color-no-invalid-hex"
when using color-no-invalid-hex
in the project.
Does the bug relate to non-standard syntax?
None
Proposal to fix the bug
- PR that introduced dynamic requires in the
stylelint
: https://github.com/stylelint/stylelint/pull/4729 - PR that fixed typings due to dynamic requires: https://github.com/stylelint/stylelint/pull/5878
Since the latter does not use the import-lazy
pattern for bundlers, bundlers are not able to resolve the require paths and fails during runtime.
Possible Resolution:
- We can keep the
import-lazy
pattern for bundlers intact, i.e. to revert back to usingimportLazy(() => require('./color-no-invalid-hex'))()
pattern - To fix usages of // @ts-nocheck, we can do something like following instead:
/** @type {(fn: () => any) => (() => any)} */
// @ts-ignore
const importLazy = require('import-lazy');
...
'color-no-invalid-hex': importLazy(() => require('./color-no-invalid-hex'))(),
I am not sure how strict this project is in terms of using @ts-ignore
comments, but I feel this is a decent trade-off to support proper bundling, unless someone has a better solution to overcome typescript type errors in rules
module.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (5 by maintainers)
Top Results From Across the Web
importing css modules results in Error: "Bundles must ... - GitHub
Appears to be generating two bundles named content.css . This is because there is a content.css file referenced from manifest.json (marked as ...
Read more >The correct way to bundle CSS modules - Stack Overflow
Fixing this issue was by doing import styles from 'path name' and then installing jest-css-modules to map the styles object in my test....
Read more >Migrating to 14.0.0 - Stylelint
If you use Stylelint to lint anything other than CSS files, you will need to install and configure these syntaxes. We recommend extending...
Read more >Introducing esbuild To Your Monorepo - DEV Community
In this post I will introduce esbuild as the bundling solution for my monorepo. I will bundle 2 packages with the same bundling ......
Read more >Creating a React Component Library using Rollup, Typescript ...
Use React; Use Sass; Use TypeScript (and bundle/export TypeScript types) ... My decision for a JavaScript module bundler to use was between ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@phoenisx Thank you for providing the reproduction with a detailed explanation! Converting the code to the following bundler-friendly pattern seems to make sense. 👍🏼
In addition, to avoid type errors, we need to apply the patch to the
import-lazy
type definition like this:Let’s create and commit a patch by using the
patch-package
package.I’ve labeled the issue as ready to implement. Please consider contributing if you have time.
There are steps on how to fix a bug in a rule in the Developer guide.
I have raised the PR. Do let me know if anything else is needed to resolve this issue 💯