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

docs(bundlers): add webpack 2 example #1497

Merged
merged 6 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions docs/app/Views/Usage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
Button,
Container,
Header,
List,
Message,
Segment,
} from 'src'
import Logo from '../Components/Logo/Logo'
Expand Down Expand Up @@ -173,6 +175,54 @@ const Usage = () => (
icon='github'
labelPosition='left'
/>

<Header as='h3'>Webpack 2</Header>
<p>
Webpack 2 fully supports Semantic UI React, it also supports tree shaking. Please ensure that you build your app
in production mode before release, it will strip <code>propTypes</code> from your build.
</p>

<Message warning>
<p>
Webpack 2 tree shaking does not completely remove unused exports, there are numerous issues that are
long-standing bugs:
</p>
<List>
<List.Item
icon='github'
content={ <a href='https://github.com/webpack/webpack/issues/1750' target='_blank'>webpack/webpack#1750</a>}
/>
<List.Item
icon='github'
content={ <a href='https://github.com/webpack/webpack/issues/2867' target='_blank'>webpack/webpack#2867</a>}
/>
<List.Item
icon='github'
content={<a href='https://github.com/webpack/webpack/issues/2899' target='_blank'>webpack/webpack#2899</a>}
/>
<List.Item
icon='github'
content={<a href='https://github.com/webpack/webpack/issues/3092' target='_blank'>webpack/webpack#3092</a>}
/>
</List>
<p>
Semantic UI React imports will be not optimized, so we recommend to use <code>babel-plugin-lodash</code> in
your builds. You can find example configuration in <code>examples</code> directory.
</p>
</Message>

<Button
content='Example configuration'
href='https://github.com/Semantic-Org/Semantic-UI-React/tree/master/examples/webpack2'
icon='github'
labelPosition='left'
/>
<Button
content='babel-plugin-lodash'
href='https://github.com/lodash/babel-plugin-lodash'
icon='github'
labelPosition='left'
/>
</Segment>
</Container>
)
Expand Down
2 changes: 1 addition & 1 deletion examples/webpack1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"cross-env": "^3.2.3",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"semantic-ui-react": "^0.67.0",
"semantic-ui-react": "^0.68.3",
"webpack": "^1.12.14"
},
"devDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions examples/webpack2/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"presets": [
["es2015", {"modules": false}],
"react",
"stage-1"
],
"plugins": [
["lodash", { "id": ["lodash", "semantic-ui-react"] }]
]
}
1 change: 1 addition & 0 deletions examples/webpack2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
27 changes: 27 additions & 0 deletions examples/webpack2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "semantic-ui-react-example-webpack2",
"version": "1.0.0",
"description": "Get started with Semantic UI React and Webpack 2",
"main": "index.js",
"scripts": {
"build": "webpack",
"build:production": "cross-env NODE_ENV=production npm run build"
},
"author": "Alexander Fedyashov <a@fedyashov.com>",
"license": "MIT",
"dependencies": {
"babel-core": "^6.24.0",
"babel-loader": "^6.4.0",
"babel-preset-es2015": "^6.24.0",
"babel-preset-react": "^6.23.0",
"babel-preset-stage-1": "^6.22.0",
"cross-env": "^3.2.3",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"semantic-ui-react": "^0.68.3",
"webpack": "^2.3.0"
},
"devDependencies": {
"webpack-bundle-analyzer": "^2.3.1"
}
}
13 changes: 13 additions & 0 deletions examples/webpack2/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<title>Semantic UI React Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.css">
</head>
<body>
<div id="root" style="height: 100%"></div>
<script src="./dist/app.js"></script>
</body>
</html>
20 changes: 20 additions & 0 deletions examples/webpack2/src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react'
import { render } from 'react-dom'
import { Button, Container, Header } from 'semantic-ui-react'
Copy link
Member

Choose a reason for hiding this comment

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

This cannot use tree shaking since it is importing commonjs modules. Use the ES build.

#524 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

This example contains bundle analyzer, so you can see same picture as there. It loads build from es dir.

Copy link
Member

Choose a reason for hiding this comment

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

Hm, seems I'll have to put in more due diligence to investigate here. I will do this as soon as I can.

Copy link

Choose a reason for hiding this comment

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

@levithomason any update here?

Copy link
Member

Choose a reason for hiding this comment

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

@pruhstal you may have already noticed, but check the latest comments on the PR. Appears to be a webpack bug.


const MOUNT_NODE = document.getElementById('root')

const App = () => (
<Container>
<Header as='h1'>Hello world!</Header>

<Button
content='Discover docs'
href='http://react.semantic-ui.com'
icon='github'
labelPosition='left'
/>
</Container>
)

render(<App />, MOUNT_NODE)
63 changes: 63 additions & 0 deletions examples/webpack2/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
const path = require('path')
const webpack = require('webpack')

const env = process.env.NODE_ENV || 'development'

const webpackConfig = {
name: 'client',
target: 'web',

entry: {
app: path.resolve('src/main.js'),
},

module: {
rules: [{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader',
}],
},

plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(env),
},
}),
new BundleAnalyzerPlugin(),
],

output: {
filename: '[name].js',
path: path.resolve('public/dist'),
publicPath: '/',
},

resolve: {
modules: [
path.resolve('src'),
'node_modules',
],
extensions: ['.js', '.jsx'],
},
}

if (env === 'production') {
webpackConfig.plugins.push(
new webpack.LoaderOptionsPlugin({
minimize: true,
debug: false,
}),
new webpack.optimize.UglifyJsPlugin({
compress: {
unused: true,
dead_code: true,
warnings: false,
},
})
)
}

module.exports = webpackConfig