-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
66 lines (64 loc) · 1.72 KB
/
webpack.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import path from 'path';
import webpack from 'webpack';
import express from 'express';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import Handlebars from 'handlebars';
const dirname = path.resolve( '.' );
/** @returns {webpack.Configuration} */
export default async function getConfig( env ) {
const loaders = {
typescript: { loader: 'ts-loader', options: {} }
};
return {
context: path.resolve( 'src' ),
devtool: 'source-map',
devServer: {
onListening: devServer => {
let leaderboard = [
{ name: 'Alice', score: 200, lines: 2, time: 10 },
{ name: 'Bob', score: 100, lines: 1, time: 10 }
];
devServer.app.use( express.json() );
devServer.app.post( '/leaderboard.php', ( req, res ) => {
if( req.body?.name ) {
leaderboard = [ ...leaderboard, req.body ].sort( ( a, b ) => b.score - a.score ).slice( 0, 10 );
}
res.json( leaderboard );
} );
}
},
entry: {
index: [ path.resolve( dirname, 'src', 'main' ) ]
},
mode: env.production ? 'production' : 'development',
module: {
rules: [
{ test: /\.ts$/i, use: [ loaders.typescript ] }
]
},
output: {
path: path.resolve( dirname, 'dist' )
},
plugins: [
new HtmlWebpackPlugin( {
title: 'Tetrominoes'
} ),
new CopyWebpackPlugin( {
patterns: [
{
from: '**/*.php',
transform: str => {
const template = Handlebars.compile( str.toString( 'utf-8' ), {} );
const { MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE } = process.env;
return template( { MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWORD, MYSQL_DATABASE } );
}
}
]
} )
],
resolve: {
extensions: [ '.ts', '.js' ]
}
};
}