Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use js modules from non-module files

I'm a beginner at using js modules.

I'm working on a fairly simple web application. It uses typescript and angular 2, which heavily relies on modules.

Most of my app ts files 'import' one or many js modules (usually mostly angular 2 modules).

As I understand, because my app ts files have a top level 'import', they are automatically considered a js module by typescript.

However, I want any of my app ts files to be accessible by any other of my app ts files, without having to 'import' each other. But because they are now modules themselves, ts requires me to do that...

Is it possible?

It seems crazy to me that for each of my app ts file, I should have to declare every other of my app ts files that are used in there (I like to have tiny files with a single class/interface). In addition, this relies on relative paths which breaks as soon as I restructure my folder structure.

Am I thinking about this the wrong way?

like image 217
Clement Avatar asked Sep 07 '16 23:09

Clement


People also ask

How do you implement modules in JavaScript?

You can import modules into a file in two ways, based on if they are named exports or default exports. Named exports are constructed using curly braces. Default exports are not.

How do I import a module in HTML?

In HTML, this is done by adding type="module" to the <script> tag. Modules are automatically interpreted in strict mode. There is also a function-like dynamic import() , which does not require scripts of type="module" .

How do I call a node js function from another file?

To include functions defined in another file in Node. js, we need to import the module. we will use the require keyword at the top of the file. The result of require is then stored in a variable which is used to invoke the functions using the dot notation.


1 Answers

You must have a js file which is an entry point to your application right?.. So in that file just import all the modules which you want to access without importing and attach them to the window object. Since the window object is available globally, you can access your module from anywhere without importing the corresponding module. For example,

Consider this scenario: You have a module in a file called module1.ts The entry point of your application is a file called index.ts And you have a module2 where you require something from module1

// module1.ts
function add(first: number, second: number): number {
    return first + second
}
export {add}

in your index.ts

// index.ts
import {add} from '<path to module1>/module1';

window.add = add

Now in your module2

// module2.ts
window.add(1, 2)

Since the window object is available globally you can attach as many properties to it as you like. As far as the type resolution is concerned you can declare a window module with the add function you require in a .d.ts file as follows:

declare module window {
add: (first: number, second: number) => number 
}
like image 187
Nahush Farkande Avatar answered Oct 22 '22 07:10

Nahush Farkande