question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to bundle `stylelint` using bundlers

See original GitHub issue

What 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

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 using importLazy(() => 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:closed
  • Created a year ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ybiquitouscommented, Nov 2, 2022

@phoenisx Thank you for providing the reproduction with a detailed explanation! Converting the code to the following bundler-friendly pattern seems to make sense. 👍🏼

-	'alpha-value-notation': importLazy('./alpha-value-notation'),
+	'alpha-value-notation': importLazy(() => require('./alpha-value-notation'))(),

In addition, to avoid type errors, we need to apply the patch to the import-lazy type definition like this:

 declare function importLazy<T = unknown>(
 	importFn: (moduleId: string) => T
-): (moduleId: string) => T;
+): (moduleId?: string) => T;

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.

1reaction
phoenisxcommented, Nov 3, 2022

I have raised the PR. Do let me know if anything else is needed to resolve this issue 💯

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found