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.

Provide a way to write a library that supports different dependencies on Node and the Browser

See original GitHub issue

Feature request

Author of Sass here 👋! I’m talking with @markwhitfeld about trying to get Dart Sass’s JS output running in the browser, and we’re running into some hassles with Webpack in particular that I think a new feature could address.

What is the expected behavior?

I need to meet all the following requirements:

  1. I want to be able to distribute a single npm package with minimal duplication…
  2. that works on both Node.js and the web…
  3. with nothing more complicated than npm i sass and require('sass')
  4. and plays nicely with Webpack bundling on both platforms…
  5. without needing any special configuration…
  6. and uses some libraries that are only available on Node…
  7. while gracefully degrading when those libraries aren’t available on the web.

As a special bonus non-requirement, it would be nice if the JS file we generate can be used as-is on web pages without bundling.

What is motivation or use case for adding/changing the behavior?

As far as I can tell, there’s no way to meet all of the above requirements right now. If I didn’t have to worry about Webpack, I’d just write

const chokidar = typeof window === 'undefined' ? require('chokidar') : null;

and be on my merry way, but that won’t actually prevent webpack from bundling the dependency’s code when targeting a browser so it fails requirement 4.

How should this be implemented in your opinion?

I’m agnostic on the best way to make this work, as long as it satisfies the requirements outlined above. The simplest solution might be to recognize typeof window === 'undefined’ as a magic check that can be inlined and used for constant-folding. A more explicit solution might be to provide a __webpack_target_browser__ constant that explicitly indicates whether the platform is a browser.

Are you willing to work on this yourself?

No.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
alexander-akaitcommented, Sep 23, 2021
0reactions
webpack-botcommented, Jan 12, 2022

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recipes on how to create a library that supports both browser ...
Introduces how to build a library that supports both browser and Node.js using webpack4 and ES6, and how to use the created library....
Read more >
Creating a simple npm library to use in and out of the browser
In this guide, I'm going to create a react app that loads random images ... axios to make requests (it works in the...
Read more >
How can I use the same library in Node AND a browser while ...
To make this work in the browser in all cases, use Browserify's --require feature to alias all dependencies to their npm expected names,...
Read more >
How to write and build JS libraries in 2018 - Medium
Extended guide about building JavaScript libraries and how to stay in ... Situation changes when we use ES6 modules that are still not...
Read more >
How To Use Node.js Modules with npm and package.json
First, you will create a package.json file to store useful metadata about the project and help you manage the project's dependent Node.js ...
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