Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

dataToNamedExports helper #29

Merged
merged 7 commits into from
May 7, 2018
Merged

dataToNamedExports helper #29

merged 7 commits into from
May 7, 2018

Conversation

guybedford
Copy link
Contributor

This is a helper I've found is needed by all the data plugins (eg yaml, json, toml) in order to output treeshakable data.

As far as I can tell the JSON plugin does currently support treeshaking in this way, but for example the YAML plugin doesn't seem to. Getting the logic on a shared helper like this seems like it would help ensure consistency here.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

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

Looks good except for the name as you are obviously creating a default export as well (that corresponds to the named exports).
Maybe something like getExportsFromObject or getExportsFromProperties?

@guybedford
Copy link
Contributor Author

Thanks for review on this - I've renamed to dataToEsm, let me know if that sounds better?

@lukastaegert
Copy link
Member

Looks good. If you want this to be released, the "compact" option should probably added to the readme. If this is still work in progress, just let me know. If we expect more than one option to be added, the options should probably become an options object.

@guybedford
Copy link
Contributor Author

I've gone ahead and updated the implementation here to match rollup/rollup-plugin-json#41, so if you want you could update that PR to reference this utility now.

I also caught a minor bug in the process that non legal keys could contain quote characters, so they now run through JSON.stringify themselves.

I think it's nice to maintain the implementation in a single place here for all the data format plugins, so would like to get this merged, even if the JSON plugin itself doesn't use this.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

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

Much nicer and I agree, definitely makes sense to update the implementation in rollup-plugin-json. Will see to it once this is released.

compact: false,
indent: '\t',
preferConst: false,
objectShorthand: false
Copy link
Member

Choose a reason for hiding this comment

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

👍

src/dataToEsm.js Outdated

let namedExportCode = '';
const defaultExportRows = [];
Object.keys( data ).forEach(key => {
Copy link
Member

Choose a reason for hiding this comment

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

Just a minor remark: Even though we do not need the keys variable any more, it might still make sense to use the more performant previous version of the loop instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's a bit of an issue with that as Buble doesn't support transforming for-of. I think we're ok when not in tight loops for this sort of optimization though.

Copy link
Member

Choose a reason for hiding this comment

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

I was actually suggesting to use the exact code as used in the initial version—a classic for loop with an iterator variable. It's still the fastest way to iterate. for...of loops in TypeScript are only fast because due to our compilation target, it is transpiled to a classic for loop. Real for...of loops are actually quite a bit slower due to the hidden iterator evaluations anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I completely forgot I did that. Sure.

test/test.js Outdated
});

it( 'supports a compact argument', function () {
assert.equal( dataToEsm( { some: 'data', another: 'data' }, true ), 'export const some="data";export const another="data";export default{some,another};' );
assert.equal( dataToEsm( { some: { deep: { object: 'definition', here: 'here' } }, another: 'data' }, true ), 'export const some={deep:{object:"definition",here:"here"}};export const another="data";export default{some,another};' );
assert.equal( dataToEsm( { some: 'data', another: 'data' }, { compact: true, objectShorthand: true } ), 'export var some="data";export var another="data";export default{some,another};' );
Copy link
Member

Choose a reason for hiding this comment

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

I would love to see the output of { compact: true, objectShorthand: false } tested as well as I think this is an important case considering backward compatibility.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure I've included this test case.

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

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

👍

@lukastaegert lukastaegert merged commit 5f40964 into master May 7, 2018
@lukastaegert lukastaegert deleted the dataToNamedExports branch May 7, 2018 05:27
@lukastaegert
Copy link
Member

Apparently, npm won't let me publish even though it pretends I am an owner of this package. Opened a ticket: npm/npm#20529

@guybedford
Copy link
Contributor Author

@lukastaegert that looks more like a registry bug than an npm one, it may be worth copying it over to the registry repo rather - https://github.com/npm/registry and closing the npm issue. May well get a better response.

@guybedford
Copy link
Contributor Author

Or maybe not - https://github.com/npm/registry/issues/312 :)

@guybedford
Copy link
Contributor Author

Otherwise it may just be worth asking @Rich-Harris to do a publish here if the permissions aren't working for you.

@Rich-Harris
Copy link
Contributor

I've published a new version, added @guybedford as an owner, and removed-then-added @lukastaegert, so hopefully whatever gremlins were affecting npm are no longer an issue

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants