-
Notifications
You must be signed in to change notification settings - Fork 0
/
runp5.js
100 lines (86 loc) · 2.47 KB
/
runp5.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
let path = require('path');
let WebpackDevServer = require("webpack-dev-server");
let Webpack = require("webpack");
let cli = require('commander');
let packagejson = require('./package.json');
let isBuild = false;
cli.version(packagejson.version);
cli.command('build').action(function() { isBuild = true; });
cli.parse(process.argv);
if (!isBuild && cli.args.length !== 1) {
console.log(cli.args);
cli.help();
process.exit(1);
}
let port = 3000;
let url = 'http://localhost:' + port;
let entryFile = path.resolve(cli.args[0]);
// TODO: test for entryFile existence here
let webpackConfig = {
mode: 'development',
entry: [
entryFile,
path.resolve(__dirname, 'node_modules', 'webpack-dev-server') +
'/client?' + url
],
output: {
path: path.dirname(entryFile),
filename: 'runp5/bundle.js',
devtoolModuleFilenameTemplate: function(info) {
console.log('info:', info);
let isWebpackInternal = (
info.resourcePath.startsWith('(webpack)') ||
info.absoluteResourcePath.startsWith('webpack')
);
let isDevServerNodeModules = (
info.absoluteResourcePath.startsWith(__dirname) &&
info.resourcePath.includes('/node_modules/')
);
if (isWebpackInternal || isDevServerNodeModules) {
return `webpack://${info.namespace}/${info.resourcePath}`;
}
return url + '/' + info.resourcePath.replace(/^\.\//, '');
},
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: path.resolve(__dirname, 'node_modules', 'babel-loader'),
options: {
plugins: [
path.resolve(__dirname, 'node_modules', 'babel-plugin-transform-strict-mode'),
path.resolve(__dirname, 'simplejs-babel-plugin.js'),
],
}
},
},
{
resource: entryFile,
use: [
path.resolve(__dirname, 'game-loop-loader.js'),
],
}
],
},
devtool: 'source-map',
watch: true,
devServer: {
contentBase: path.resolve(__dirname, 'public'),
},
};
let webpackCompiler = Webpack(webpackConfig);
if (isBuild) {
// TODO(aria): Make this less hacky
webpackCompiler.run(function() {
console.log('compiled');
process.exit(0);
});
} else {
let server = new WebpackDevServer(webpackCompiler, webpackConfig.devServer);
server.listen(port, '127.0.0.1', function() {
console.log('Starting server on ' + url);
});
}