Skip to content

Commit

Permalink
Merge pull request #3 from aaronpurt/setupCSS
Browse files Browse the repository at this point in the history
This is the CSS modules and styles part of project
  • Loading branch information
aaronpurt authored Sep 30, 2018
2 parents 21f92e7 + 65cc17e commit 626a0fd
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 9 deletions.
170 changes: 168 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,177 @@ npm i webpack-dev-server -D

### Add a clean up script
* NPM Scripts: Tips: https://corgibytes.com/blog/2017/04/18/npm-tips/
``` Javascript
"scripts": {
"clean": "rm -rf ./node_modules ./package-lock.json ./dist"
}
```

# Configuring CSS

### Npm install css and style loading for webpack

``` Javascript
npm i css-loader style-loader -D
```


### Update the webpack.config.js file like below

``` Javascript
const HtmlWebPackPlugin = require("html-webpack-plugin");

# Configuring CSS
const htmlWebpackPlugin = new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
});

module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
}
]
},
plugins: [htmlWebpackPlugin]
};
```

### Notes

* Order of loaders is important
* Need to resolve the CSS files before adding to DOM with style-loader
* Webpack by default: uses the `loaders` from `right -> left`
* - css-loads 1st followed by style-loader second. css-loader resolves the files.


### Making the CSS modular

* This means the class name wil be scoped locally and specifically to the component.
* Provide `options` to the css-loader.

``` Javascript

const HtmlWebPackPlugin = require("html-webpack-plugin");

const htmlWebpackPlugin = new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
});

module.exports = {
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
modules: true,
importLoaders: 1,
localIdentName: "[name]_[local]_[hash:base64]",
sourceMap: true,
minimize: true
}
}
]
}
]
},
plugins: [htmlWebpackPlugin]
};

```
Each loader is an object with a key-value pair.
* modules:true; Enables CSS modules
* importLoaders:Integer; Configures how many loaders before css-loader should be applied. Example: sass-loader would come before css-loader
localIdentName: Configures generated Identification
- [name] name of component
- [local] is name of the class/id
- [hash:base64] randomly generated hash for each components CSS. (Unique module with this ID. )




### Entry and output points

Webpack 4 by default has default entry point of `index.js` in src folder. You can change this default.

Example
``` javascript
module.exports = {
entry: "./src/app.js",
module: {
...
}
}
```

Or

``` javascript
const path = require('path')
module.exports = {
entry: "./src/app.js",
output: {
path: path.resolve(‘dist’),
filename:bundled.js
},
module: {
...
}
}
```

###
###

``` javascript

```


###

``` javascript

```


###

``` javascript

```

###

``` javascript

```

###

``` javascript

```

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
"@babel/preset-env": "^7.1.0",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.4",
"css-loader": "^1.0.0",
"html-webpack-plugin": "^3.2.0",
"style-loader": "^0.23.0",
"webpack": "^4.20.2",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.9"
Expand Down
4 changes: 4 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.primaryColor {
background-color: red;
color:blue;
}
4 changes: 3 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
// 2
import React from "react";
import ReactDOM from "react-dom";
import styles from "./index.css";


const Index = () => {
return <div>Hello Amazing React!</div>;
return <div className={styles.primaryColor}>Hello Amazing React!</div>;
};

ReactDOM.render(<Index />, document.getElementById("root"));
28 changes: 22 additions & 6 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
const HtmlWebPackPlugin = require("html-webpack-plugin");

// template: value template where looking for my html file.
// filename: value is the name of the minified html file generated in dist folder
const HtmlWebPackPlugin = require("html-webpack-plugin");

const htmlPlugin = new HtmlWebPackPlugin({
const htmlWebpackPlugin = new HtmlWebPackPlugin({
template: "./src/index.html",
filename: "./index.html"
});
Expand All @@ -17,8 +15,26 @@ module.exports = {
use: {
loader: "babel-loader"
}
},
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options: {
modules: true,
importLoaders: 1,
localIdentName: "[name]_[local]_[hash:base64]",
sourceMap: true,
minimize: true
}
}
]
}
]
},
plugins: [htmlPlugin]
};
plugins: [htmlWebpackPlugin]
};

0 comments on commit 626a0fd

Please sign in to comment.