You need to browse to download dependencies from the global scope, and not expect them to be merged together

I am developing library A and library B, B depending on A. I would like, using a browser, to combine them myself, so in my browser I could do:

var A = require("A"); var B = require("B"); 

I would like to link them myself, since I am also developing a C library that depends only on A and if A is included in B, then C will not be available, and if A is in B and C, I have duplicates.

So, I start with the Browning Library A:

 browserify -r ./src/A.js:A -o build/A.js 

Which works fine, I can distribute A, and other people can develop their applications with it.

Then I look at library B:

 browserify -r ./src/B.js:B -o build/B.js 

But I now have A twice, A loads independently in the browser and is again packaged with B. So I use the -i option from the browser to prevent it from turning on:

 browserify -r ./src/B.js -o build/B.js -i A 

But then, when B requires A, it gets an empty object {} instead of a library. Library A, although still accessible from a global area, requiring ("A").

I tried the external thing with -x, but then I can no longer require my library from the global scope.

I managed to get the behavior that I wanted by hacking the generated output of B, forcing the module permission to get A from the previous requirement, which makes me think that there may be a simple solution, but I can not find it.

I am using browser 2.18.1

+6
source share
1 answer

Two ways to look at it:

  • Think of B as jquery-ui and A as jquery. This allows users to include jQuery in the DOM if they want to use jquery-ui. Therefore, when building B does not require A.

  • Document the fact that B has A. built in to it. Users who use B simply should not include A at all. In this scenario - you obviously need to require A and put B together with A.

  • In the general case, if you have 2 browser packages, with common parts that are imported to the SAME page, you already have a problem with the organization / dependency of the package, which components are included in any, etc. Something in the basics of this needs to be reviewed / changed

+1
source

Source: https://habr.com/ru/post/948394/


All Articles