Skip to content
This repository has been archived by the owner on Feb 24, 2018. It is now read-only.

Bundling with Aurelia CLI #418

Closed
dragoscirjan opened this issue May 31, 2017 · 1 comment
Closed

Bundling with Aurelia CLI #418

dragoscirjan opened this issue May 31, 2017 · 1 comment

Comments

@dragoscirjan
Copy link

dragoscirjan commented May 31, 2017

I'm trying to bundle cognito using Aurelia CLI and I think I've read everything there was to read on the internet about it... with no success in implementing a thorough solution.

Step 1

A logical solution for bundling would be

    ...
    "bundles": [
      ..
      {
        "name": "account-bundle.js",
        "dependencies": [
          ... awk-sdk dependencies (lodash and xmlbuilder") ...
          {
            "name": "aws-sdk",
            "path": "../node_modules/aws-sdk",
            "main": "lib/aws",
          },
          ... amazon-cognito-identity-js dependencies ...
          {
            "name": "amazon-cognito-identity-js",
            "path": "../node_modules/amazon-cognito-identity-js",
            "main": "lib/index",
          }
        ],
        "source": {
          "include": [
            "**/components/account/**/*.js",
            "**/components/account/**/*.{html,css}",
            "**/templates/account/**/*.js",
            "**/templates/account/**/*.{html,css}"
          ]
        }
      },
      ...
    ]
    ...

But at this step I'm stuck with two interesting errors:

1st of all, I have lodash embedded already in another bundle, if I embed it in this file as well, it will throw an error.

2nd of all, the most annoying errors of all which will follow us throughout all my issue

File not found or not accessible: /work/dragosc/Workspace/Localized/desilacol-client/node_modules/aws-sdk/lib/region_config_data.json.js. Requested by /work/dragosc/Workspace/Localized/desilacol-client/node_modules/aws-sdk/lib/aws.js
{ Error: ENOENT: no such file or directory, open '/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aws-sdk/lib/region_config_data.json.js'
    at Object.fs.openSync (fs.js:584:18)
    at Object.fs.readFileSync (fs.js:491:33)
    at Object.exports.readFileSync (/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aurelia-cli/lib/file-system.js:74:13)
    at fileRead (/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aurelia-cli/lib/build/bundled-source.js:101:27)
    at Object.context.fileRead (/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aurelia-cli/lib/build/amodro-trace/lib/loader/Loader.js:179:18)
    at Object.context.load (/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aurelia-cli/lib/build/amodro-trace/lib/loader/Loader.js:370:32)
    at Module.load (eval at <anonymous> (/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aurelia-cli/lib/build/amodro-trace/lib/loader/Loader.js:14:1), <anonymous>:832:29)
    at Module.fetch (eval at <anonymous> (/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aurelia-cli/lib/build/amodro-trace/lib/loader/Loader.js:14:1), <anonymous>:822:66)
    at Module.check (eval at <anonymous> (/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aurelia-cli/lib/build/amodro-trace/lib/loader/Loader.js:14:1), <anonymous>:854:30)
    at Module.enable (eval at <anonymous> (/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aurelia-cli/lib/build/amodro-trace/lib/loader/Loader.js:14:1), <anonymous>:1173:22)
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/work/dragosc/Workspace/Localized/desilacol-client/node_modules/aws-sdk/lib/region_config_data.json.js' }

First error, the one with lodash I have fixed (even though can't really agree it's a best practice) by moving the bundle setup to my main vendor bundle where lodash was initially loaded.

Second error, not.

Step 2

I have tried loading the dist file instead

          {
            "name": "aws-sdk",
            "path": "../node_modules/aws-sdk",
            "main": "dist/aws-sdk",
            "exports": "AWS"
          }

However at this point I am pointed by aurelia-cli that global.js file is not available. So no good practice here either. Package is not friendly at all.

Step 3

Read and read and read and decided to give it a try to this article Cognito staff has referenced in their documentation, so my latest setup...

      {
        "name": "vendor-bundle.js",
        "prepend": [
          "node_modules/aws-sdk/dist/aws-sdk.min.js",
          "node_modules/amazon-cognito-identity-js/dist/aws-cognito-sdk.min.js",
          "node_modules/amazon-cognito-identity-js/dist/amazon-cognito-identity.min.js"
        ],
        "dependencies": [
          ... my initial setup ...
        ]
      }

In comparsion to the mentioned article, I have bluebird already embedded in aurelia-bundle.js

Well... now I get no errors in compiling, but when I refresh the page... guess what:

GET https://white.dev/src/aws-sdk/global.js 404 (Not Found)
req.load @ aurelia-bundle.js:6210
load @ aurelia-bundle.js:5936
load @ aurelia-bundle.js:5085
fetch @ aurelia-bundle.js:5075
check @ aurelia-bundle.js:5107
enable @ aurelia-bundle.js:5427
enable @ aurelia-bundle.js:5808
(anonymous) @ aurelia-bundle.js:5412
(anonymous) @ aurelia-bundle.js:4385
each @ aurelia-bundle.js:4310
enable @ aurelia-bundle.js:5364
init @ aurelia-bundle.js:5039
callGetModule @ aurelia-bundle.js:5454
completeLoad @ aurelia-bundle.js:5841
onScriptLoad @ aurelia-bundle.js:5968
aurelia-bundle.js:3878 Error: Script error for "aws-sdk/global", needed by: i18next-xhr-backend/i18nextXHRBackend.min
http://requirejs.org/docs/errors.html#scripterror
    at makeError (https://white.dev/dist/aurelia-bundle.js:4419:17)
    at HTMLScriptElement.onScriptError (https://white.dev/dist/aurelia-bundle.js:5989:36)
From previous event:
    at DefaultLoader.loadModule (https://white.dev/dist/aurelia-bundle.js:14914:14)
    at config (https://white.dev/dist/aurelia-bundle.js:12460:29)
    at https://white.dev/dist/aurelia-bundle.js:12496:14
From previous event:
    at bootstrap (https://white.dev/dist/aurelia-bundle.js:12495:26)
    at https://white.dev/dist/aurelia-bundle.js:12482:9
    at <anonymous>
From previous event:
    at run (https://white.dev/dist/aurelia-bundle.js:12477:61)
    at Object.<anonymous> (https://white.dev/dist/aurelia-bundle.js:12502:37)
    at Object.execCb (https://white.dev/dist/aurelia-bundle.js:5947:33)
    at Module.check (https://white.dev/dist/aurelia-bundle.js:5134:51)
    at Module.enable (https://white.dev/dist/aurelia-bundle.js:5427:22)
    at Object.enable (https://white.dev/dist/aurelia-bundle.js:5808:39)
    at Module.<anonymous> (https://white.dev/dist/aurelia-bundle.js:5412:33)
    at https://white.dev/dist/aurelia-bundle.js:4385:23
    at each (https://white.dev/dist/aurelia-bundle.js:4310:31)
    at Module.enable (https://white.dev/dist/aurelia-bundle.js:5364:17)
    at Module.init (https://white.dev/dist/aurelia-bundle.js:5039:26)
    at https://white.dev/dist/aurelia-bundle.js:5711:36
tryCatcher @ aurelia-bundle.js:3878
Promise._settlePromiseFromHandler @ aurelia-bundle.js:3094
Promise._settlePromise @ aurelia-bundle.js:3151
Promise._settlePromise0 @ aurelia-bundle.js:3196
Promise._settlePromises @ aurelia-bundle.js:3271
(anonymous) @ aurelia-bundle.js:620
aurelia-bundle.js:6210 GET https://white.dev/src/aws-sdk/clients/cognitoidentityserviceprovider.js 

Step 4 I try again...

        "prepend": [
          "node_modules/aws-sdk/global.js",
          "node_modules/aws-sdk/dist/aws-sdk.min.js",
          "node_modules/amazon-cognito-identity-js/dist/aws-cognito-sdk.min.js",
          "node_modules/amazon-cognito-identity-js/dist/amazon-cognito-identity.min.js"
        ],

Outcome... not far...

aurelia-bundle.js:4419 Uncaught Error: Module name "lib/node_loader" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
    at makeError (aurelia-bundle.js:4419)
    at Object.localRequire [as require] (aurelia-bundle.js:5687)
    at requirejs (aurelia-bundle.js:6048)
    at vendor-bundle.js:1

Step 5... Not giving up...

At this point I try a combination, hoping that by bundling aws-sdk dist file and appending the rest...

      {
        "name": "vendor-bundle.js",
        "append": [
          "node_modules/amazon-cognito-identity-js/dist/aws-cognito-sdk.min.js",
          "node_modules/amazon-cognito-identity-js/dist/amazon-cognito-identity.min.js"
        ],
        "dependencies": [
          ...
          {
            "name": "lodash",
            "path": "../node_modules/lodash",
            "main": "index"
          },
          {
            "name": "xmlbuilder",
            "path": "../node_modules/xmlbuilder",
            "main": "lib/index"
          },
          {
            "name": "aws-sdk",
            "path": "../node_modules/aws-sdk",
            "main": "dist/aws-sdk",
            "resources": [
              "lib/node_loader.js",
              "lib/region_config_data.json"
            ],
            "exports": "AWS"
          }
        ]
      }

Well... no...

File not found or not accessible: /work/dragosc/Workspace/Localized/desilacol-client/node_modules/aws-sdk/lib/region_config_data.json.js. Requested by /work/dragosc/Workspace/Localized/desilacol-client/node_modules/aws-sdk/lib/node_loader.js

The nemesis haunts me again...

Step 6 - Fudge it ... append them all

    ...
        "append": [
          "node_modules/aws-sdk/dist/aws-sdk.min.js",
          "node_modules/amazon-cognito-identity-js/dist/aws-cognito-sdk.min.js",
          "node_modules/amazon-cognito-identity-js/dist/amazon-cognito-identity.min.js"
        ],
    ....

HA! No bundle errors, no browser errors...

Until... Well... AWSCognito loads, AWSCognito.CognitoIdentityServiceProvide is present (type function)... however all the objects within that I'm aiming for... not present.

So my question, after this entire epic story... remains:

How can I bundle cognito api with Aurelia CLI?" Has anyone actually tried and managed to do it?

@dragoscirjan
Copy link
Author

dragoscirjan commented Jun 8, 2017

Funny to give the answer myself, but hey... if it will help others...

I managed to bundle the scripts by prepending them in the aurelia-bundle.js ahead of every other scripts that need to be loaded. Please understand, that (for me), placing the scripts in any other position that the following, will end up in neither loading correctly or not loading at all.

Don't pretend this is a general solution, but hey... works for me.

      {
        "name": "aurelia-bundle.js",
        "prepend": [
          "node_modules/aws-sdk/dist/aws-sdk.min.js",
          "node_modules/amazon-cognito-identity-js/dist/aws-cognito-sdk.min.js",
          "node_modules/amazon-cognito-identity-js/dist/amazon-cognito-identity.min.js",
          "node_modules/babel-polyfill/dist/polyfill.min.js",
          "node_modules/whatwg-fetch/fetch.js",
          "node_modules/bluebird/js/browser/bluebird.core.js",
          "node_modules/aurelia-cli/lib/resources/scripts/configure-bluebird.js",
          "node_modules/requirejs/require.js"
        ],
        "dependencies": [
          "aurelia-animator-css",
          "aurelia-binding",
          "aurelia-bootstrapper",
          "aurelia-dependency-injection",
          "aurelia-event-aggregator",

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant