Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Rework rollup config to output named exports (NODE-2712) #404

Merged
merged 3 commits into from
Sep 30, 2020

Conversation

nbbeeken
Copy link
Contributor

Rollup's output was bundling the library to have one single default export this change makes the output preserve the named outputs as well as have the default export. In addition the rollup config now compiles from typescript source instead of the typescript output.

NODE-2712

Rollup's output was bundling the library to have one single default export this change makes the output preserve the named outputs. In addition the rollup config now compiles from typescript instead of from the typescript output.
src/bson.ts Outdated
EJSON
};

export default bsonDefaultModuleExport;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this looks pretty funky. Are we mixing named and default exports? It looks like you specified named in the rollup config, do you still need this here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because our original bundles had both named and default exports so we need to keep the behavior.

@@ -60,7 +60,7 @@ export class ObjectId {
this.__id = id.__id;
}

if (typeof id === 'object' && 'id' in id) {
if (typeof id === 'object' && id && 'id' in id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this change needed? the typeof check should prove the existence of the object (it's not typeof === undefined)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typeof null is 'object', so this is checking for that

rollup.config.js Outdated
file: 'dist/bson.bundle.js',
format: 'iife',
name: 'BSON'
format: 'esm',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
format: 'esm',
format: 'iife',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The format here is correct, babel is downgrading the code after rollup has done its transformations so babel is handling the bundle format. I use UMD in the babel preset because it is equivalent to an iife (babel doesn't have an iife output without a plugin, which seems unnecessary).

rollup.config.js Outdated
/* Browser UMD bundle */
{
file: 'dist/bson.browser.umd.js',
format: 'umd',
name: 'BSON'
format: 'esm',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
format: 'esm',
format: 'umd',

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as comment above. This is correct for what we're doing here.

rollup.config.js Outdated

export default [
{
input,
/* Browser ESM bundle */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain what's going on in this change? Also, it looks like the config is mostly duplicated below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea the key difference is the babel settings. There is definitely a lot of duplication but after wrestling with the config for a better part of this project an explicit set of settings for each output is actually quite helpful rather than debugging a programatic config.

rollup.config.js Outdated
plugins: [nodeResolve({ preferBuiltins: true }), commonjs()]
plugins: [
typescript({ tsconfig: './tsconfig.rollup.json' }),
nodeResolve({ preferBuiltins: true }),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed the comment above saying "note preferBuiltins false" which not actually the case here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yea out of date comment, good catch.

@@ -0,0 +1,32 @@
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to embed this config directly in the rollup.config.js? I feel like otherwise it's just another file added to the heap of tooling files laying around. Is there a benefit to having this in its own file that I'm missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep sounds good!

package.json Show resolved Hide resolved
Copy link
Contributor

@reggi reggi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, babel plugins to the rescue?

Copy link
Member

@mbroadst mbroadst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@nbbeeken nbbeeken merged commit a48676b into master Sep 30, 2020
@nbbeeken nbbeeken deleted the chore/fix-bundle branch September 30, 2020 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants