UP | HOME

import() function

Table of Contents

1 TODO Add up more details for this page

2 Overview

Webpack v1 transpiled import() into require.ensure().

Since import in ES6 is static (the module resolved at compile time, not runtime), import() is resolved at runtime and returned a promise. So, we can use it like:

import('module').then(module => ...);

Or even import multiple modules:

const modules = ['module1', 'module2', 'module3'].map(module => import(module));
Promise.all(modules).then([module1, module2, module3] => { ... });

More read:

3 import() vs require()

require is CommonJS, import() is ES6 module.

Question: Why we can use both require() and import() in Webpack?

Date: 2019-01-02 Wed 00:00

Author: Huy Tran

Created: 2019-04-22 Mon 14:41