Add minification

This commit is contained in:
schmelczerandras 2020-10-07 14:13:22 +02:00
parent b3da27e73b
commit d34f25295c
14 changed files with 142 additions and 94 deletions

View file

@ -13,16 +13,16 @@
"keywords": [], "keywords": [],
"author": "András Schmelczer <andras@schmelczer.dev> (https://schmelczer.dev/)", "author": "András Schmelczer <andras@schmelczer.dev> (https://schmelczer.dev/)",
"devDependencies": { "devDependencies": {
"nodemon": "^2.0.4",
"concurrently": "^5.3.0",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"terser-webpack-plugin": "^2.3.5",
"webpack-dev-server": "^3.10.3",
"clean-webpack-plugin": "^3.0.0", "clean-webpack-plugin": "^3.0.0",
"concurrently": "^5.3.0",
"esbuild-loader": "^2.4.0",
"nodemon": "^2.0.4",
"raw-loader": "^4.0.1", "raw-loader": "^4.0.1",
"resolve-url-loader": "^3.1.1", "resolve-url-loader": "^3.1.1",
"ts-loader": "^8.0.1" "terser-webpack-plugin": "^2.3.5",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
}, },
"dependencies": { "dependencies": {
"cors": "^2.8.5", "cors": "^2.8.5",

View file

@ -1,14 +0,0 @@
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": false,
"target": "es5",
"downlevelIteration": true,
"allowJs": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"module": "commonjs",
"esModuleInterop": true
}
}

View file

@ -1,13 +1,15 @@
const path = require('path'); const path = require('path');
const TerserJSPlugin = require('terser-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const nodeExternals = require('webpack-node-externals'); const nodeExternals = require('webpack-node-externals');
const { ESBuildPlugin } = require('esbuild-loader');
const TerserJSPlugin = require('terser-webpack-plugin');
const PATHS = { const PATHS = {
entryPoint: path.resolve(__dirname, 'src/main.ts'), entryPoint: path.resolve(__dirname, 'src/main.ts'),
bundles: path.resolve(__dirname, 'dist'), bundles: path.resolve(__dirname, 'dist'),
}; };
module.exports = { module.exports = (env, argv) => ({
entry: { entry: {
main: [PATHS.entryPoint], main: [PATHS.entryPoint],
}, },
@ -21,21 +23,29 @@ module.exports = {
filename: '[name].js', filename: '[name].js',
path: PATHS.bundles, path: PATHS.bundles,
}, },
devtool: 'source-map', devtool: argv.mode === 'development' ? 'source-map' : false,
watchOptions: { watchOptions: {
poll: true poll: true
}, },
optimization: { optimization: {
minimize: true, minimize: argv.mode !== 'development',
usedExports: true,
minimizer: [ minimizer: [
new TerserJSPlugin({ new TerserJSPlugin({
sourceMap: true, sourceMap: false,
cache: true, test: /\.js$/,
test: /\.ts$/, terserOptions: {
keep_classnames: true,
}
}), }),
], ],
}, },
plugins: [
new ESBuildPlugin(),
new CleanWebpackPlugin({
protectWebpackAssets: false,
cleanAfterEveryBuildPatterns: [],
}),
],
module: { module: {
rules: [ rules: [
{ {
@ -50,14 +60,15 @@ module.exports = {
}, },
{ {
test: /\.ts$/, test: /\.ts$/,
use: { loader: 'esbuild-loader',
loader: 'ts-loader', options: {
}, loader: 'ts',
exclude: /node_modules/, target: 'es2015',
}
}, },
], ],
}, },
resolve: { resolve: {
extensions: ['.ts', '.js'], extensions: ['.ts', '.js'],
}, },
}; });

View file

@ -6,7 +6,7 @@
"scripts": { "scripts": {
"start": "webpack-dev-server --mode development", "start": "webpack-dev-server --mode development",
"lint": "eslint --fix \"src/**/*.ts\" && prettier --write \"src/**/*.ts\"", "lint": "eslint --fix \"src/**/*.ts\" && prettier --write \"src/**/*.ts\"",
"build": "webpack --mode production && find dist -type f -not -name '*.html' | xargs rm", "build": "webpack --mode production",
"try-build": "npm run build && cd dist && python3 -m http.server 8080", "try-build": "npm run build && cd dist && python3 -m http.server 8080",
"initialize": "npm install" "initialize": "npm install"
}, },
@ -18,11 +18,9 @@
} }
}, },
"devDependencies": { "devDependencies": {
"webpack": "^4.43.0", "terser-webpack-plugin": "^4.2.2",
"webpack-cli": "^3.3.11",
"terser-webpack-plugin": "^2.3.5",
"webpack-dev-server": "^3.10.3",
"clean-webpack-plugin": "^3.0.0", "clean-webpack-plugin": "^3.0.0",
"esbuild-loader": "^2.4.0",
"html-webpack-inline-source-plugin": "0.0.10", "html-webpack-inline-source-plugin": "0.0.10",
"html-webpack-plugin": "^3.2.0", "html-webpack-plugin": "^3.2.0",
"image-webpack-loader": "^6.0.0", "image-webpack-loader": "^6.0.0",
@ -34,7 +32,10 @@
"sass": "^1.26.3", "sass": "^1.26.3",
"sass-loader": "^9.0.2", "sass-loader": "^9.0.2",
"svg-url-loader": "^6.0.0", "svg-url-loader": "^6.0.0",
"ts-loader": "^8.0.1" "webpack": "^4.43.0",
"webpack-bundle-analyzer": "^3.9.0",
"webpack-cli": "^3.3.11",
"webpack-dev-server": "^3.10.3"
}, },
"browserslist": [ "browserslist": [
"defaults" "defaults"

View file

@ -56,7 +56,7 @@ export class Game {
this.socket.on(TransportEvents.ServerToPlayer, (serialized: string) => { this.socket.on(TransportEvents.ServerToPlayer, (serialized: string) => {
const command = deserialize(serialized); const command = deserialize(serialized);
console.log(command); //console.log(command);
this.gameObjects.sendCommand(command); this.gameObjects.sendCommand(command);
}); });

View file

@ -38,14 +38,15 @@ export class GameObjectContainer extends CommandReceiver {
[DeleteObjectsCommand.type]: (c: DeleteObjectsCommand) => [DeleteObjectsCommand.type]: (c: DeleteObjectsCommand) =>
c.ids.forEach((id: Id) => this.objects.delete(id)), c.ids.forEach((id: Id) => this.objects.delete(id)),
[UpdateObjectsCommand.type]: (c: UpdateObjectsCommand) => [UpdateObjectsCommand.type]: (c: UpdateObjectsCommand) => {
c.objects.forEach((o) => { c.objects.forEach((o) => {
this.objects.delete(o.id); this.objects.delete(o.id);
this.addObject(o); this.addObject(o);
if (o.id === this.player.id) { if (o.id === this.player.id) {
this.player = o as CharacterView; this.player = o as CharacterView;
} }
}), });
},
}; };
protected defaultCommandExecutor(c: Command) { protected defaultCommandExecutor(c: Command) {

View file

@ -1,18 +0,0 @@
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": false,
"target": "es6",
"downlevelIteration": true,
"allowJs": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"moduleResolution": "Node",
"module": "es6",
"lib": [
"es2015",
"dom"
]
}
}

View file

@ -1,28 +1,29 @@
const path = require('path'); const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin'); const HtmlWebpackPlugin = require('html-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin'); const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const TerserJSPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin'); const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin'); const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin'); const HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin');
const Sass = require('sass'); const Sass = require('sass');
const TerserJSPlugin = require('terser-webpack-plugin');
const { ESBuildPlugin } = require('esbuild-loader');
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
const PATHS = { const PATHS = {
entryPoint: path.resolve(__dirname, 'src/index.ts'), entryPoint: path.resolve(__dirname, 'src/index.ts'),
bundles: path.resolve(__dirname, 'dist'), bundles: path.resolve(__dirname, 'dist'),
}; };
module.exports = { module.exports = (env, argv) => ({
entry: { entry: {
index: PATHS.entryPoint, index: PATHS.entryPoint,
}, },
target: 'web', target: 'web',
output: { output: {
filename: '[name].[contenthash].js', filename: '[name].js',
path: PATHS.bundles, path: PATHS.bundles,
}, },
devtool: argv.mode === 'development' ? 'source-map' : false,
devtool: 'source-map',
devServer: { devServer: {
host: '0.0.0.0', host: '0.0.0.0',
disableHostCheck: true, disableHostCheck: true,
@ -31,19 +32,20 @@ module.exports = {
}, },
}, },
optimization: { optimization: {
minimize: false, minimize: argv.mode !== 'development',
usedExports: true,
minimizer: [ minimizer: [
new TerserJSPlugin({ new TerserJSPlugin({
sourceMap: true, sourceMap: false,
cache: true, test: /\.js$/,
test: /\.ts$/, terserOptions: {
keep_classnames: true,
}
}), }),
new OptimizeCSSAssetsPlugin({}), new OptimizeCSSAssetsPlugin({}),
], ],
}, },
plugins: [ plugins: [
new CleanWebpackPlugin(), new ESBuildPlugin(),
new HtmlWebpackPlugin({ new HtmlWebpackPlugin({
xhtml: true, xhtml: true,
template: './src/index.html', template: './src/index.html',
@ -53,14 +55,18 @@ module.exports = {
removeRedundantAttributes: true, removeRedundantAttributes: true,
removeScriptTypeAttributes: true, removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true, removeStyleLinkTypeAttributes: true,
useShortDoctype: true, useShortDoctype: true
}, },
inlineSource: '.(js|css)$', inlineSource: '.(js|css)$',
}), }),
new HtmlWebpackInlineSourcePlugin(), new HtmlWebpackInlineSourcePlugin(),
new MiniCssExtractPlugin({ new MiniCssExtractPlugin({
filename: '[name].[contenthash].css', filename: '[name].css',
chunkFilename: '[id].[contenthash].css', chunkFilename: '[id].css',
}),
new CleanWebpackPlugin({
protectWebpackAssets: false,
cleanAfterEveryBuildPatterns: ['*.js', '*.css'],
}), }),
], ],
module: { module: {
@ -98,14 +104,15 @@ module.exports = {
}, },
{ {
test: /\.ts$/, test: /\.ts$/,
use: { loader: 'esbuild-loader',
loader: 'ts-loader', options: {
}, loader: 'ts',
exclude: /node_modules/, target: 'es2015',
}
}, },
], ],
}, },
resolve: { resolve: {
extensions: ['.ts', '.js'], extensions: ['.ts', '.js'],
}, },
}; });

View file

@ -3,7 +3,7 @@
"private": true, "private": true,
"scripts": { "scripts": {
"start": "lerna run --parallel start", "start": "lerna run --parallel start",
"try-build": "lerna run --parallel try-build", "try-build": "lerna run --parallel try-build-before && lerna run --parallel try-build",
"build": "lerna run build", "build": "lerna run build",
"initialize": "lerna run initialize" "initialize": "lerna run initialize"
}, },

View file

@ -2,9 +2,19 @@
"name": "shared", "name": "shared",
"private": true, "private": true,
"description": "Shared library between backend and frontend", "description": "Shared library between backend and frontend",
"main": "src/main.ts", "main": "lib/main.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "start": "webpack --mode development --watch",
"lint": "eslint --fix \"src/**/*.ts\" && prettier --write \"src/**/*.ts\"",
"build": "webpack --mode production",
"try-build-before": "npm run build",
"initialize": "npm install" "initialize": "npm install"
},
"devDependencies": {
"clean-webpack-plugin": "^3.0.0",
"terser-webpack-plugin": "^2.3.8",
"ts-loader": "^8.0.3",
"webpack": "^4.43.0",
"webpack-cli": "^3.3.11"
} }
} }

View file

@ -1,7 +1,7 @@
import { Command } from '../command'; import { Command } from '../command';
export class StepCommand extends Command { export class StepCommand extends Command {
public constructor(public readonly deltaTimeInMiliseconds: DOMHighResTimeStamp) { public constructor(public readonly deltaTimeInMiliseconds: number) {
super(); super();
} }
} }

View file

@ -1,4 +1,4 @@
export const serialize = (object): string => { export const serialize = (object: any): string => {
return JSON.stringify(object, (_, value) => { return JSON.stringify(object, (_, value) => {
if (value?.__serializable_type) { if (value?.__serializable_type) {
return [value.__serializable_type, ...value.toArray()]; return [value.__serializable_type, ...value.toArray()];

View file

@ -1,18 +1,20 @@
{ {
"compilerOptions": { "compilerOptions": {
"outDir": "./dist/", "outDir": "lib",
"sourceMap": true, "sourceMap": true,
"noImplicitAny": false, "declaration": true,
"target": "es6", "declarationMap": true,
"downlevelIteration": true, "target": "es2015",
"allowJs": true, "esModuleInterop": true,
"strict": true,
"experimentalDecorators": true, "experimentalDecorators": true,
"allowSyntheticDefaultImports": true, "downlevelIteration": true,
"moduleResolution": "Node", "moduleResolution": "Node",
"module": "es6", "module": "es6",
"composite": true,
"lib": [ "lib": [
"es2015", "dom",
"dom" "es2017"
] ]
} }
} }

48
shared/webpack.config.js Normal file
View file

@ -0,0 +1,48 @@
const path = require('path');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const PATHS = {
entryPoint: path.resolve(__dirname, 'src/main.ts'),
bundles: path.resolve(__dirname, 'lib'),
};
module.exports = (env, argv) => ({
entry: {
main: PATHS.entryPoint,
},
target: 'node',
output: {
filename: '[name].js',
path: PATHS.bundles,
libraryTarget: 'umd',
umdNamedDefine: true,
},
devtool: argv.mode === 'development' ? 'source-map' : false,
watchOptions: {
poll: true,
ignored: /node_modules/
},
optimization: {
minimize: false,
},
plugins: [
new CleanWebpackPlugin({
protectWebpackAssets: false,
cleanAfterEveryBuildPatterns: [],
}),
],
module: {
rules: [
{
test: /\.ts$/,
use: {
loader: 'ts-loader',
},
exclude: /node_modules/,
}
],
},
resolve: {
extensions: ['.ts', '.js'],
},
});