·Publikoval Frank Hliva·3 min čítania

Problems with Imports in JavaScript

Problems with Imports in JavaScript

I remember the days when JS did not officially support modules. Developers had to bend objects to their will, and visibility/scope was handled via closures. The only thing JS did not have solved was loading individual files. Many got around this by dynamically adding script tags to the document header.

Even back then, I used a preprocessor that merged all JS files into a single bundle… which also minimized the number of HTTP requests.

Note: Today, optimizing the number of requests is not as crucial, but in the days when HTTP 1.0 ruled the internet, every single transferred file meant a separate request (HTTP 2.0 optimizes these connections much better).

Today (thanks to Webpack and native browser support), JavaScript officially supports modules. They have even become a part of the HTML5 standard. However, these modules often bring more headaches than benefits.

Module Problems in TypeScript:

  1. Problem – You cannot easily split a single module across multiple files.
  2. Problem – You cannot easily declare a nested module in a separate file from its parent module.
  3. Problem – You cannot import the entire contents of a module without a prefix unless you explicitly list all of its exports (something taken for granted in other languages). In JS, you must list every single identifier you want to import:
import { ClassName, function1, function3, ..., function1000 } from "/module";

or you have to use the wildcard * operator:

import * as namespaceName from "/module";

but in that case, you have to call them using the namespace prefix:

namespaceName.function1();

In the past, you could bypass this using a hack like:

for (let k in importedModuleObject) {
    eval(`var ${k} = importedModuleObject.${k}`);
}

This no longer works in modern JS due to strict mode. Today, it could be solved more elegantly at the Webpack loader level, but evidently, nobody wants to put in the effort.

Note: Do not use eval! Eval is evil: both due to security risks and poor performance (code stored in strings is hard for modern JS engines to optimize).

  1. Problem – In JS, we have two types of exports: default and named exports. Default exports allow you to import individual parts, while named exports are often easier to import (a decent IDE will import them automatically!). Unfortunately, in TS, you cannot easily export a single module using both methods.

But since it’s you, let me show you a dirty hack anyway:

File ./NamedModule/AnonymousModule.ts:

export const alertX = function() {
    alert("X");
}

export const alertY = function() {
    alert("Y");
}

export const alertZ = function() {
    alert("Z");
}

File ./NamedModule/index.ts:

import * as NamedModule from './AnonymousModule';
export { NamedModule };

And then you can import it beautifully in either of these ways:

import { alertX, alertY } from '@lib/NamedModule/AnonymousModule';

or like this:

import { NamedModule } from '@lib/NamedModule';

Of course, this doesn’t solve problems 1 through 3.

These are the issues with TypeScript modules in combination with bundlers like Webpack. In pure JS, there are slightly fewer restrictions, and I am not entirely sure if point 4 applies to JS as well since I have been writing exclusively in TypeScript for the last two years. Most of these modular issues were created by the authors of today’s specifications. The implementation was simply not fully thought through, causing front-end developers to struggle with problems that other software development platforms solved back in the mid-1990s.

Share:
Späť na blog

Súvisiace čítanie

Ďalšie články z blogu

Zobraziť všetky články