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

CommonsChunkPlugin example #61

Merged
merged 1 commit into from Mar 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions 03 Misc/05 Commons Chunk Plugin/README.md
@@ -0,0 +1,3 @@
# 00 TypeScript

TBD
40 changes: 40 additions & 0 deletions 03 Misc/05 Commons Chunk Plugin/package.json
@@ -0,0 +1,40 @@
{
"name": "boilerplate",
"version": "1.0.0",
"description": "Front-End Lemoncode Master, Bundle Modules, Webpack Demo 00 Boilerplate",
"main": "index.js",
"scripts": {
"start": "webpack-dev-server"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Lemoncode/webpack-2.x-by-sample.git"
},
"author": "Lemoncode",
"license": "MIT",
"bugs": {
"url": "https://github.com/Lemoncode/webpack-2.x-by-sample/issues"
},
"homepage": "https://github.com/Lemoncode/webpack-2.x-by-sample#readme",
"devDependencies": {
"@types/jquery": "^2.0.40",
"@types/left-pad": "^1.1.0",
"awesome-typescript-loader": "^3.1.2",
"css-loader": "^0.26.2",
"extract-text-webpack-plugin": "^2.0.0",
"file-loader": "^0.10.1",
"html-webpack-plugin": "^2.28.0",
"left-pad": "^1.1.3",
"node-sass": "^4.5.0",
"sass-loader": "^6.0.2",
"style-loader": "^0.13.2",
"typescript": "^2.2.1",
"url-loader": "^0.5.8",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.4.1"
},
"dependencies": {
"bootstrap": "^3.3.7",
"jquery": "^3.1.1"
}
}
9 changes: 9 additions & 0 deletions 03 Misc/05 Commons Chunk Plugin/src/averageService.ts
@@ -0,0 +1,9 @@
export function getAvg(scores): number {
return getTotalScore(scores) / scores.length;
}

function getTotalScore(scores): number {
return scores.reduce((score, count) => {
return score + count;
});
}
21 changes: 21 additions & 0 deletions 03 Misc/05 Commons Chunk Plugin/src/index.html
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Webpack 2.x by sample</title>
</head>
<body>
<div class="jumbotron">
<h1>Testing Bootstrap</h1>
<p>
Bootstrap is the most popular ...
</p>
</div>
Hello Webpack 2!
<div class="red-background ">
RedBackground stuff
</div>
</body>
</html>
5 changes: 5 additions & 0 deletions 03 Misc/05 Commons Chunk Plugin/src/mystyles.scss
@@ -0,0 +1,5 @@
$blue-color: teal;

.red-background {
background-color: $blue-color;
}
12 changes: 12 additions & 0 deletions 03 Misc/05 Commons Chunk Plugin/src/students.ts
@@ -0,0 +1,12 @@
import {getAvg} from "./averageService";
import * as $ from 'jquery';
import leftPad = require('left-pad');
Copy link
Member

Choose a reason for hiding this comment

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

Why are you using require instead of from?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh, the @types on it sucks big time. There is no way of using from with that. That is a typescript old type of import but there is no way of doing it in here because the types doesn't export a module.

I used this library for being super simple on the output

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also works a bit on explaining people that using import ... from ... is not working in all cases. In fact I have planned some talk about that.

Copy link
Member

Choose a reason for hiding this comment

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

Nice, thanks


$('body').css('background-color', 'lightSkyBlue');

const scores: number[] = [90, 75, 60, 99, 94, 30];
const averageScore: number = getAvg(scores);

const messageToDisplay: string = leftPad(`average score ${averageScore}`, 35, 'jorobateflanders');

document.write(messageToDisplay);
2 changes: 2 additions & 0 deletions 03 Misc/05 Commons Chunk Plugin/src/vendor.ts
@@ -0,0 +1,2 @@
import 'jquery';
import 'left-pad';
15 changes: 15 additions & 0 deletions 03 Misc/05 Commons Chunk Plugin/tsconfig.json
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"sourceMap": true,
"noLib": false,
"suppressImplicitAnyIndexErrors": true
},
"compileOnSave": false,
"exclude": [
"node_modules"
]
}
100 changes: 100 additions & 0 deletions 03 Misc/05 Commons Chunk Plugin/webpack.config.js
@@ -0,0 +1,100 @@
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

var basePath = __dirname;

module.exports = {
context: path.join(basePath, 'src'),
resolve: {
extensions: ['.js', '.ts']
},
entry: {
app: './students.ts',
appStyles: [
'./mystyles.scss',
],
vendor: './vendor.ts',
vendorStyles: [
'../node_modules/bootstrap/dist/css/bootstrap.css',
],
},
output: {
path: path.join(basePath, 'dist'),
filename: '[chunkhash].[name].js',
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
loader: 'awesome-typescript-loader',
},
{
test: /\.scss$/,
exclude: /node_modules/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{ loader: 'css-loader', },
{ loader: 'sass-loader', },
],
}),
},
{
test: /\.css$/,
include: /node_modules/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: {
loader: 'css-loader',
},
}),
},
// Loading glyphicons => https://github.com/gowravshekar/bootstrap-webpack
// Using here url-loader and file-loader
{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/octet-stream'
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
},
],
},
// For development https://webpack.js.org/configuration/devtool/#for-development
devtool: 'source-map',
devServer: {
port: 8080,
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor'
Copy link
Member

Choose a reason for hiding this comment

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

Do you have planned to explain something about manifest?
We introduced this configuration in 00 Intro/04 JQuery

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, my explanation is going to be very complete on everything related with CommonsChunkPlugin but this repo contains the final version of what I intend to explain.

Between us, I don't see why you have an external manifest on that example, but I will explain the cons and pros of that and a good use case of when that is an awesome idea.

Copy link
Member

Choose a reason for hiding this comment

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

Nice, thanks

}),
//Generate index.html in /dist => https://github.com/ampedandwired/html-webpack-plugin
new HtmlWebpackPlugin({
filename: 'index.html', //Name of file in ./dist/
template: 'index.html', //Name of template in ./src
hash: true,
Copy link
Member

Choose a reason for hiding this comment

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

Indentation

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

All the examples in here have that issue. It uses 3 tabs instead of 6 spaces and the copy & paste caught that.

Copy link
Member

Choose a reason for hiding this comment

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

Ok, thanks. I have to open an issue for that

}),
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
new ExtractTextPlugin({
filename: '[chunkhash].[name].css',
disable: false,
allChunks: true,
}),
],
};
@@ -1,4 +1,4 @@
# 05 Production Configuration
# 06 Production Configuration

In this demo we are going to create different builds for each environment.
We will learn how to configure it and how to reduce bundle file sizes.
Expand Down
16 changes: 15 additions & 1 deletion README.md
Expand Up @@ -260,7 +260,21 @@ Summary steps:
- Create other component and scss file with same class name.
- Create selector using custom class and Bootstrap class.

### 05 Production Configuration
### 05 Commons Chunk Plugin

In this demo we will learn how to split our application in different bundles and how does Webpack manage to do so with the CommonsChunkPlugin.
We will also learn some tips and tricks.

We will start from sample _02 Fx/00 TypeScript_.

Summary steps:
- Comment out the CommonsChunkPlugin to analyze what happens.
- Add it back and see the changes.
- Add a new third party library and see how the inline vendor grows.
- create a vendor file as an alternative to inline vendor in the config.
- move all the vendors imports there.

### 06 Production Configuration

In this demo we are going to create different builds for each environment.
We will learn how to configure it and how to reduce bundle file sizes.
Expand Down