-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
executable file
·113 lines (98 loc) · 3.12 KB
/
run.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
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/env node
'use strict';
const apps = require('./apps.json');
const { log } = require('util');
const fs = require('mz/fs');
const path = require('path');
const commandLineArgs = require('command-line-args')
const webpack = require('webpack');
const webpackDevServer = require('webpack-dev-server');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const optionDefinitions = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'src', type: String, multiple: false, defaultOption: true },
{ name: 'port', type: Number, multiple: false },
];
// process.traceDeprecation = true;
var [,, ...args] = process.argv;
// log(`args: ${args}`);
let _args = commandLineArgs(optionDefinitions)
// log("options:", _args);
let bDevelop;
let lastIndex = args[0].length - 1;
if (!lastIndex) {
log("no args provided");
}
bDevelop = args[0][lastIndex] !== '+';
if (!bDevelop) args[0] = args[0].slice(0, args[0].length - 1);
bDevelop ? log("DEBUG") : log("RELEASE");
let app = apps[args[0]];
if (!app) {
log("entry not found:", args);
process.exit();
}
// let context = path.join(__dirname, 'src/apps/', app.dir);
// let entry = [ `./${app.name}.js` ];
let context = __dirname;
let entry = [ `./src/apps/${app.dir}/${app.name}.js` ];
console.log("-> dirname: " + __dirname);
console.log("-> entry: " + entry);
const config = require('./webpack.config.js');
if (bDevelop) {
process.env.DEBUG = true;
Object.assign(config, {
mode: 'development',
devtool: "inline-source-map",
entry,
context,
plugins:[
new HtmlWebpackPlugin({ title: app.dir + " development"}),
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
new webpack.DefinePlugin({ __DEBUG__: JSON.stringify(true) }),
]
});
const options = {
//contentBase: 'www', // Relative directory for base of server
host: '0.0.0.0', // Change to '0.0.0.0' for external facing server
hot: true, // Live-reload
hotOnly: true,
inline: true,
noInfo: true,
port: _args.port || 3020,
stats: {
colors: true
}
};
webpackDevServer.addDevServerEntrypoints(config, options);
const compiler = webpack(config);
const server = new webpackDevServer(compiler, options);
server.listen(options.port, 'localhost', () => {
console.log('-> dev server listening on port ' + options.port);
});
}
else {
process.env.DEBUG = false;
Object.assign(config, {
mode: 'production',
entry,
context,
output: {
path: path.resolve("build/", app.dir),
filename: 'bundle.js',
},
plugins: [
new HtmlWebpackPlugin({ title: app.dir }),
new webpack.DefinePlugin({ __DEBUG__: JSON.stringify(false) }),
new UglifyJsPlugin(),
]
});
const compiler = webpack(config);
compiler.run((err, stats) => {
if (err || stats.hasErrors()) {
log(err, stats);
}
log("done");
});
}