FormidableLabs / inspectpack

An inspection tool for Webpack frontend JavaScript bundles.
MIT License
592 stars 20 forks source link

Replace bundle string handling with parse + traverse #24

Closed tptee closed 7 years ago

tptee commented 7 years ago

I ran into this when integrating with webpack-dashboard: https://github.com/webpack/webpack/blob/93ac8e9c3699bf704068eaccaceec57b3dd45a14/lib/Template.js#L118

Turns out that Webpack bundles can contain either an array of modules or an object of id keys/module values. The object version seems rare, but I caught one in a simple lodash + entrypoint bundle, and inspectpack can't currently understand it.

This PR parses the bundles with babylon, traverses the AST, and creates Code objects from AST data. It parses both the array and object forms of bundles. Most string manipulation is gone, save for a bit of trickery in extracting data from module header comments. I figure this approach will make inspectpack more resistant to changes in the Webpack bundle format in the future.

I also modified the Code API a little, since we know more about the type of Code objects a priori during traversal. I've updated its usage in models/bundle.js and actions/sizes.js, but need to update the remainder of the action files.

@ryan-roemer Do you have example bundles that trigger non-empty cases for duplicates/files/parse/pattern/versions? I added some super-basic smoke tests for each action, but they won't be meaningful unless they're run against a bundle with positive results.

tptee commented 7 years ago

Some rough edges left, but probably worth an initial pass!

ryan-roemer commented 7 years ago

Looking great so far! Let me know when this hits complete and what specific types of examples you need for the smoke tests.

tptee commented 7 years ago

I've neutered the versions smoke test for now, since doing it right requires some nasty postinstalling or heavy mocking 😬

tptee commented 7 years ago

Should be ready for a final peek!

ryan-roemer commented 7 years ago

@tptee -- The fixtures are huge and I'd rather they not be part of this git repo. Can we move them to a separate published packaged? Like inspectpack-test-fixtures or something?

tptee commented 7 years ago

Done!