JavaScript Share common dependencies between contexts do containers share common dependencies

Premise

Let's say I have two different AMD-based AngularJS apps, each of them with their own sets of controllers, directives, services, etc. Each of them are bundled in their own dist/{app-name}.min.js and loaded in <script> tags in the same HTML page (this is all in the context of a CMS that then contains these apps among other things)

Now the apps end up sharing some of the services, directives, and vendor libraries like angular itself, moment, jQuery, etc, so I've made another folder for all of these resources, which results in a bundle that will be added to the page before the js bundles of the apps:

<script src="/some-path/dist/shared-resources.min.js"></script>
<script src="/some-path/dist/first-app.min.js"></script>
<script src="/some-path/dist/second-app.min.js"></script>

This is the resulting folder structure:

.
├── shared-resources/
│    ├── dist/
│    ├── src/
│    │     └── common/
│    │          ├── directives/
│    │          ├── modules/
│    │          ├── services/
│    │          └── vendor/
│    └── build.js
│
├── first-app
│    ├── dist/
│    ├── src/
│    │    ├── first-app/
│    │    │    ├── controllers/
│    │    │    ├── modules/
│    │    │    ├── services/
│    │    │    ├── directives/
│    │    │    └── app.js
│    │    └── first-app.js
│    └── build.js
│
└── second-app
     ├── dist/
     ├── src/
     │    ├── second-app/
     │    │    ├── controllers/
     │    │    ├── modules/
     │    │    ├── services/
     │    │    ├── vendor/
     │    │    └── app.js
     │    └── second-app.js
     └── build.js

This is an example of what the build.js file for the common modules looks like

({
    baseUrl: 'src',
    removeCombined: true,
    out: 'dist/shared-resources.min.js',
    paths: { // forcing a `common/{modulename}` convention
        'common/jquery': 'common/vendor/jquery.min',
        'common/moment': 'common/vendor/moment.min',
        'common/angular': 'common/vendor/angular/angular.min',
    },
    shim: {
        'common/angular': {
            exports: 'angular',
        }
    },
    include: [
        'common/modules/vendors', // Just a bundle of every vendor modules
        'common/directives/common-directive',
        'common/services/common-service'
    ],
})

Now my intention was to have all the shared modules being namespaced with common/, so each of the apps could require common/angular, common/directives/common-directive, and so on, and then exclude the common path when creating their bundle (since all the common modules are already present in the shared-resources.js bundle), for example:

// first-app/src/first-app/controllers/app-controller.js
define([
    'first-app/modules/controllers',
    'first-app/services/app-service',
    'common/services/common-service'
], function (controllers) {
    'use strict';

    controllers.controller('AppController', ['CommonService', 'AppService', function (CommonService, AppService) {
        CommonService.call();
        AppService.call();
    }]);
});

// first-app/build.js
({
    baseUrl: 'src',
    out: 'dist/first-app.min.js',
    paths: {
        'common': 'empty:'
    },
    name: 'first-app',
    deps: ['first-app/app']
})

Problem

The problem is how these two apps, which again are both loaded on the page (this can't be avoided), are supposed to correctly look up these common modules.

Given that each of the apps have obviously a different baseUrl, they are put in different RequireJS contexts, otherwise the baseUrl of the second app would override the baseUrl of the first one, causing the incorrect loading of its modules:

// first-app/src/first-app.js
require.config({
    context: 'first-app',
    baseUrl: 'first-app/src',
})(['fist-app/app']);

// first-app/src/second-app.js
require.config({
    context: 'second-app',
    baseUrl: 'second-app/src',
})(['second-app/app']);

But putting them in context then causes the look up for the common modules to fail, as the modules are looked in the baseUrl of the context. Actually this happens only for the second app (second in order of loading), while the first app to be included in the page can load the common modules fine

Question

So how should I make the apps to correctly share the common modules? Am I approaching this wrong? Should I use something else than RequireJS?

Answer:1

So, I have a page, where when a user holds left mouse button, Shrek images are drawn where his/her cursor is. The problem is, only one picture is created when the mouse is down, but I need an ENDLESS ...

So, I have a page, where when a user holds left mouse button, Shrek images are drawn where his/her cursor is. The problem is, only one picture is created when the mouse is down, but I need an ENDLESS ...

I have a string json object that I am trying to format and display in the html. I have tried using JSON.parse() and JSON.stringify() but the typeof is still showing as a string and it is displaying in ...

I have a string json object that I am trying to format and display in the html. I have tried using JSON.parse() and JSON.stringify() but the typeof is still showing as a string and it is displaying in ...

  1. formatting json string
  2. formatting json string in java
  3. formatting json string c#
  4. formatting json string js
  5. python string formatting json
  6. javascript formatting json string

I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...

I'm using the Facebook API SDK for JavaScript to invite the authenticated user's friends to use the app. To invite the friends, I use the Invitable Friends API, like this: FB.ui({ method: '...

  1. facebook game invite
  2. facebook game invite friends
  3. facebook instant game invite
  4. facebook instant game invite friend

I created an AFK command for my discord bot but I am struggling to figure out how I would add "[AFK]" into the current users nickname. Below is what I have but this takes the users discord name ...

I created an AFK command for my discord bot but I am struggling to figure out how I would add "[AFK]" into the current users nickname. Below is what I have but this takes the users discord name ...

  1. discord force user into voice channel