The presence of an Angular Element within an Angular application can lead to a problematic cycle of constant reloading,

, I am encountering issues with integrating Angular Elements as plugins into my Angular application. The problem arises when building the element with "--prod" - it functions properly with "ng serve" in my development setup but causes infinite reloading when using "ng serve --prod" or after running "ng build --prod" for production. An attempt to mitigate this involved building the element with "--optimization=false", which worked for the production app but not in the development setup. I had hoped that building an Angular Element with "--prod" would work seamlessly for both scenarios. Is there a feasible solution to resolve this discrepancy? Further details reveal our objective at work of implementing configurable plugins within our Angular site where the server dictates the active plugins. While exploring dynamic loading of Angular modules, we encountered challenges that were set aside for future consideration. Subsequently, we explored Angular Elements for its potential, only to face obstacles in adhering to the recommended build process. Initial steps included setting up a core application as the plugin host and creating an Angular Element for the plugin itself. Modifications were made to various configuration files and code snippets according to online tutorials and recommendations. The overall structure entailed scripts in the package.json file to facilitate building processes, along with adjustments to component templates and TypeScript files to accommodate the desired functionality. Despite the workaround implemented to ensure functionality in both development and production environments by loading different builds based on optimization settings, it leaves room for improvement regarding optimized code usage. For further insights and hands-on experience with the issue, you can refer to the GitHub repository link provided above. Your feedback and suggestions are welcome as we strive to enhance this integration for a seamless user experience.

Answer №1

Success! I've cracked the code!

The culprit, as it turns out, is running multiple webpack projects simultaneously in the same environment. Essentially, when you compile projects with webpack, they undergo some runtime bootstrapping that relies on a specific global function (webpackJsonp). When more than one webpack setup tries to perform this bootstrapping in the identical context, it triggers the issues outlined here. (For a more detailed breakdown, check out this explanation - https://example.com/unique-link-here)

A GitHub comment offers a workaround without diving into the step-by-step process - https://example.com/different-link-here. Below, I'll share my tailored fix.

We can tweak the webpack configuration to rename the webpackJsonp for our web component, ensuring harmony between different Angular projects (or any webpack-built applications).

The Fix

To start, we need to add the @angular-builders/custom-webpack package to modify the default webpack setup in Angular CLI.

npm install --save-dev @angular-builders/custom-webpack

Next, we need to update our angular.json file to utilize the new builder and introduce a fresh property value, customWebpackConfig, under options. This feature specifies a path to a new file we're about to create and sets a merge strategy to append configurations to the output section of webpack settings.

angular.json
...
      "architect": {
        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./extra-webpack.config.js",
              "mergeStrategies": { "output": "append"}
            },
...

Lastly, we simply create an extra-webpack.config.js file in the directory containing angular.json. The file should include the following snippet:

module.exports = {
    output: {
        jsonpFunction: '<webcomponent-prefix>-webpackJsonp'
    }
}

If you alter the value of jsonpFunction from its default

webpackJsonp</code, the setup will still function appropriately. Personally, I chose to preserve the function name but incorporate a prefix for my web component application. Ideally, you could have multiple webpack setups coexisting in the same environment, provided each possesses a unique <code>jsonpFunction
.

Recompile your web component, and voilà, problem solved!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

activate a single on.click event to trigger additional actions - utilizing javascript and jQuery

My website features dynamically generated buttons that lead to specific pages. The challenge I'm facing is retrieving the automatically generated data-num value for each button and using it as a jQuery selector to redirect users to the corresponding p ...

I encountered an error stating "angular not found" when attempting to dynamically load my Angular scripts

My Angular application is running smoothly. However, I found it tedious to include multiple script tags in my HTML documents every time. To simplify this process, I decided to create a small script that would automatically generate the necessary tags for m ...

The Bootstrap modal-backdrop dilemma requiring JavaScript intervention

I am encountering some problems with the Bootstrap modal. When I try to open a modal, everything looks good, but when I close it, the screen turns completely black. Upon closer inspection, I found that every time I click on the X to close the modal, there ...

What are some effective ways to stretch specific muscles?

I am facing an issue with my select element. The default value is "please choose," but there are options like "Accommodation & Hotels" that are longer and not clearly visible on IE browsers, though they work fine in Firefox. How can I resolve this issue? S ...

"Hidden panels in Sencha Touch only respond to show() and hide() methods after a resize event

Check out this demonstration of a Sencha Touch app by visiting this link. The button located in the bottom-left corner is supposed to show or hide the menu panel on top of the "Location info goes here" bar, but it seems to be functioning in an unexpected m ...

Handling Errors with Symfony 5 JSON Responses and VueJS Using Axios for Customized Error Messages

I need to show a personalized error message when my JSON response throws an error. Some of my services trigger an error like this: if (count($recipients) === 0) { throw new TransportException($this->carrierService::ERROR_NO_MAIL_ADDRESSES); } The ...

Exploring LZMA compression in conjunction with next.js

Is anyone familiar with using the lzma algorithm to compress video, photo, text, etc. in the Next.js framework? I have searched everywhere but couldn't find a solution. I haven't found a proper demonstration or answer anywhere. I would greatly a ...

Click on the button to add a new question and watch as another row magically appears

Working with ReactJS My goal is to display 10 rows by default, followed by a button labeled "Add a new question" which would create the 11th row. View current row image here Currently, only one row is being shown [referencing the image below]. I aim to ...

In TypeScript, what specific term denotes a type of data?

Given the following code snippet: class Foo { } interface TypeProvider() { type(): ?; } class Bar implements TypeProvider { type(): ? { return (Foo); } } class Baz implements TypeProvider { type(): ? { return (Bar); ...

What is the process for retrieving information from a database in Django when the submit button is clicked?

Is there a way to retrieve database objects in HTML only when the submit button is clicked, instead of loading them automatically on page load? Currently, I have an AJAX script running on a form that displays text entered in a textbox onto the HTML when th ...

Incorporate Aria Label into a Website Link

Currently working on enhancing website accessibility. I have identified a close menu button that lacks an Aria Label, and my goal is to rectify this using JavaScript. Although I am utilizing the script below to target the specific ID and add the attribute ...

Encountering difficulties in generating a binary from a nodejs application with pkg

I am having trouble generating a binary executable from my nodejs app using the pkg command. My nodejs app is simple and consists of only three .js files: index.js, xlsx_to_pdf.js, and xlsx_extractor.js. This is how my package.json file looks like: { & ...

Guide to sending a HTTP POST request with parameters in typescript

I need assistance sending a POST request using parameters in the following format: http://127.0.0.1:9000/api?command={"command":"value","params":{"key":"value","key":"value","key":"value","key":value,}} I attempted to do this but encountered an issue: l ...

AgGrid supports multi-line content within its cells

I attempted to utilize this solution, however, it is not functioning properly for me. While it does resize the column height correctly, the text is still not wrapped as expected. Ag-Grid - Row with multiline text let gridOptions = { columnDefs: column ...

Setting up a Node.js application with Nginx on DigitalOcean

While running my application on a DigitalOcean droplet using nginx, I encountered a peculiar issue. The app runs perfectly fine with http, but when switching to https, nginx throws a 502 BAD GATEWAY error. Despite trying various DigitalOcean guides and sco ...

Ways to stop two JavaScript files from running at the same time

After combining two JavaScript files, I am facing some issues. The first file handles validation while the second one has an ajax plugin for form submission after validation. When I include these files in the header section, they both run simultaneously. H ...

Why are these additional curly brackets added within an else statement?

Upon reviewing some typescript code, I noticed the presence of extra curly braces within an else block. Are these additional braces serving a specific purpose or are they simply used to provide further grouping for two nearly identical code snippets? Consi ...

Retrieve the response status using a promise

There is a promise in my code that sometimes results in an error response (either 400 or 403, depending on the user). I am trying to handle this situation by catching the response and implementing a conditional logic to execute different functions based on ...

Skipping the configuration file during the process of converting Node.js files to an executable using pkg

I am currently working on a node.js application and in order to prevent users from reading the code, I am attempting to convert my JavaScript files to executable files. I have been using the pkg module for this task, but I have encountered an issue. The p ...

What is the best way to create a navigation bar that opens on the same page when clicked?

Can someone help me figure out how to create a navbar that, when clicked, opens a small window on the same page like in this example image? ...