-
Notifications
You must be signed in to change notification settings - Fork 18
Now longer create a fake AST but let rollup handle it #41
Conversation
src/index.js
Outdated
defaultExportRows.push(`${key}: ${key}`); | ||
namedExportCode += `export ${declarationType} ${key} = ${JSON.stringify(data[key])};\n`; | ||
} else { | ||
defaultExportRows.push(`"${key}": ${JSON.stringify(data[key])}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a lot nicer than what I did in https://github.com/rollup/rollup-pluginutils/pull/29/files actually.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great and nice to have JSON treeshaking!
I may well update https://github.com/rollup/rollup-pluginutils/pull/29/files to merge in this logic and reference the utility function from here when I get back to the plugin stuff.
Unrelated - I'd be interested to know why it's necessary to return an empty map with a mappings key? Was that related to the AST side of things perhaps and we can remove it now? It would be good to understand the use case from a plugin authoring perspective. Completely understand if that is lost to history too... although then perhaps best to just remove it entirely?
@guybedford I have finally updated the plugin to use dataToEsm. Note that the function produces some weird (or at least maybe not 100% consistent) formatting that we may want to improve in rollup-pluginutils, cf. the test samples in the
I must admit I did not investigate further in rollup-pluginutils as to the cause. Thanks a lot for taking a look at the issues there BTW! I wonder which of the two updates you reverted was the original culprit. Does not feel good to have to be careful about updates but then again I probably should not have done any updates on a minor version, at least no major ones. I also updated the dependencies of rollup-plugin-json but this time it is going to be a major version anyway.
This is following the convention described here: https://github.com/rollup/rollup/wiki/Plugins#conventions Basically we signal that a source map does not make sense (because we are too lazy to calculate one...). I guess this is acceptable for pure data exports. |
@lukastaegert thanks for the update and on the source maps stuff. So it turns out this is the way that the third-party package we're using handles whitespace (from the Yaml / Toml plugin I believe). I couldn't actually find any alternatives at all in the ecosystem so ended up rewriting the logic in https://github.com/rollup/rollup-pluginutils/pull/31/files. |
I guess a rewrite is ok here. Of course for the JSON use case, a good old |
anyway, I hope this does not make everyone angry again)
properly with the upcoming version of rollup
This will resolved some issues encountered when implementing rollup/rollup#2167
Previously, rollup-plugin-json went to great lengths to create a "fake AST", presumably to avoid an unnecessary parse. This fake AST, however, would pretend all values (even objects!) were literals with value
null
while only the actual rendering would "fix" the generated code.Now that tree-shaking for conditionals is about to be extended, this obviously causes a lot of issues. The approach taken here is to not generate an AST at all but just do the conversion to JavaScript and let Rollup generate a valid AST itself. In combination with rollup/rollup#2167, this will enable things like