Using Webpack to bring in AngularJS modules

Our current project is transitioning from AngularJS to Angular 4, and as part of this process, we are first rewriting everything in TypeScript and converting AngularJS components. To facilitate this transition, we have set up a new project using AngularJS + Webpack + TypeScript, where we are converting legacy AngularJS modules to TypeScript. However, due to the size of the project, we need to temporarily include the 'old' AngularJS modules and execute their scripts to ensure that the project functions correctly.

We are facing challenges in integrating these old modules into our project structure. Our app.module.ts file looks something like this:

import * as angular from 'angular';
import { moduleName as convertedModule1 } from './app/converted.module1';
import { moduleName as convertedModule2 } from './app/converted.module2';

export const moduleName =
    angular.module('application', [
        convertedModule1,
        convertedModule2,
        'legacy_module_3',
        'legacy_module_4'
    ]).name;

While convertedModule1 and convertedModule2 have been successfully migrated to TypeScript, 'legacy_module_3' and 'legacy_module_4' remain in their original AngularJS form. We are looking for a way to reference these old modules (located at '/frontend/module3.js' and '/frontend/module4.js') within our webpack setup so that their JavaScript code can be executed. Any suggestions on how we can achieve this integration?

Answer №1

Instead of tackling each module individually, consider taking the plunge and doing a minimal conversion on everything at once.

I recently underwent a similar process when converting an existing angularjs project to utilize webpack. The changes required for each module were minor enough that I decided to convert all modules in one go. For instance:

angular.module('somemodule').controller(function(){ ... })

was converted to:

export default function SomeController() { ... }

and the module declaration files now look like this:

import SomeController from './some.controller'
export default angular.module('SomeModule', [])
.controller(SomeController)
.name;

At the top level:

import SomeModule from './some/module'
angular.module('app', [ SomeModule ]);

It took some time, but since most changes were straightforward, I was able to methodically work through the entire application. In some sub-folders where only the 'app' module name was used, I simply added a 'module.js' file with an appropriate module name, with no other significant changes needed.

I also had to convert all template urls into imports and imported the '.scss' file at the top level of the app (although it wasn't strictly necessary).

The next step will involve converting controllers and directives into components, which should be a relatively simple task at this point.

Answer №2

To organize your code properly, make sure to export each of your modules and then import them into your main app module. Take a look at this example:

// Import your other TypeScript modules
import module3 from './frontend/module3.js' // Make sure to use relative paths
import module4 from './frontend/module4.js'

export const moduleName =
    angular.module('application', [
        module1,
        module2,
        module3.name ,
        module4.name
    ]).name;

Additionally, remember that your old JavaScript modules must also export a module:

For example, the filters module.

import angular from 'angular';
import myNiceFilter from './myNiceFilter.filter';

var filters = angular.module('filters',[]);
filters.filter('myNiceFilter', myNiceFilter);

export default filters;

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

Converting a TypeScript nested dictionary into a list of strings

I am currently working with a nested dictionary and my goal is to convert it into a list of strings. For example, the initial input looks like this: var group = { '5': { '1': { '1': [1, 2, 3], ...

Deciphering a mysterious message in Typescript while defining a function for one of my tasks

Currently, I am working with a stack that includes React, TypeScript, and Redux. Unfortunately, I have encountered an issue while using an interface for one of my actions. The error message I received is quite cryptic: Duplicate identifier 'number&apo ...

Utilizing System.import with Webpack 2 and React for splitting code and managing nested routes

I followed the instructions from a tutorial article on Modus Create titled Code Splitting for React Router with ES6 Imports to create an app. I made some modifications by adding children routes, resulting in the following router configuration: function er ...

Leveraging React and TypeScript's capabilities to pass around arguments efficiently

As I integrate TypeScript into my application, I find myself at a juncture where I need to specify the following: { id, label, type, styles, ...props } Incorporating this structure into a component like the one below: const TestComponent = ({ id, label, t ...

Will a JavaScript source map file continue to function properly even after the source code file has been minified?

My experience I specialize in utilizing TypeScript and Visual Studio to transform highly organized code into functional JavaScript. My skills involve configuring the project and Visual Studio to perform various tasks: Merging multiple compiled JavaScrip ...

Quick method for executing TypeScript files

Overview Currently, I am running a TypeScript program from this repository. node --loader ts-node/esm ./examples/ts/cli.ts bitget fetchBalance The TypeScript version of this program takes significantly longer to run compared to the JavaScript version. no ...

Transform a string into a class in Typescript/Angular

In my application, I've created a reusable modal popup component that takes a string as input and dynamically loads other components based on that input. This approach allows me to use the same modal popup component for multiple modals in the app inst ...

Having issues with $timeout functionality in angular.js

I've implemented $timeout in my controller but it doesn't seem to be functioning correctly. app.controller("Friendsrequests",['$http','$log','$location','$timeout','$scope', function($http,$log,$ ...

angular does not update dynamically created dropdowns with populated arrays

Upon loading the page, my initial dropdown functions correctly. However, the child dropdown is loaded from localstorage after the first dropdown. The issue arises when I load the second dropdown and set it to the default value - at this point, I am unabl ...

Creating dynamic columns in tables using AngularJS by clicking on ng-repeat items

The backend is providing me with this type of response. [ { "id": "1", "name": "Roshan", "av": "1000", "compname": [ { "id": "43", "cname": "TCS", "constraint_value": "", "details": [ { ...

Dynamic scope variables in AngularJS are powerful tools that allow you

I am currently working with a scope variable called jsonData which looks like this: $scope.jsonData={id:'1234',abcd:{array:[{a:'data',b:'bdata',c:'cdata'},{a2:'a2data',b2:'b2data',c2:'c2data ...

The integrity subresource feature in webpack does not have any impact or influence

I have been attempting to implement the plugin on my nuxtjs's webpack configuration in order to enable subresource integrity. Below is my nuxt.config.js file: extend(config) { config.output!.crossOriginLoading = "anonymous"; }, ...

Exploring Angular 5's BehaviourSubject for validating multiple email fields

Working with Angular5, I have a project page that includes an upload feature using a core upload component from my organization. Users can upload up to 5 files and must fill in the email field before clicking the "upload" button. My goal is to trigger a s ...

Retrieve data upon component mounting and deactivate the query in React-query

When navigating to a search result page, query parameters are passed to useQuery. I want the data to be fetched only when the user clicks the "Search" button after changing the search prompt. I attempted to use enabled: false and call refetch() on button ...

What is the reason for the presence of additional mandatory dependencies in the package-lock.json file?

The recent release of React includes breaking changes to the TypeScript typings, causing packages that require "@types/react" with a wildcard version to automatically target this new version and break my project. Initially, I wanted to reach out to projec ...

Filtering by element of an array in Angular.js

I have a collection of json objects, for instance: $scope.users = [ {name:'Maria',age:25,skills["Angular.js","Node.js"]}, {name:'Maxime',age:28,skills["HTML","MongoDB"]}, {name:'Noemie',age:28,skills["CSS","Mongo ...

One function in Typescript lodash is missing a default export

Is there a way to import just one function from lodash? I attempted it like this: import get from 'lodash/get'; Even after installing both lodash and @types/lodash, I encountered the following error message: @types/lodash/get/index"' ha ...

React JS displayed the string of /static/media/~ instead of rendering its markdown content

When I looked at the material UI blog template, I created my own blog app. I imported a markdown file using import post1 from './blog-posts/blog-post.1.md'; Next, I passed these properties to this component like so: <Markdown className=" ...

Getting the specific information you need from a JSON object in Angular 2

Struggling to extract specific data from a JSON file with nested objects like this: { "results" : [ { "address_components" : [ { "long_name" : "277", "short_name" : "277", "types" : [ "street_number" ] ...

Having trouble resolving React within the Formik/dist package due to a custom webpack configuration

Struggling to set up projects from scratch, encountering an issue with webpack not being able to resolve formik's modules while other third-party modules like styled-components work fine. I've tried searching online for a solution but couldn&apos ...