What is the process for transferring an environment.json file to the output directory and then utilizing it with Webpack 4?

Our application is expanding with multiple environments and vendors on the horizon. While the traditional approach of running

webpack --env.NODE_ENV=myenvironment
works for now, it will soon become inefficient.

The main objective here is to streamline the process by simply copying the environment.json file into the output folder. This way, I can handle transformations in Octopus Deploy without the need for multiple builds.

Essentially, I want to use a single environment.json file for variable substitutions in Octopus Deploy. Here's a peek at what that might look like:

{
    "production": false,
    "baseUrl": "http://myapp.com",
    "appId": "MyAppId"
}

In my AngularJS code, I'm setting up constants based on these values:

import * as environment from '../../environment.json';
console.log(environment.baseUrl); // http://myapp.com

angular.module('app')
  .constant('environment', environment);

Seems straightforward so far.

The issue arises when I make changes within the dist/environment.json file (located in the output folder) and those updates don't get reflected. For instance, if I update the value of baseUrl to http://myapp-dev.com, the console still shows http://myapp.com.

Here's a snippet of code from my webpack.config.js:

Rules

...
{
  test: /\.json$/,
  use: 'json-loader',
  exclude: [/environment\.json$/]
},
... 

Plugins

...
new CopyWebpackPlugin([{
  from: 'environment.json'
},
...

While this code successfully copies the environment.json file to the output folder, the other files there seem unaffected. It seems like they are still tied to webpack somehow.

Answer №1

CopyWebpackPlugin simply copies a file without any further actions taking place, and Webpack does not utilize it internally. The static JSON import is likely included in your main bundle js.

Instead of attempting to dynamically interchange certain parts of the build, consider defining an external module. This approach grants you the flexibility to provide any desired globals to the browser environment, and webpack will be able to locate them at runtime.

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

What is causing the reluctance of my Angular test to accept my custom form validation function?

I'm currently facing an issue with testing an angular component called "FooComponent" using Karma/Jasmine. Snippet of code from foo.component.spec.ts file: describe('FooComponent', () => { let component: FooComponent let fixture ...

Transform the date and time into a timestamp with milliseconds while adding one month

Issue with high chart not rendering in Internet Explorer and Safari has been identified. The solution to this problem is converting the date from the API into a timestamp in milliseconds. Here is an example code snippet for conversion: var date = '201 ...

Automatically fill in a text box with previously saved information

Does anyone have suggestions on how to create this type of textbox or what it is called? (View original image here) ...

The special function fails to execute within an "if" condition

As a newcomer to JavaScript/jQuery and Stack Overflow, I kindly ask for your patience in case there are any major errors in my approach. I am currently developing an HTML page with Bootstrap 3.3.7, featuring a pagination button group that toggles the visib ...

"Twice the loading of Meteor templates: once with an undefined collection, and once with it

After searching various resources for solutions to my issue, I stumbled upon this helpful and . Both of these links provided valuable insights. The issue I'm facing is that one of my templates is loading twice - first with the collection undefined, ...

The internet browser encountered a JavaScript runtime error (0x800a138f) in which it was unable to retrieve property '0' due to it being either undefined or pointing to a

I'm encountering an issue with my javascript/jquery function that retrieves the selected dropdown value. Everything works fine in Chrome, but when I try to run my application in IE, it throws an error. $("#Application_AppCountries").change(function ( ...

What is the best way to implement a nested lookup in MongoDB within a field?

Within my database, I have a collection named Randomhospital. Inside this collection, there is a field named hospital structured as follows: { "id": "GuDMUPb9gq", "Hospital Name": "UPHI", "Hospital City&qu ...

Issue with jQuery hide() function in Internet Explorer 9

I need help with creating a hidden div that becomes visible when a user clicks a link. The code I have works in FF / IE7 / IE8 but not in IE9, where the div is always visible without any content. Any advice is appreciated! <script> $(document).r ...

Determining the name of an angular module using JavaScript

How can I extract the name of the Angular Module using JavaScript? I need to fetch the module name of an Angular JS application in my JavaScript file to be able to implement additional functionality on that particular module. I am looking for a way to ac ...

Consolidate and then transfer to a different database

How can I insert the data returned from my aggregate (controller function) into another collection called User? Here is the schema for the User: const userSchema = new mongoose.Schema({ firstName: { type: String, required: [true, &ap ...

The compilation process encountered an error: TypeError - Unable to access property 'exclude' as it is undefined (awesome-typescript-loader)

After successfully converting my existing Angular 2 project into Angular 4, I encountered the following error: Module build failed: TypeError: Cannot read property 'exclude' of undefined For more details, please refer to the attached image bel ...

Forward the value of the selected radio button

Currently, I am focusing on this code snippet. <html> <script> var submit = document.getElementById('btnFormSubmit'); submit.addEventListener('click', submitForm); function submitForm(event){ event.preventDefault(); event. ...

Is there someone who can assist me in transferring data from an Axios JSON response to plot points on a line chart using Chart js?

I'm currently in the process of developing a React application that includes a line chart showcasing the timeline of data recordings starting from a specific point in time up to the present day. To achieve this, I am leveraging the Chart.js JavaScript ...

Wrap HTML attributes with double quotes using JavaScript

My goal is to add " around attributes that are missing them, using JavaScript. For example: <a class=external title=Title href="http://www.google.com" rel=external>My link</a> Should become: <a class="external" title="Title" href="http:/ ...

Checkbox ensemble computes total score

I am currently working on a project that involves multiple checkbox groups, with each group containing 3 checkboxes labeled as (1, X, 2). My goal is to assign a value of 100% to each group, distributed evenly among the checkboxes within it. The distributio ...

Display a dynamic variable within React's HTML code

const fetchTime = () => { const currentDate = new Date(); const currentTime = currentDate + ' ' + currentDate.getHours() + ":" + currentDate.getMinutes() + ":" + currentDate.getSeconds(); return {currentTime}; } export default fun ...

What is the process for transferring the "event" object to the functional form of "this.setState()"?

Utilizing the functional form of this.setState() where a function returning an object is passed. When attempting to bind onChange on an input and using this... onInputChange = event => { this.setState(() => ({ ...this.state, [ev ...

Challenges with managing controllers within Directives

Currently, I am in the process of updating some code within a personal project that utilizes Angular to adhere to best practices. I have come across suggestions that the future direction of Angular involves incorporating a significant amount of functionali ...

Exploring the Differences Between JQuery Ajax Page Fragment Retrieval with Find and Filter

I am currently testing out the following Ajax code for a form. $('body').on('submit','#sign-in', function(e) { e.preventDefault(); var data = $(this).serialize(); var url = $(this).attr('action'); ...

Getting the name and value from an object

Just starting out with Javascript and Nodejs, using express and making a call to the following link: localhost:7080/v1/movies/order/notify/insert?breed=Whippet&age=10 After that, I am attempting to extract the property name along with its correspon ...