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

Why angular inject scripts in my code because I want to inject manually!

Open alex2sorin opened this issue 8 years ago • 12 comments

Angular 6 inject scripts in my code, and I don't want that!

capture first

This happens after I run my .net core app which uses static files generated by angular cli with ng build command

alex2sorin avatar Aug 08 '18 12:08 alex2sorin

What's the use case? Could you also help us understand why you don't want these scripts? Do you plan to add them manually in your HTML?

alexeagle avatar Aug 10 '18 16:08 alexeagle

Angular CLI, requires 3 tags to be present in the provided index file, app-root, head and body, and not having the last 2 is causing your compilation error. As @page directives always need to be the first thing in the view.

I think what you might really want here is for Angular CLI not to process an index file. However, how to get around your issue, and is based on how your .NET application is setup.

Scenario 1. You are using a standard .NET and Razer configuration, without a custom view locator that processes the view that will be outputted to the dist folder. You wouldn't need fro Angular-CLI to process the view as it won't be able to be located and processed by Razor, as the view engine will only try too look for views in src/Views.

Now you can go around your error, simply by providing a dummy index.html, that won't be used by your view engine.

Scenario 2. You are using a custom view locator to locate your views in the dist folder rather than src/Views. In this case you should be able to update your index.cshtml;

@page
@model RazorPagesContacts.Pages.HomeModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

<html>
<body>
   <app-root></app-root>
</body>
</html>

If you are using layouts you can also do something like the below;

<!DOCTYPE html>
<html>
<head>
</head>
<body>    
    <app-root></app-root>
   @RenderBody()
</body>
</html>

In think that scenario 2, is the best one, for various reasons, such as you can use the output-hashing without the need that on every build you need to change manually the paths of the JS & CSS. No room to error, as when doing the files manually it's easier to skip a file or miss something especially during upgraded. Or in your above case, a css file is placed in a script tag (Line 21).

More info about view locators; https://samueleresca.net/2016/07/net-core-1-0-customize-view-paths/ https://weblogs.asp.net/imranbaloch/view-location-expander-aspnet5-mvc6

I hope that this helps you.

alan-agius4 avatar Aug 11 '18 12:08 alan-agius4

Angular CLI injects these scripts because they are derived via internal logic from the Angular CLI configuration. We don't have an option to not add them because a user cannot easily determine which should be added or not.

filipesilva avatar Aug 15 '18 14:08 filipesilva

I have a similar scenario. My workaround was to move away from angular cli and use custom webpack build. this way I can remove HtmlWebpackPlugin from my build process entirely. However, It would be great if a command line switch could be added which prevents angular cli from modifying index.html. something like --preserve-index-html or --disable-script-injection

chintans avatar Aug 22 '18 07:08 chintans

I wonder if we can use ngx-build-plus to overcome this kinda issues.

my case is I don't use index.html at all, but I still want to inject the scripts into 2 files, one for scripts and the other one for style.

I tried to use

 plugins: [
    new HtmlWebpackPlugin({  
      filename: 'test.html',
      template: 'src/assets/test.html'
    })
  ]

but it doesn't work. It did generate a file but no scripts.

The other way I have tried is using Gulp. This way does work. Just not pretty.

maxisam avatar Oct 13 '18 10:10 maxisam

We often create Angular-Apps and host them inside a Content Management System, where index.html is included/embedded on the server side and the host-website already contains the html- and body-tags.

Angular cli 6.2.5 seem to add the script-tags before the closing </body> or at the very beginning of index.html (if there is no </body>-tag). There seems to be no convenient way to either:

  1. disable script-injection entirely
  2. or have the script-tags at the end of index.html, even if no </body> is present.

Today we use manual workarounds for this limitation but with downsides of not having automatic differences between development and production builds. We would very appreciate if such a feature is added.

BeniFreitag avatar Oct 18 '18 09:10 BeniFreitag

Any news about this? I need to migrate ionic 3 application to ionic 4 which uses angular 6, and I also need to add those build files manually.

BDominik avatar Jun 13 '19 07:06 BDominik

Would be nice if we could define in the config a query selector to determine which elements would get the css and scripts injected into.

RobJacobs avatar Feb 20 '20 02:02 RobJacobs

Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

Find more details about Angular's feature request process in our documentation.

angular-robot[bot] avatar Feb 01 '22 18:02 angular-robot[bot]

Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

You can find more details about the feature request process in our documentation.

angular-robot[bot] avatar Feb 21 '22 13:02 angular-robot[bot]

Thanks for reporting this issue. This issue is now obsolete due to changes in the recent releases. Please update to the most recent Angular CLI version.

If the problem persists after upgrading, please open a new issue, provide a simple repository reproducing the problem, and describe the difference between the expected and current behavior.

alan-agius4 avatar Dec 12 '23 13:12 alan-agius4

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.