angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Angular-CLI not bundling external library properly

Open angheladrianclaudiu opened this issue 9 years ago • 6 comments

Please provide us with the following information:

OS?

Windows 10

Versions.

1.0.0-beta.28.3

Repro steps.

Just build the application and then serve it.

The log given by the failure.

It's not actually an error.

Mention any other details that might be useful.


I have the following package.json:

{
  "name": "website",
  "version": "1.0.0",
  "license": "Wrapbootstrap",
  "angular-cli": {},
  "scripts": {
    "start": "ng serve",
    "prod": "ng build -prod",
    "lint": "tslint \"src/**/*.ts\"",
    "test": "ng test",
    "pree2e": "webdriver-manager update",
    "e2e": "protractor"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^2.3.1",
    "@angular/compiler": "^2.3.1",
    "@angular/core": "^2.3.1",
    "@angular/forms": "^2.3.1",
    "@angular/http": "^2.3.1",
    "@angular/platform-browser": "^2.3.1",
    "@angular/platform-browser-dynamic": "^2.3.1",
    "@angular/router": "^3.3.1",
    "core-js": "^2.4.1",
    "marked": "0.3.6",
    "rxjs": "^5.0.1",
    "ts-helpers": "^1.1.1",
    "zone.js": "^0.7.2",
    "bootstrap": "3.3.7",
    "jquery": "2.1.4",
    "jquery-sticky": "1.0.4",
    "jquery.easing": "1.3",
    "owl.carousel": "2.2.0",
    "magnific-popup": "1.1.0"
  },
  "devDependencies": {
    "@types/jasmine": "2.2.30",
    "@types/marked": "0.0.28",
    "angular-cli": "^1.0.0-beta.28",
    "codelyzer": "1.0.0-beta.1",
    "jasmine-core": "2.5.2",
    "jasmine-spec-reporter": "2.5.0",
    "karma": "1.2.0",
    "karma-chrome-launcher": "^2.0.0",
    "karma-cli": "^1.0.1",
    "karma-jasmine": "^1.0.2",
    "karma-remap-istanbul": "^0.2.1",
    "protractor": "4.0.9",
    "ts-node": "1.2.1",
    "tslint": "^4.3.0",
    "typescript": "2.0.3"
  }
}

and the following angular-cli.json:

{
  "project": {
    "version": "1.0.0",
    "name": "website"
  },
  "apps": [
    {
      "root": "src",
      "outDir": "../webapp",
      "assets": [
        "assets",
        "favicon.ico"
      ],
      "index": "index.html",
      "main": "main.ts",
      "test": "test.ts",
      "tsconfig": "tsconfig.json",
      "prefix": "app",
      "mobile": true,
      "styles": [
        "../node_modules/bootstrap/dist/css/bootstrap.min.css",
        "assets/styles/themify-icons.css",
        "../node_modules/owl.carousel/dist/assets/owl.carousel.min.css",
        "../node_modules/owl.carousel/dist/assets/owl.theme.default.min.css",
        "../node_modules/magnific-popup/dist/magnific-popup.css",
        "assets/styles/styles.css"
      ],
      "scripts": [
        "../node_modules/jquery/dist/jquery.min.js",
        "../node_modules/bootstrap/dist/js/bootstrap.min.js",
        "../node_modules/jquery-sticky/jquery.sticky.js",
        "../node_modules/jquery.easing/jquery.easing.min.js",
        "../node_modules/owl.carousel/dist/owl.carousel.min.js",
        "../node_modules/magnific-popup/dist/jquery.magnific-popup.min.js",
        "assets/js/app.js"
      ],
      "environments": {
        "source": "environments/environment.ts",
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],
  "addons": [],
  "packages": [],
  "e2e": {
    "protractor": {
      "config": "./protractor.conf.js"
    }
  },
  "test": {
    "karma": {
      "config": "./karma.conf.js"
    }
  },
  "defaults": {
    "styleExt": "css",
    "prefixInterfaces": false
  }
}

assets/js/app.js is bundled within scripts bundle Javascript and added in index.html before main bundle, like below:

<script type="text/javascript" src="inline.c4f83930cdfce013fa53.bundle.js"></script>
<script type="text/javascript" src="scripts.87a08269b31e920c5ae1.bundle.js"></script>
<script type="text/javascript" src="vendor.1f7ce844059add8c0d05.bundle.js"></script>
<script type="text/javascript" src="main.d1e0312785d030472370.bundle.js"></script>

Is it possible to have scripts bundle after main bundle ? I need this because the external assets/js/app.js needs to be loaded after everything else gets loaded.

Thanks.

PS: I posted the same question also on stackoverflow: http://stackoverflow.com/questions/42636224/angular-cli-external-js-library-not-loaded-properly

angheladrianclaudiu avatar Mar 07 '17 14:03 angheladrianclaudiu

You might be able to hack around this by importing the app.js at the end of your main.ts. You might need to fiddle around a bit to find the right file, but it should eventually end up at the end of your bundle.

Can I ask why app.js has to be loaded after your angular app, though?

mattdistefano avatar Mar 07 '17 19:03 mattdistefano

Hi. Thanks for the hint. I will give it a try. This file has to be loaded after the main bundle because it's an external JS library that is needed by the layout. More precisely, I am trying to break into components some HTML template I bought and turn it into an Angular 2 app. And this JS holds the logic for the header staying sticky on top. Please see here the template that is not yet turned into an Angular 2 app: https://eventpromoter.ro: note the nav bar staying sticky on top when scrolling down, and the effect when clicking on menu links. These are contained inside app.js.

angheladrianclaudiu avatar Mar 07 '17 22:03 angheladrianclaudiu

Yeah glancing at the code I suspect this'll be a little tricky - most likely you need this code not just executed after your main bundle, but executed after certain angular lifecycle hooks to ensure the DOM is in the appropriate state.

So, ideally, you'd chop up that JS file and locate the different bits of functionality within the appropriate component. As a quick hack, you could try copying the contents of the app.js into the afterViewInit hook of your app component.

mattdistefano avatar Mar 07 '17 22:03 mattdistefano

I should add - I wouldn't really consider this a CLI issue so probably this should be closed.

mattdistefano avatar Mar 07 '17 22:03 mattdistefano

Thanks @mattdistefano. Closing.

angheladrianclaudiu avatar Mar 07 '17 22:03 angheladrianclaudiu

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.