0

I have a library which is to run in the browser as well as on the server. This library is dependent on Needle which makes XMLHttpRequests requests if used in the browser. This library is to use the bundled browserified library on the front-end and uses the regular ES6 code in Node.

So there are two sets of code here, dist/vendor.min.js for the browser to use, and lib/index.js for node to use. In the entry point for my library

index.js

'use strict';

// In browsers, load the bundled version
module.exports = process.browser ? require('./dist/vendor.min.js') : require('./lib/index.js}');

package.json

{
  "main": "index.js"
}

This solution works fine until I decide to uglify the front-end code. The application that is using this library is uglifying all of the 3rd party libraries in the production environment and whats going on here is uglify is also trying to uglify the node code which is non transpiled ES6 javascript.

Is there a better way to conditionally load these libraries?

1

1 Answer 1

1

Seems there is a more elegant way of doing this.

package.json

{
  "main": "index.js",
  "browser": {
    "index.js": "./dist/vendor.min.js"
  },
}

Not sure what effect this would have on any nested files with the name index.js but in this case I have none. But yes, it seems we can 'proxyquire' require('index.js') when in a browser environment with package.json. Neat. Thanks internets

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.