-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.config.js
192 lines (172 loc) · 5.44 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
var webpack = require('webpack');
var browserify = require('browserify');
var path = require('path');
var fs = require('fs');
var os = require('os');
var dts = require('dts-bundle');
var deleteEmpty = require('delete-empty');
/* small hack to build map of node modules used for excluding from webpack */
var nodeModules = {};
fs.readdirSync('node_modules').filter(function (x) {
return ['.bin'].indexOf(x) === -1;
}).forEach(function (mod) {
nodeModules[mod] = 'commonjs ' + mod;
});
/* helper function to get into build directory */
var libPath = function(name) {
if ( undefined === name ) {
return path.join(__dirname, 'lib');
}
return path.join(__dirname, 'lib', name);
}
/* helper to clean leftovers */
var outputCleanup = function(dir, initial) {
if (false == fs.existsSync(libPath())){
return;
}
if ( true == initial ) {
console.log("Build leftover found, cleans it up.");
}
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(), true);
console.log("Build started... Good luck!");
} else if ( 1 == percentage ) {
// TODO: No Error detection. :(
create_browser_version(webpack_opts.output.filename);
// Invokes dts bundling
console.log("Bundling d.ts files ...");
dts.bundle(bundle_opts);
// Invokes lib/ cleanup
deleteEmpty(bundle_opts.baseDir, function(err, deleted) {
if ( err ) {
console.error("Couldn't clean up : " + err);
throw err;
} else {
console.log("Cleanup " + deleted);
}
});
}
}
var bundle_opts = {
// Required
// name of module likein package.json
// - used to declare module & import/require
name: 'ts-library-starter',
// path to entry-point (generated .d.ts file for main module)
// if you want to load all .d.ts files from a path recursively you can use "path/project/**/*.d.ts"
// ^ *** Experimental, TEST NEEDED, see "All .d.ts files" section
// - either relative or absolute
main: 'lib/main.d.ts',
// Optional
// base directory to be used for discovering type declarations (i.e. from this project itself)
// - default: dirname of main
baseDir: 'lib',
// path of output file. Is relative from baseDir but you can use absolute paths.
// if starts with "~/" then is relative to current path. See https://github.com/TypeStrong/dts-bundle/issues/26
// ^ *** Experimental, TEST NEEDED
// - default: "<baseDir>/<name>.d.ts"
out: 'main.d.ts',
// include typings outside of the 'baseDir' (i.e. like node.d.ts)
// - default: false
externals: false,
// reference external modules as <reference path="..." /> tags *** Experimental, TEST NEEDED
// - default: false
referenceExternals: false,
// filter to exclude typings, either a RegExp or a callback. match path relative to opts.baseDir
// - RegExp: a match excludes the file
// - function: (file:String, external:Boolean) return true to exclude, false to allow
// - always use forward-slashes (even on Windows)
// - default: *pass*
exclude: /^defs\/$/,
// delete all source typings (i.e. "<baseDir>/**/*.d.ts")
// - default: false
removeSource: true,
// newline to use in output file
newline: os.EOL,
// indentation to use in output file
// - default 4 spaces
indent: ' ',
// prefix for rewriting module names
// - default ''
prefix: '__',
// separator for rewriting module 'path' names
// - default: forward slash (like sub-modules)
separator: '/',
// enable verbose mode, prints detailed info about all references and includes/excludes
// - default: false
verbose: false,
// emit although included files not found. See "Files not found" section.
// *** Experimental, TEST NEEDED
// - default: false
emitOnIncludedFileNotFound: false,
// emit although no included files not found. See "Files not found" section.
// *** Experimental, TEST NEEDED
// - default: false
emitOnNoIncludedFileNotFound: false,
// output d.ts as designed for module folder. (no declare modules)
outputAsModuleFolder: true
};
var webpack_opts = {
entry: './modules/main.ts',
target: 'node',
output: {
filename: libPath('main.js'),
libraryTarget: "commonjs2"
},
resolve: {
extensions: ['', '.ts', '.js']
},
module: {
preLoaders: [{ test: /\.ts$/, loader: 'tslint' }],
loaders: [{ test: /\.ts$/, loader: 'babel-loader!ts-loader' }]
},
// externals: nodeModules,
plugins: [
// TODO: Minifiy JS.
// new webpack.optimize.UglifyJsPlugin(),
new webpack.IgnorePlugin(/\.(css|less)$/),
new webpack.ProgressPlugin(percentage_handler)
],
tslint: {
emitErrors: true,
failOnHint: true
},
ts: {
compiler: 'typescript',
configFileName: 'tsconfig.json'
}
}
var create_browser_version = function (inputJs) {
let outputName = inputJs.replace(/\.[^/.]+$/, "");
outputName = `${outputName}.browser.js`;
console.log("Creating browser version ...");
let b = browserify(inputJs, {
standalone: bundle_opts.name,
});
b.bundle(function(err, src) {
if ( err != null ) {
console.error("Browserify error:");
console.error(err);
}
}).pipe(fs.createWriteStream(outputName));
}
module.exports = webpack_opts;