-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.js
116 lines (105 loc) · 2.81 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
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;
/* helper function to get into dist 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: {
app:'./src/index.ts',
vendor: ["react", "sodiumjs"],
},
target: 'node',
output: {
filename: libPath('index.js'),
libraryTarget: 'commonjs2'
},
resolve: {
extensions: ['.ts', '.js'],
modules: [
'node_modules',
'src',
]
},
module: {
loaders: [
{
test: /\.ts$/,
loader: 'ts-loader',
exclude: [
/node_modules/
],
},
],
},
externals: [nodeExternals()],
plugins: [
new webpack.optimize.UglifyJsPlugin(),
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,
});
b.bundle(function(err, src) {
if ( err != null ) {
console.error('Browserify error:');
console.error(err);
}
}).pipe(fs.createWriteStream(outputName));
}
module.exports = webpack_opts;