jsondiffpatch icon indicating copy to clipboard operation
jsondiffpatch copied to clipboard

Refactor dynamic require statements

Open glf256 opened this issue 10 years ago • 7 comments

Anything along the lines of:

var dynamicRequire = ...;
var resultingModule = require(dynamicRequire);

Unless this is an artifact of the build process, it seems to me that it is unnecessary and breaks software like crcn/nexe.

glf256 avatar Sep 17 '15 21:09 glf256

yes, it is intentional for the build process, basically this makes browserify to not bundle that dependency, so it's used for any dependency that I don't want in the main browser bundle. there are other similar issues reported, but unfortunately, I think I haven't yet seen a nice solution that can make all bundling tools/module systems happy. I would be very happy if we can find it.

benjamine avatar Sep 17 '15 21:09 benjamine

Would explicitly excluding those modules in your browserify configuration work? https://github.com/substack/node-browserify#bexcludefile

glf256 avatar Sep 17 '15 22:09 glf256

it could be better, but one problem I see with that is, that won't work for people using this is as a dependency, browserify config is not inherited (I wish browserify supported inline comments for this stuff, like jshint for example)

benjamine avatar Sep 17 '15 22:09 benjamine

This also causes warnings like this in dependent libraries with Webpack.

screen shot 2016-02-23 at 13 48 48

One way to fix this would be to refactor env-specific modules to be functions accepting isClientSide as an argument. Then you would be able to use browser field in package.json (respected by all major bundlers) to “remap” these files to browser versions that inject true there.

You can see this technique in action here.

gaearon avatar Feb 23 '16 13:02 gaearon

thanks @gaearon I want to do some refactor on the bundling process, but yes I've been looking to have a mechandism that makes at least browserify and webpack happy, I'm just not familiar with webpack, but if package.json browser is something both respect (even when entering into dependencies) that might be the perfect solution.

benjamine avatar Feb 23 '16 20:02 benjamine

if package.json browser is something both respect (even when entering into dependencies) that might be the perfect solution.

I can confirm this is the case.

gaearon avatar Feb 23 '16 21:02 gaearon

awesome!, pasting this here for reference https://github.com/defunctzombie/package-browser-field-spec

benjamine avatar Feb 25 '16 06:02 benjamine

I believe this has been resolved.

Methuselah96 avatar Aug 26 '23 19:08 Methuselah96