create-react-app: comparisons compression option in uglifyjs webpack prod config breaks on react-mapbox-gl
Can you reproduce the problem with latest npm?
Yes (including npm 5).
Description
There seems to be an incompatibility between CRA 1.0+ and the react-mapbox-gl
component. This bug was not present before CRA 1.0, and only shows up in production mode, which makes me think it’s a webpack 2.0 related issue.
Expected behavior
See https://github.com/davidascher/mapbox-repro-bug for a minimal test case (single component render – works in development mode, doesn’t work in production mode)
Actual behavior
In production mode (yarn build
+ serve -s build
) , the map doesn’t show and there’s an incomprehensible traceback (as it’s minified).
Environment
Run these commands in the project folder and fill in their results:
npm ls react-scripts
(if you haven’t ejected): react-scripts@1.0.6node -v
: v7.9.0npm -v
: 5.0.0 (but it happened with 4.x as well)
Then, specify:
- Operating system: OSX
- Browser and version: Chrome 58.0.3029.110
Reproducible Demo
Source: https://github.com/davidascher/mapbox-repro-bug
(trivial CRA repo with one dependency).
Live site showing bug: https://build-wnyrhmqiph.now.sh/
/cc @alex3165 as he is the maintainer of react-mapbox-gl AFAICT.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 19 (12 by maintainers)
Commits related to this issue
- Disable a micro-option in uglify that appears to be buggy See https://github.com/facebookincubator/create-react-app/issues/2376 — committed to davidascher/create-react-app by davidascher 7 years ago
- Disable comparisons feature in uglify compression in production (#2379) * Disable a micro-option in uglify that appears to be buggy See https://github.com/facebookincubator/create-react-app/issues... — committed to facebook/create-react-app by davidascher 7 years ago
- Disable comparisons feature in uglify compression in production (#2379) * Disable a micro-option in uglify that appears to be buggy See https://github.com/facebookincubator/create-react-app/issues... — committed to rrdelaney/reason-scripts by davidascher 7 years ago
- Disable comparisons feature in uglify compression in production (#2379) * Disable a micro-option in uglify that appears to be buggy See https://github.com/facebookincubator/create-react-app/issues... — committed to romaindso/create-react-app by davidascher 7 years ago
- Merge upstream create-react-app v1.0.7 (#90) * Bust AppVeoyr cache * Relax ESLint config peerDependency (#1740) * Fix internal linting setup and add missing headers (#1741) * Fix eject for l... — committed to trunkclub/tcweb-build by iamlacroix 7 years ago
- Disable comparisons feature in uglify compression in production (#2379) * Disable a micro-option in uglify that appears to be buggy See https://github.com/facebookincubator/create-react-app/issues... — committed to wmonk/create-react-app-typescript by davidascher 7 years ago
- Disable comparisons feature in uglify compression in production (#2379) * Disable a micro-option in uglify that appears to be buggy See https://github.com/facebookincubator/create-react-app/issues... — committed to rrdelaney/reason-scripts by davidascher 7 years ago
- Disable comparisons feature in uglify compression in production (#2379) * Disable a micro-option in uglify that appears to be buggy See https://github.com/facebookincubator/create-react-app/issues... — committed to Azure/iot-ux-baseline by davidascher 7 years ago
It’s a confluence of factors:
global
usingtypeof global !== "undefined"
. At the point whereglobal
is used, there’s no outer context in which a variableglobal
is defined.function (global) { ... }
.global
and transform the resultingtypeof g !== "undefined"
tovoid 0 !== g
. Together, this produces broken code when the stringified function is executed outside of thefunction (g) { ... }
wrapper.Because of this confluence, it’s likely to be one of those situations where the maintainers of each of the individual components involved insist that what their code does is perfectly valid, and it’s the other stuff that’s making invalid assumptions and must be fixed. At the risk of doing so myself, I would say the bug is in webpack: it provides no mechanism for a package to indicate what its build/packaging/bundling requirements are. If we, the authors of mapbox-gl-js, could put something in package.json indicating “don’t mess with this code, it’s already been bundled and minified in the way that it needs to be; redundant wrapping, bundling, and mangling is likely to break it”, then issues like this wouldn’t keep cropping up.
mapbox-gl
fixed their webpacktypeof global
issue in https://github.com/mapbox/mapbox-gl-js/pull/6956.The workaround in https://github.com/facebook/create-react-app/pull/2379/files is no longer needed and can be reverted.
Hey guys; I’m a little confused: this bug (and the mapbox-gl linked bug) are both “closed” but the problem still persists, right? And the disable micro-option doesn’t seem to have been pulled in (yet? is the plan to pull that in for now?)…
Just trying to understand how to workaround this issue / when/what the real fix is (going to be)
I’m not a user of
mapbox-gl-js
, but here’s a couple of possible workarounds to avoid the use ofglobal
…These are the contentious
global
s in questionbrowserify
’d bymapbox-gl-js
:https://github.com/mapbox/pbf/blob/v1.3.7/index.js#L5
That can be solved by upgrading
pbf
to the latest version which does not useglobal
.And
util.deprecate
which no one cares about in the context ofmapbox-gl-js
:https://github.com/defunctzombie/node-util/blob/b5d10a26402c3b60ce496e675a13c58bbed47801/util.js#L67
That can be solved by making a fork of
util@0.10.3
to remove that function.Perhaps
util.deprecate
can be retained but simply remove this code:https://github.com/defunctzombie/node-util/blob/b5d10a26402c3b60ce496e675a13c58bbed47801/util.js#L66-L71
Alternatively, if
browserify
(or is itwebpack
?) can be modified to change the order of conditions in the following generated code to check forwindow
first andglobal
last it may also work:I don’t know where that code is.
With @kzc’s help, this has been traced it down to an uglify option (see https://github.com/mishoo/UglifyJS2/issues/2011#issuecomment-304405775).
Setting
in the webpack prod config for the UglifyJS plugin fixes the problem.
I’ll see if I can figure out how to override the webpack configs, I vaguely remember a bug going by about that new capability.