fix(build): publish Node (server) friendly bundles #3483
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #3279.
@ionic-native
libraries currently break server-side rendering as Node does not natively handle ES modules yet (well it kinda does since v13 but the current LTS is still v12 and they are many limitations, e.g. only the default CommonJS export can be imported from an ES module usingimport
).For that reason libraries that are published as ES modules (which is great as it provides better tree-shaking) should also provide legacy UMD or CommonJS bundles for Node to be able to natively execute the code on the server, as explained in the Angular Package Format specification, maintained by the Angular team.
Note that most of the time such code never gets executed on the server, as most
@ionic-native
functionality doesn't make sense in that environment, but the only fact that it uses the ES modules syntax causes the JS parser to throw aSyntaxError: Unexpected token import
when reading those files.As an aside starting with Angular CLI v9,
bundleDependencies
defaults totrue
for server-side builds which will cause the app dependencies to go through Webpack, which will transpile ES imports into CommonJS ones, effectively making@ionic-native
packages work on the server. HoweverbundleDependencies
still has many limitations such as poor i18n support (angular/angular-cli#25726), and the problem remains when not using the Angular CLI for instance.This PR fixes the issue by simply using Rollup to downgrade the previously built ES modules into
bundle.js
CommonJS bundles, and set those as the packagesmain
entry points (used byrequire()
on the server). The packagesmodule
entry points (used by client-side module bundlers, e.g. Webpack) remains unchanged.