question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rollup config error on dynamic import during library build

See original GitHub issue

Type of Issue

[x ] Bug Report
[ ] Feature Request

Description

This bug relates to building angular library which uses dynamic imports. Consider building foo library with the command ng build foo. When the library contains a file (service in my case), which uses dynamic import, the build will fail throwing error below.

Error: You must set "output.dir" instead of "output.file" when generating multiple chunks.
    at error (c:\Development\rollup-test\node_modules\rollup\dist\rollup.js:3410:30)
    at normalizeOutputOptions (c:\Development\rollup-test\node_modules\rollup\dist\rollup.js:17107:13)
    at getOutputOptions (c:\Development\rollup-test\node_modules\rollup\dist\rollup.js:16865:24)
    at Object.write (c:\Development\rollup-test\node_modules\rollup\dist\rollup.js:16957:43)
    at Object. (c:\Development\rollup-test\node_modules\ng-packagr\lib\flatten\rollup.js:46:22)
    at Generator.next ()
    at fulfilled (c:\Development\rollup-test\node_modules\ng-packagr\lib\flatten\rollup.js:4:58)

How To Reproduce

See details about how to reproduce at this post: https://github.com/angular/angular-cli/issues/14444#issue-444906345

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:38
  • Comments:51 (3 by maintainers)

github_iconTop GitHub Comments

14reactions
raphael22commented, Sep 5, 2019

I agree with @blemaire. In a plugin context you want the library to be autonomous. Even more when you deal with non routed lazy-loaded component.

If you take my exemple in earlier post, why a plugin button which call a modal could not take the responsability of it ? THIS look like a bad pattern to me.

10reactions
kingfolkcommented, Jul 25, 2019

The issue may come from https://github.com/ng-packagr/ng-packagr/blob/master/src/lib/flatten/rollup.ts#L69 when there is dynamic import, rollup will try to use output.dir instead of output.file to build multiple bundles.

For me, the library I am trying to build is not entirely depends on Angular. So I just use typescript-library-starte and override the https://github.com/alexjoverm/typescript-library-starter/blob/master/rollup.config.ts#L15 to output.dir then will have the following issue https://github.com/rollup/rollup/issues/2072 which just says umd bundle format is not compatible for dynamic import. As I googled what that means, one of github issue says IIFE and UMD formats are (purposefully) self-contained https://github.com/lukeed/navaid/issues/5#issuecomment-488540276

It seems a lot of effort to make umd support dynamic import. For now I guess the best way right now is to support rollup build multiple bundles and add a ng-packagr option to disable umd build.

Read more comments on GitHub >

github_iconTop Results From Across the Web

rollup.js
Import modules using the dynamic import API. import('./modules.js').then(({ default: DefaultExport, NamedExport }) => { // do something with modules. });.
Read more >
@rollup/plugin-dynamic-import-vars - npm
A rollup plugin to support variables in dynamic imports in Rollup. ... Create a rollup.config.js configuration file and import the plugin:.
Read more >
How to dynamically import a module which name is defined by ...
Unfortunetely i found that rollup doesn't support variables in dynamic imports for now. Please see these issues:
Read more >
Features | Vite
Vite will detect such bare module imports in all served source files and perform ... via dynamic import and will be split into...
Read more >
Code Splitting - webpack
Dynamic Imports : Split code via inline function calls within modules. ... webpack-demo |- package.json |- package-lock.json |- webpack.config.js |- /dist ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found