How to import RequireJS (AMD) module into Angular 4.x (Angular Cli) application?

I have an obsolete JS file written in the following notation of the RequireJS module (AMD):

define('mymodule', [], function(){

    // ... bla bla bla

    return {
        calculate: function() { return 1 + 1; }
    };
});

I am importing this file from another (old) project that uses RequireJS, therefore - I cannot change the definition of the module used .

I want to import it into an Angular Cli application (Angular 4.x) written in TypeScript.

Since Angular Cli uses Webpack 2 to create projects that support AMD, I thought I could import it as follows:

import * as MyModule from './mymodule.js';

... but this does not work, it causes an error that is mymodule.jsnot a module.

Any ideas (or workarounds) on how I can import this deprecated module?

+4
1

amd.

TS:

import * as MyModule from './mymodule.js';

console.log('my value is', MyModule.value);

mymodule.js:

define(["require", "exports"], function(require, exports){
   exports.value = "aaa";
});

tsconfig.js

"allowJs": true

UPDATE:

System.import, webpack.

 declare var System: any; 

 System.import('./mymodule.js')
     .then(MyModule=>console.log(MyModule.value));
+8

Source: https://habr.com/ru/post/1677698/


All Articles