Skip to content

Commit c553a68

Browse files
committed
chore: add webpack 1 example
1 parent 316653a commit c553a68

File tree

10 files changed

+773
-989
lines changed

10 files changed

+773
-989
lines changed

.babelrc

Lines changed: 0 additions & 5 deletions
This file was deleted.

.flowconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,5 @@ esproposal.export_star_as=enable
1111
[libs]
1212
./node_modules/fbjs/flow/lib
1313
./flow/decls
14+
[version]
15+
^0.47.0

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
Example project demonstrating the use of [React Flag Icon Css](//github.com/matteocng/react-flag-icon-css) with [React Css Modules](//github.com/gajus/react-css-modules), standard Css and custom flags. Powered by [Webpack 2](//github.com/webpack/webpack).
1+
Example project demonstrating the use of [React Flag Icon Css](//github.com/matteocng/react-flag-icon-css) with [React Css Modules](//github.com/gajus/react-css-modules) or standard (global) Css, and custom flags. Powered by [webpack 1](//github.com/webpack/webpack).
2+
3+
### This is the `webpack` **1** example, [click here](https://github.com/matteocng/react-flag-icon-css-example-multi) if you are looking for the `webpack` **2** example.
4+
5+
## Deprecation warning
6+
7+
**webpack 1 is deprecated**, this branch is an example for existing apps that can't or won't be updated to `webpack 2`. We recommend upgrading to `webpack 2` ([official migration guide](https://webpack.js.org/guides/migrating/)) and then looking at the `react-flag-icon-css` [webpack 2 example](https://github.com/matteocng/react-flag-icon-css-example-multi) in the master branch.
28

39
## Installation
410

@@ -7,7 +13,8 @@ It is recommended to use [Yarn](https://yarnpkg.com/) instead of npm for managin
713
Clone the repository:
814

915
```bash
10-
$ git clone https://github.com/matteocng/react-flag-icon-css-example-multi.git && cd react-flag-icon-css-example-multi
16+
$ git clone -b webpack-1 https://github.com/matteocng/react-flag-icon-css-example-multi.git
17+
$ cd react-flag-icon-css-example-multi
1118

1219
```
1320

@@ -38,6 +45,15 @@ This example project uses the <code>USE_CSS_MODULES</code> environment variable
3845

3946
In a real project you would probably choose to either go with [React Css Modules](//github.com/gajus/react-css-modules) or standard Css, removing the <code>USE_CSS_MODULES</code> switch and <code>getStylePropName</code>, replacing <code>[options.stylePropName]</code> in the React components with either <code>className</code> (standard Css) or <code>styleName</code> (modules).
4047

48+
## Styles
49+
50+
This example app uses `sass` (`.scss`) styles, but `sass` is not required to use `react-flag-icon-css`. Your app can use any CSS preprocessor (such as [`postcss`](https://github.com/postcss/postcss)) or no CSS preprocessor at all. For instance, to use [`postcss`](https://github.com/postcss/postcss) in your stylesheets:
51+
- rename `.scss` files to `.css`.
52+
- remove *`sass-loader`* from [webpack.config.js](webpack/webpack.config.js).
53+
- `npm install` or `yarn add` the `PostCSS` plugins that you need.
54+
- `import()` them into [webpack.config.js](webpack/webpack.config.js)
55+
- add them to the `postcss` section after `autoprefixer`.
56+
4157
## Contributing
4258

4359
Contributions are welcome. Please use a topic branch, follow the [AngularJS commit style guidelines](//github.com/angular/angular.js/blob/master/CONTRIBUTING.md#-git-commit-guidelines) and check that <code>yarn run pre-commit</code> returns zero errors before opening a PR. Thanks!

package.json

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222
"url": "git://github.com/matteocng/react-flag-icon-css-example-multi.git"
2323
},
2424
"scripts": {
25-
"start": "USE_CSS_MODULES=true babel-node webpack/server.js",
26-
"start:classes": "USE_CSS_MODULES=false babel-node webpack/server.js",
25+
"start": "cross-env USE_CSS_MODULES=true NODE_ENV=development babel-node webpack/server.js",
26+
"start:classes": "cross-env USE_CSS_MODULES=false NODE_ENV=development babel-node webpack/server.js",
2727
"flow": "flow; test $? -eq 0 -o $? -eq 2",
2828
"lint": "npm run lint:js && npm run lint:css",
29-
"lint:js": "eslint src cmrh.conf.js",
29+
"lint:js": "eslint src webpack cmrh.conf.js",
3030
"lint:css": "sass-lint 'src/**/*.scss' -v -q",
31-
"lint:fix": "eslint src --fix",
31+
"lint:fix": "eslint src webpack --fix",
3232
"pre-commit": "npm run lint && npm run flow"
3333
},
3434
"pre-commit": [
@@ -38,41 +38,46 @@
3838
],
3939
"dependencies": {
4040
"node-sass": "^4.5.3",
41+
"prop-types": "^15.5.10",
4142
"react": "^15.5.4",
4243
"react-css-modules": "^4.0.5",
4344
"react-dom": "^15.5.4",
44-
"react-flag-icon-css": "^1.0.16",
45-
"webpack": "^2.5.1",
46-
"webpack-dev-server": "^2.4.5"
45+
"react-flag-icon-css": "^1.0.18",
46+
"webpack": "^1.15.0",
47+
"webpack-dev-server": "^1.16.5"
4748
},
4849
"devDependencies": {
49-
"babel-cli": "^6.18.0",
50-
"babel-core": "^6.21.0",
50+
"autoprefixer": "^7.1.1",
51+
"babel-cli": "^6.24.1",
52+
"babel-core": "^6.24.1",
5153
"babel-eslint": "^7.1.1",
5254
"babel-loader": "^6.2.10",
53-
"babel-plugin-transform-object-rest-spread": "^6.20.2",
54-
"babel-preset-es2015": "^6.24.1",
55-
"babel-preset-react": "^6.24.1",
56-
"babel-register": "^6.24.1",
57-
"css-loader": "^0.28.1",
55+
"babel-preset-react-app": "^2.2.0",
56+
"babel-runtime": "^6.23.0",
57+
"cross-env": "^5.0.0",
58+
"css-loader": "^0.28.3",
5859
"eslint": "^3.19.0",
5960
"eslint-config-airbnb": "^13.0.0",
6061
"eslint-plugin-babel": "^4.0.0",
6162
"eslint-plugin-flowtype": "^2.29.1",
6263
"eslint-plugin-import": "^2.2.0",
6364
"eslint-plugin-jsx-a11y": "^2.2.3",
6465
"eslint-plugin-react": "^6.8.0",
65-
"extract-text-webpack-plugin": "^2.1.0",
66+
"extract-text-webpack-plugin": "^1.0.1",
6667
"file-loader": "^0.9.0",
67-
"flow-bin": "^0.46.0",
68-
"html-webpack-plugin": "^2.28.0",
68+
"flow-bin": "^0.47.0",
69+
"html-webpack-plugin": "^1.7.0",
6970
"json-loader": "^0.5.4",
70-
"postcss-loader": "^2.0.5",
71+
"postcss-loader": "^1.3.3",
7172
"pre-commit": "^1.2.2",
7273
"sass-lint": "^1.10.2",
73-
"sass-loader": "^6.0.5",
74-
"style-loader": "^0.13.1",
75-
"webpack": "^2.5.1",
76-
"webpack-dev-server": "^2.4.5"
74+
"sass-loader": "^4.1.1",
75+
"style-loader": "^0.18.1",
76+
"webpack-dev-server": "^1.16.5"
77+
},
78+
"babel": {
79+
"presets": [
80+
"react-app"
81+
]
7782
}
7883
}

src/.babelrc

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/js/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { AppPropsType } from '../types/flow'
77

88
const appProps: AppPropsType = {
99
headingText: 'React Flag Icon Css',
10-
subHeadingText: `Webpack Example ${__USE_CSS_MODULES__ ? 'using CSS Modules' : 'with global CSS'}`
10+
subHeadingText: `Webpack 1 Example ${__USE_CSS_MODULES__ ? 'using CSS Modules' : 'with global CSS'}`
1111
}
1212

1313
const rootEL = document.querySelector('#app')

webpack/server.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import WebpackDevServer from 'webpack-dev-server'
22
import webpack from 'webpack'
3-
import path from 'path'
3+
import webpackConfig, { outputPath } from './webpack.config'
44

5-
import configMaker, { outputPath } from './webpack.config.js'
65

6+
const configMaker = webpackConfig
77
const config = configMaker()
8-
var compiler = webpack(config)
9-
8+
const compiler = webpack(config)
109

1110

1211
const server = new WebpackDevServer(compiler, {

webpack/templates/index.ejs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
<meta charset="utf-8">
55
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
66
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
<title><%= htmlWebpackPlugin.options.title %></title>
7+
<title>{%= o.htmlWebpackPlugin.options.title %}</title>
88
</head>
99
<body>
10-
<a href="<%= htmlWebpackPlugin.options.githubRepositoryUrl %>"><img style="position: absolute; top: 0; right: 0; border: 0; width: 70px;" src="https://camo.githubusercontent.com/e7bbb0521b397edbd5fe43e7f760759336b5e05f/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png"></a>
10+
<a href="{%= o.htmlWebpackPlugin.options.githubRepositoryUrl %}">
11+
<img style="position: absolute; top: 0; right: 0; border: 0; width: 70px;" src="https://camo.githubusercontent.com/e7bbb0521b397edbd5fe43e7f760759336b5e05f/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_green_007200.png" />
12+
</a>
1113
<div id="app"></div>
1214
</body>
1315
</html>

webpack/webpack.config.js

Lines changed: 55 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,56 @@
11
import path from 'path'
22
import webpack from 'webpack'
3-
import HtmlWebpackPlugin from 'html-webpack-plugin'
4-
import ExtractTextPlugin from 'extract-text-webpack-plugin'
5-
import autoprefixer from 'autoprefixer'
6-
3+
import HtmlWebpackPlugin from 'html-webpack-plugin' // eslint-disable-line import/no-extraneous-dependencies
4+
import ExtractTextPlugin from 'extract-text-webpack-plugin' // eslint-disable-line import/no-extraneous-dependencies
5+
import autoprefixer from 'autoprefixer' // eslint-disable-line import/no-extraneous-dependencies
76

87
const port = 8080
98
const JS_REGEX = /\.js$|\.jsx$|\.es6$|\.babel$/
109
const CSS_REGEX = /\.css$|\.scss$/
1110
const context = path.join(__dirname, '../src/js')
1211
export const outputPath = 'www'
1312

13+
const makeExtractTextLoader = ({ useCssModules }: { useCssModules: boolean }): string =>
14+
ExtractTextPlugin.extract(
15+
'style-loader!',
16+
[`css-loader?importLoaders=1${useCssModules ? '&modules=2&localIdentName=[name]__[local]__[hash:base64:5]' : ''}`,
17+
'postcss-loader',
18+
'sass-loader'
19+
].join('!')
20+
)
1421

15-
const getRules = options => {
16-
const useCssModules = JSON.parse(process.env.USE_CSS_MODULES)
17-
18-
return [
19-
{
20-
test: JS_REGEX,
21-
loaders: ['babel-loader'],
22-
exclude: /node_modules/
23-
},
24-
{
25-
test: CSS_REGEX,
26-
loader: ExtractTextPlugin.extract({
27-
fallback: "style-loader",
28-
use: [
29-
{ loader: 'css-loader', query: {
30-
modules: useCssModules,
31-
importLoaders: useCssModules ? 2 : '',
32-
localIdentName: useCssModules ? '[name]__[local]__[hash:base64:5]' : ''
33-
} },
34-
{ loader: 'postcss-loader', options: {
35-
plugins: [
36-
autoprefixer({ browsers: ['last 4 versions'] }),
37-
],
38-
} },
39-
{ loader: 'sass-loader', options: {} }
40-
]
41-
}),
42-
},
43-
{
44-
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
45-
loader: "file-loader"
46-
},
47-
{
48-
test: /\.json$/,
49-
loader: "json-loader"
22+
const getLoaders = (options: {}): {}[] => [
23+
{
24+
test: JS_REGEX,
25+
exclude: /node_modules/,
26+
loader: 'babel',
27+
query: {
28+
// This is a feature of `babel-loader` for webpack (not Babel itself).
29+
// It enables caching results in ./node_modules/.cache/babel-loader/
30+
// directory for faster rebuilds.
31+
cacheDirectory: true
5032
}
51-
]
52-
}
33+
},
34+
{
35+
test: CSS_REGEX,
36+
loader: makeExtractTextLoader(options),
37+
},
38+
{
39+
test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
40+
loader: 'file-loader'
41+
},
42+
{
43+
test: /\.json$/,
44+
loader: 'json-loader'
45+
},
46+
]
5347

48+
const config = (options: {}): {} => {
49+
const useCssModules = JSON.parse(process.env.USE_CSS_MODULES)
50+
const computedOptions = { useCssModules, ...options }
5451

55-
const config = options => {
5652
return {
57-
context: context,
53+
context,
5854
entry: [
5955
`webpack-dev-server/client?http://0.0.0.0:${port}/`,
6056
path.join(context, 'index.js'),
@@ -64,11 +60,22 @@ const config = options => {
6460
filename: 'bundle.js',
6561
publicPath: '/',
6662
},
63+
// Some libraries import Node modules but don't use them in the browser.
64+
// Tell Webpack to provide empty mocks for them so importing them works.
65+
node: {
66+
fs: 'empty',
67+
net: 'empty',
68+
tls: 'empty'
69+
},
70+
module: {
71+
loaders: getLoaders(computedOptions),
72+
},
6773
plugins: [
6874
new webpack.HotModuleReplacementPlugin(),
6975
new webpack.NamedModulesPlugin(),
7076
new HtmlWebpackPlugin({
71-
title: 'React Flag Icon Css - Webpack Example Multi',
77+
inject: 'body',
78+
title: 'React Flag Icon Css - Webpack 1 Example Multi',
7279
devServerScript: `http://0.0.0.0:${port}/webpack-dev-server.js`,
7380
template: path.join(__dirname, 'templates/index.ejs'),
7481
githubRepositoryUrl: 'https://github.com/matteocng/react-flag-icon-css'
@@ -78,13 +85,10 @@ const config = options => {
7885
__USE_CSS_MODULES__: JSON.stringify(JSON.parse(process.env.USE_CSS_MODULES || 'true'))
7986
})
8087
],
81-
node: {
82-
fs: "empty"
83-
},
84-
module: {
85-
rules: getRules(options)
86-
}
87-
}
88+
postcss: (): [] => ([
89+
autoprefixer({ browsers: ['last 4 versions'] })
90+
])
91+
}
8892
}
8993

9094
export default config

0 commit comments

Comments
 (0)