Skip to content

Commit

Permalink
Change to Webpack instead of Parcel (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonsam authored Aug 22, 2018
1 parent fc58464 commit b426aa8
Show file tree
Hide file tree
Showing 9 changed files with 2,334 additions and 1,032 deletions.
42 changes: 42 additions & 0 deletions bin/postbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const FILE_SYSTEM = require('fs');
const ARCHIVER = require('archiver');

const MAX_SIZE = 13 * 1024; // 13kb

FILE_SYSTEM.unlinkSync('./dist/main.js');
FILE_SYSTEM.unlinkSync('./dist/main.css');

let output = FILE_SYSTEM.createWriteStream('./dist/build.zip');
let archive = ARCHIVER('zip', {
zlib: { level: 9 } // set compression to best
});

output.on('close', function() {
const bytes = archive.pointer();
const percent = ((bytes / MAX_SIZE) * 100).toFixed(2);

if (bytes > MAX_SIZE) {
console.error(`Size overflow: ${bytes} bytes (${percent}%)`);
} else {
console.log(`Size: ${bytes} bytes (${percent}%)`);
}
});

archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
console.warn(err);
} else {
throw err;
}
});

archive.on('error', function(err) {
throw err;
});

archive.pipe(output);
archive.append(FILE_SYSTEM.createReadStream('./dist/index.html'), {
name: 'index.html'
});

archive.finalize();
21 changes: 17 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,29 @@
],
"license": "MIT",
"devDependencies": {
"archiver": "^2.1.1",
"autoprefixer": "^9.1.1",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"css-loader": "^1.0.0",
"html-webpack-inline-source-plugin": "^0.0.10",
"html-webpack-plugin": "^3.2.0",
"husky": "^0.14.3",
"kontra": "^4.0.1",
"parcel-bundler": "^1.9.7",
"mini-css-extract-plugin": "^0.4.2",
"optimize-css-assets-webpack-plugin": "^5.0.0",
"prettier": "^1.14.2",
"pretty-quick": "^1.6.0"
"pretty-quick": "^1.6.0",
"style-loader": "^0.22.1",
"webpack": "^4.17.0",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
},
"scripts": {
"build": "parcel build index.html",
"watch": "parcel index.html",
"start": "webpack-dev-server --mode development",
"build": "webpack --mode=production",
"postbuild": "node bin/postbuild.js",
"precommit": "pretty-quick --staged"
},
"prettier": {
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion index.html → src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

<body>
<canvas width="800" height="600"></canvas>
<script src="src/index.js"></script>
</body>

</html>
81 changes: 2 additions & 79 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,2 @@
import 'kontra/src/core';
import 'kontra/src/sprite';
import 'kontra/src/gameLoop';
import 'kontra/src/pointer';
import 'kontra/src/keyboard';
import 'kontra/src/assets';
import 'kontra/src/spriteSheet';
import 'kontra/src/tileEngine';
import text from './text/';

const GAME_STATES = { playing: 0, won: 1, lost: 2 };
let currentGameState = GAME_STATES.playing;
let validLetters = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'.split(
','
);
let textAlreadyTyped = '';
let textLeftToType = 'watermelon';

let checkWin = () => {
if (
textLeftToType === textAlreadyTyped &&
currentGameState === GAME_STATES.playing
) {
currentGameState = GAME_STATES.won;
}
};

let initKeyboardInput = () => {
let keyCallback = ({ key }) => {
let nextCharToType = textLeftToType.replace(textAlreadyTyped, '').charAt(0);
if (nextCharToType === key) {
textAlreadyTyped += key;
checkWin();
}
};
validLetters.forEach((key) => {
kontra.keys.bind(key, keyCallback);
});
};

let main = () => {
kontra.init();

initKeyboardInput();

let loop = kontra.gameLoop({
update: () => {},
render: () => {
// Game State Machine
switch (currentGameState) {
case GAME_STATES.playing:
text.drawText({ text: 'Type the text below to win:', x: 0, y: 25 });
text.drawText({ text: textLeftToType, x: 0, y: 75 });
text.drawText({ text: textAlreadyTyped, color: 'blue', x: 0, y: 75 });
break;
case GAME_STATES.won:
text.drawText({
text: 'You typed a word!',
color: 'green',
x: 0,
y: 25
});
break;
case GAME_STATES.lost:
text.drawText({
text: 'You lost!',
color: 'red',
x: 0,
y: 25
});
break;
}
}
});

loop.start();
};

main();
import './js/index.js';
import './css/index.css';
79 changes: 79 additions & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'kontra/src/core';
import 'kontra/src/sprite';
import 'kontra/src/gameLoop';
import 'kontra/src/pointer';
import 'kontra/src/keyboard';
import 'kontra/src/assets';
import 'kontra/src/spriteSheet';
import 'kontra/src/tileEngine';
import text from './text/';

const GAME_STATES = { playing: 0, won: 1, lost: 2 };
let currentGameState = GAME_STATES.playing;
let validLetters = 'a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z'.split(
','
);
let textAlreadyTyped = '';
let textLeftToType = 'watermelon';

let checkWin = () => {
if (
textLeftToType === textAlreadyTyped &&
currentGameState === GAME_STATES.playing
) {
currentGameState = GAME_STATES.won;
}
};

let initKeyboardInput = () => {
let keyCallback = ({ key }) => {
let nextCharToType = textLeftToType.replace(textAlreadyTyped, '').charAt(0);
if (nextCharToType === key) {
textAlreadyTyped += key;
checkWin();
}
};
validLetters.forEach((key) => {
kontra.keys.bind(key, keyCallback);
});
};

let main = () => {
kontra.init();

initKeyboardInput();

let loop = kontra.gameLoop({
update: () => {},
render: () => {
// Game State Machine
switch (currentGameState) {
case GAME_STATES.playing:
text.drawText({ text: 'Type the text below to win:', x: 0, y: 25 });
text.drawText({ text: textLeftToType, x: 0, y: 75 });
text.drawText({ text: textAlreadyTyped, color: 'blue', x: 0, y: 75 });
break;
case GAME_STATES.won:
text.drawText({
text: 'You typed a word!',
color: 'green',
x: 0,
y: 25
});
break;
case GAME_STATES.lost:
text.drawText({
text: 'You lost!',
color: 'red',
x: 0,
y: 25
});
break;
}
}
});

loop.start();
};

main();
File renamed without changes.
52 changes: 52 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require('webpack');
const PATH = require('path');
const HTML_PLUGIN = require('html-webpack-plugin');
const MINI_CSS_EXTRACT_PLUGIN = require('mini-css-extract-plugin');
const HTML_INLINE_SOURCE_PLUGIN = require('html-webpack-inline-source-plugin');
const OPTIMIZE_CSS_ASSETS_PLUGIN = require('optimize-css-assets-webpack-plugin');
const IS_PRODUCTION = process.env.npm_lifecycle_event === 'build';

const APP_ENTRY = PATH.resolve('src', 'index.js');

let jsRule = {
test: /\.js$/,
use: ['babel-loader']
};

let cssRule = {
test: /\.css$/,
use: [
MINI_CSS_EXTRACT_PLUGIN.loader,
{
loader: 'css-loader'
}
]
};

const CONFIGURATION = {
entry: APP_ENTRY,
devtool: !IS_PRODUCTION && 'source-map',
module: {
rules: [jsRule, cssRule]
},
plugins: [
new HTML_PLUGIN({
template: 'src/index.html',
minify: IS_PRODUCTION && {
collapseWhitespace: true
},
inlineSource: IS_PRODUCTION && '.(js|css)$'
}),
new HTML_INLINE_SOURCE_PLUGIN(),
new OPTIMIZE_CSS_ASSETS_PLUGIN({}),
new MINI_CSS_EXTRACT_PLUGIN({
filename: '[name].css'
})
],
devServer: {
stats: 'minimal',
overlay: true
}
};

module.exports = CONFIGURATION;
Loading

0 comments on commit b426aa8

Please sign in to comment.