Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable webpack sideEffects for css and scss files #3805

Merged
merged 1 commit into from
Jul 27, 2020

Conversation

chandlerprall
Copy link
Contributor

@chandlerprall chandlerprall commented Jul 27, 2020

Summary

Closes #3397

Marks EUI's *.css and *.scss files as having sideEffects when they are imported by webpack. Setup I tested with:

package.json
{
  "name": "test",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "@babel/core": "^7.10.5",
    "@babel/preset-env": "^7.10.4",
    "@babel/preset-react": "^7.10.4",
    "@elastic/datemath": "^5.0.3",
    "@elastic/eui": "^27.2.0",
    "babel-loader": "^8.1.0",
    "css-loader": "^4.0.0",
    "lodash": "^4.17.19",
    "moment": "^2.27.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "style-loader": "^1.2.1",
    "webpack": "^4.44.0",
    "webpack-cli": "^3.3.12",
    "webpack-dev-server": "^3.11.0"
  }
}
webpack.config.js
const path = require('path');

module.exports = {
	mode: 'production',

	entry: path.resolve(__dirname, './src/index.js'),

	output: {
		path: path.resolve(__dirname, 'dist'),
		filename: 'bundle.js',
	},

	module: {
		rules: [
			{
				test: /\.(js|tsx?)$/,
				loader: 'babel-loader',
				options: {
					babelrc: false,
					presets: [
						'@babel/react',
						'@babel/env'
					],
				},
				exclude: /node_modules/,
			},
			{
				test: /\.css$/,
				loaders: [
					'style-loader',
					'css-loader',
				],
			},
		],
	},
};
src/index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { EuiButton } from '@elastic/eui';
import '@elastic/eui/dist/eui_theme_dark.css';

const App = () => (
	<>
		<EuiButton onClick={() => {}}>Click Me!</EuiButton>
	</>
);

ReactDOM.render(
	<App/>,
	document.getElementById('app')
);
dist/index.html
<!DOCTYPE html>
<html>
	<body>
		<div id="app"></div>
		<script type="text/javascript" src="./bundle.js"></script>
	</body>
</html>

First confirmed that the css imports were tree-shaken away, and the page left unstyled. Then I manually applied the change in this PR to node_modules/@elastic/eui/package.json and rebuilt, confirming the expected styles were then present.

This change somewhat makes the assumption that the usual css-loader/style-loader configuration is used, which does follow our consuming.md documentation. If the files are instead processed with a loader like css modules, turning them into import Styles from '...', then there is technically no side effect, as the contents are handled elsewhere - but this change would have no effect as they are already imported. However, this change could in theory bloat applications which are configured to use css modules but have an import '@elastic/eui/dist/some_theme.css'; as that would have previously been [correctly] tree shaken out. My concern for that case is barely measurable 😁

/cc @clintandrewhall

Checklist

- [ ] Check against all themes for compatibility in both light and dark modes
- [ ] Checked in mobile
- [ ] Checked in IE11 and Firefox
- [ ] Props have proper autodocs
- [ ] Added documentation
- [ ] Checked Code Sandbox works for the any docs examples
- [ ] Added or updated jest tests

  • Checked for breaking changes and labeled appropriately
    - [ ] Checked for accessibility including keyboard-only and screenreader modes
  • A changelog entry exists and is marked appropriately

@chandlerprall chandlerprall requested a review from thompsongl July 27, 2020 18:33
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_3805/

1 similar comment
@kibanamachine
Copy link

Preview documentation changes for this PR: https://eui.elastic.co/pr_3805/

Copy link
Contributor

@thompsongl thompsongl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙌

I agree that the potentially negative scenarios cause little real concern.

@chandlerprall chandlerprall merged commit b1dd1f5 into elastic:master Jul 27, 2020
@chandlerprall chandlerprall deleted the bug/3379-sideeffects branch July 27, 2020 21:04
@clintandrewhall
Copy link
Contributor

Bravo!! For anyone curious, this was the case that got caught within Kibana: elastic/kibana#72990

Our Shareable Runtime bundles up EUI with itself, (and I still want to figure out a better approach). But thankfully the bug was caught and fixed before 7.9 was released.

Thanks for the vigilant follow-up, @chandlerprall ...!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reconsider sideEffects designation
4 participants