fix: only require parsers if need be#9002
Conversation
this also allows to remove them from bundle. However this is not the best way. We should use global vars for cssParser so that weback automatically removes the code
| if (this._source) { | ||
| switch (parser) { | ||
| case 'css-tree': | ||
| const cssTreeParse = require('../../css/css-tree-parser').cssTreeParse; |
There was a problem hiding this comment.
I'd wonder too if this would be subject to sideEffects when tree shaking is utilized? - perhaps the global.cssParser option in webpack configs may be a nice way to remove these blocks for those not used.
There was a problem hiding this comment.
It can't webpack has no way of knowing we use one or the other. So it will add the 3. That s why I talked about global var for cssParder so that webpack can remove what we don't use
There was a problem hiding this comment.
Instead of adding yet another global, we can add "magic" strings
In this case, I would do
switch(__CSS_PARSER__) {and just make ts happy with
declare readonly var __CSS_PARSER__: stringin global-types.d.ts
We would inject the variable with the DefinePlugin in webpack.
There was a problem hiding this comment.
I dont think it would work Igor. Dont know if the tree shaker will clean the switch . I think we have to something like if (__CSS_PARSER__ === 'rework')...
There was a problem hiding this comment.
Yep, we can convert to if/else if's
This rollup repl shows it working!
I expect it to work the same through webpack as well!
There was a problem hiding this comment.
Various React ecosystem packages use if(__DEV__){} blocks explicitly because Webpack does treeshake paths based on the value of the constant defined by WebpackDefinePlugin, if it’s any help.
Co-authored-by: Igor Randjelovic <rigor789@gmail.com>
|
@farfromrefug I believe this is obsolete since we merged this dce7408 in with the release branch, is that correct? |
|
Yes it is ! |
this also allows to remove them from bundle.
However this is not the best way. We should use something like
global.cssParserso that weback automatically removes the code