-
Notifications
You must be signed in to change notification settings - Fork 4
/
webpack.config.js
148 lines (130 loc) · 3.92 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var nodeExternals = require('webpack-node-externals');
var webpack = require('webpack');
var browserify = require('browserify');
var path = require('path');
var fs = require('fs');
var os = require('os');
var deleteEmpty = require('delete-empty');
var PACKAGE_FILE = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf-8'));
var LIB_NAME = PACKAGE_FILE.name;
var UglifyJS = require("uglify-js");
var UglifyES = require("uglify-es");
/* helper function to get into build directory */
var libPath = function (name) {
if (undefined === name) {
return 'dist';
}
return path.join('dist', name);
}
/* helper to clean leftovers */
var outputCleanup = function (dir) {
if (false == fs.existsSync(libPath())) {
return;
}
var list = fs.readdirSync(dir);
for (var i = 0; i < list.length; i++) {
var filename = path.join(dir, list[i]);
var stat = fs.statSync(filename);
if (filename == '.' || filename == '..') {
// pass these files
} else if (stat.isDirectory()) {
// outputCleanup recursively
outputCleanup(filename, false);
} else {
// rm fiilename
fs.unlinkSync(filename);
}
}
fs.rmdirSync(dir);
};
/* precentage handler is used to hook build start and ending */
var percentage_handler = function handler(percentage, msg) {
if (0 === percentage) {
/* Build Started */
outputCleanup(libPath());
console.log('Build started... Good luck!');
} else if (1.0 === percentage) {
// TODO: No Error detection. :(
create_browser_version(webpack_opts.output.filename);
}
}
var webpack_opts = {
entry: './src/main/main.ts',
target: 'node',
output: {
filename: libPath('index.js'),
libraryTarget: 'commonjs2'
},
devtool: 'source-map',
resolve: {
extensions: ['.ts', '.js'],
modules: [
'node_modules',
'src',
]
},
module: {
loaders: [
{
enforce: 'pre',
test: /\.ts$/,
loader: 'tslint-loader',
exclude: /node_modules/,
},
{
test: /\.ts$/,
loader: 'ts-loader',
exclude: [
/node_modules/
],
},
{
test: /\.js$/,
loader: 'source-map-loader',
exclude: /node_modules/,
}
]
},
externals: [nodeExternals()],
plugins: [
new webpack.optimize.UglifyJsPlugin({sourceMap: true}),
new webpack.LoaderOptionsPlugin({
options: {
tslint: {
emitErrors: true,
failOnHint: true
}
}
}),
new webpack.ProgressPlugin(percentage_handler)
],
}
var create_browser_version = function (inputJs) {
let outputName = inputJs.replace(/\.[^/.]+$/, '');
outputName = `${outputName}.browser.js`;
console.log('Creating browser version ...');
let b = browserify(inputJs, {
standalone: LIB_NAME,
});
let writeStream = fs.createWriteStream(outputName);
const browserified = b.bundle(function (err, src) {
if (err !== null) {
console.error('Browserify error:');
console.error(err);
}
});
let data = "";
browserified.on('data', (chunk) => {
data += chunk;
});
browserified.on('end', () => {
const uglified = UglifyJS.minify(data).code;
//console.log(UglifyJS.minify(data));
const byte = encodeURI(uglified).split(/%..|./).length - 1;
const aproxMb = (byte / 1024 / 1024).toString().substring(0, 4);
console.log("Approximate size: " + aproxMb + " MB!");
writeStream.write(uglified);
writeStream.end();
});
};
module.exports = webpack_opts;