Skip to content

Instantly share code, notes, and snippets.

@alex996
Last active September 5, 2018 04:14
Show Gist options
  • Save alex996/444614a8cabacb6ec637f263abb57795 to your computer and use it in GitHub Desktop.
Save alex996/444614a8cabacb6ec637f263abb57795 to your computer and use it in GitHub Desktop.

Test results

Problem: When --experimental-scope-hoisting is used for tree shaking, the bundle builds, but the app fails with a runtime error when being served from index.html.

// index.js
import React from 'react'
import { render } from 'react-dom'
// import [variations of Typograpy import here]

render(
  <Typography>Hello</Typography>,
  document.getElementById('app')
)
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>App</title>
</head>
<body>
  <div id="app"></div>
  <script src="index.js"></script>
</body>
</html>
"scripts": {
  "start": "parcel index.html",
  "build": "rm -rf dist && parcel build index.html",
  "build:shake": "rm -rf dist && parcel build --experimental-scope-hoisting index.html",
  "serve:dist": "serve dist"
},
"dependencies": {
  "@material-ui/core": "^3.0.2",
  "@material-ui/icons": "^3.0.1",
  "react": "^16.4.2",
  "react-dom": "^16.4.2"
},
"devDependencies": {
  "babel-preset-env": "^1.7.0",
  "babel-preset-react": "^6.24.1",
  "parcel-bundler": "^1.9.7",
  "serve": "^10.0.0"
}

@material-ui/core (CommonJS)

no shaking

import Typography from '@material-ui/core/Typography'
197512 B
import { Typography } from '@material-ui/core'
657321 B

shaking

import Typography from '@material-ui/core/Typography'
153188 B "ReferenceError: React is not defined"
import { Typography } from '@material-ui/core'
199294 B "ReferenceError: React is not defined"

@material-ui/core/es (ES Modules)

no shaking

import Typography from '@material-ui/core/es/Typography'
193886 B
import { Typography } from '@material-ui/core/es'
648934 B

shaking

import Typography from '@material-ui/core/es/Typography'
174464 B  "ReferenceError: React is not defined"
import { Typography } from '@material-ui/core/es'
220577 B  "ReferenceError: React is not defined"

@material-ui/icons (CommonJS only)

Found similar results with @material-ui/icons using

import AccessAlarmIcon from '@material-ui/icons/AccessAlarm'

no shaking

200726 B

shaking

154712 B "TypeError: (0 , Oj.default) is not a function"

It freezes up when using a named import

import { AccessAlarm } from '@material-ui/icons'

P.S. Couldn't test with ESM, as the library no longer provides them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment