From other side, webpack bundler has a feature called which allows you to split your bundle into chunks which can be downloaded asynchronously at a later time. For instance, this allows to serve a minimal bootstrap bundle first and to asynchronously load additional features later.

    At first glance, it’s natural to think (if we are using webpack in our dev workflow) that by using TypeScript 2.4 dynamic import expressions, will automatically produce bundle chunks and automatically code-split you JS final bundle. BUT, that is not as easy as it seems, because it depends on the tsconfig.json configuration we are working with.

    Let’s see an example to figure out how to configure webpack + TypeScript 2.4.

    In the following code I want to lazy load the library moment but I am interested on code splitting as well, which means, having moment library in a separate chunk of JS (javascript file) and that will be loaded only when required.

    1. "compilerOptions": {
    2. "target": "es5",
    3. "module": "esnext",
    4. "lib": [
    5. "dom",
    6. "es5",
    7. "scripthost",
    8. ],
    9. "jsx": "react",
    10. "declaration": false,
    11. "sourceMap": true,
    12. "outDir": "./dist/js",
    13. "strict": true,
    14. "moduleResolution": "node",
    15. ],
    16. "types": [
    17. "node",
    18. "react",
    19. "react-dom"
    20. ]
    21. }

    Important notes:

    • For further information read this article: .

    You can see full example here.