In a project I\'m collaborating on, we have two choices on which module system we can use:
require
, and exporting using
Not sure why (probably optimization - lazy loading?) is it working like that, but I have noticed that import
may not parse code if imported modules are not used.
Which may not be expected behaviour in some cases.
Take hated Foo class as our sample dependency.
foo.ts
export default class Foo {}
console.log('Foo loaded');
For example:
index.ts
import Foo from './foo'
// prints nothing
index.ts
const Foo = require('./foo').default;
// prints "Foo loaded"
index.ts
(async () => {
const FooPack = await import('./foo');
// prints "Foo loaded"
})();
On the other hand:
index.ts
import Foo from './foo'
typeof Foo; // any use case
// prints "Foo loaded"