Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong file order in index.html when bundleName is used #13568

Closed
matthiaskomarek opened this issue Feb 1, 2019 · 7 comments · Fixed by #13571
Closed

Wrong file order in index.html when bundleName is used #13568

matthiaskomarek opened this issue Feb 1, 2019 · 7 comments · Fixed by #13571

Comments

@matthiaskomarek
Copy link

🐞 Bug report

Command (mark with an x)

- [x] build
- [x] serve

Is this a regression?

No

Description

When i use a bundleName like the following inside the angular.json for the styles array. The order of scripts generated in the index.html are in the wrong order.

angular.json

"build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist",
            "index": "src/index.html",
            "main": "src/ng2/main.ts",
            "polyfills": "src/ng2/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/ng2/favicon.ico",
              "src/ng2/assets"
            ],
            "styles": [
              { "input": "src/ng2/main.scss", "bundleName": "main" }
            ],
            "scripts": [
              "build/main-ng1.js"
            ]
          },

Order of scripts in index.html after running ng build

<script type="text/javascript" src="runtime.js"></script>
<script type="text/javascript" src="polyfills.js"></script>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript" src="scripts.js"></script>

The Problem is, that the main.js get inserted before the scripts.js and this will fail if you have some external dependency added via scripts.js

I have already digged into the implementation in @angular-devkit/build-angular/src/angular-cli-files/plugins/index-html-webpack-plugin.js and found out that the problem is the bundle name main which is added into the this._options.entrypoints array before the scripts entrypoint is added.

🔬 Minimal Reproduction

See example above

🌍 Your Environment


Angular CLI: 7.3.0
Node: 8.11.1
OS: darwin x64
Angular: 7.2.0
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router, upgrade

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.12.1
@angular-devkit/build-angular     0.12.1
@angular-devkit/build-optimizer   0.12.1
@angular-devkit/build-webpack     0.12.1
@angular-devkit/core              7.2.1
@angular-devkit/schematics        7.3.0
@angular/cli                      7.3.0
@ngtools/webpack                  7.2.1
@schematics/angular               7.3.0
@schematics/update                0.13.0
rxjs                              6.3.3
typescript                        3.2.2
webpack                           4.23.1
@alan-agius4
Copy link
Collaborator

alan-agius4 commented Feb 1, 2019

Hi, I was unable to replicate this.

<script type="text/javascript" src="runtime.js"></script>
<script type="text/javascript" src="es2015-polyfills.js" nomodule></script>
<script type="text/javascript" src="polyfills.js"></script>
<script type="text/javascript" src="styles.js"></script>
<script type="text/javascript" src="scripts.js"></script>
<script type="text/javascript" src="vendor.js"></script>
<script type="text/javascript" src="main.js"></script>

Can you try to update to the latest version of @angular-devkit/build-angular?

This seems like a bug but we'll need to look at a reproduction to find and fix the problem. Can you setup a minimal repro please?

If the problem persists, please provide a reproduction good way to make a minimal repro is to create a new app via ng new repro-app and adding the minimum possible code to show the problem. Then you can push this repository to github and link it here.

@alan-agius4 alan-agius4 added the needs: repro steps We cannot reproduce the issue with the information given label Feb 1, 2019
@matthiaskomarek
Copy link
Author

Hey @alan-agius4 thanks for the fast reply. I think the issue occurs only when you add a bundleName of main to the styles config.
I have setup a repo here: https://github.com/matthiaskomarek/issue-app

When you run ng build you will see that the scripts.js file is now loaded after the main.js.

I have done a quick-fix on my side (via patch-package) in the index-html-webpack-plugin.js by reordering the scripts array. But i am not sure if this fix will fit your desired solution.

@alan-agius4
Copy link
Collaborator

Hi, thanks for the repo.

The root cause is that the name chosen for the css bundle is conflicting with the default module for the main.js script.

@alan-agius4 alan-agius4 added type: bug/fix freq1: low Only reported by a handful of users who observe it rarely severity3: broken workaround1: obvious area: @angular-devkit/build-angular and removed needs: repro steps We cannot reproduce the issue with the information given labels Feb 1, 2019
@ngbot ngbot bot added this to the Backlog milestone Feb 1, 2019
@clydin
Copy link
Member

clydin commented Feb 1, 2019

The CLI should emit an error in the case a bundle name option conflicts with a built-in name.

@matthiaskomarek
Copy link
Author

matthiaskomarek commented Feb 1, 2019

Or, as i mentioned. The cli could reorder the scripts array before it gets inserted into the index.html file.
I could come up with a PR, if this is a proper solution for you guys

@alan-agius4
Copy link
Collaborator

@matthiaskomarek, re-ordering alone won't solve all the issues as this might also effect bundle budgets and other webpack plugins that we use.

alexeagle pushed a commit that referenced this issue Feb 13, 2019
… have been name the same

Naming more than 1 bundle with the same name might cause undefined behavior.

Fixes #13568
@angular-automatic-lock-bot
Copy link

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.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants