-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.ts
117 lines (108 loc) · 3.04 KB
/
webpack.ts
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
import webpack from 'webpack';
import path from 'path';
import { argv } from 'process';
let env = process.env['NODE_ENV'];
let isProduction = env && env.match(/production/);
let watching = argv.reduce((prev, cur) => prev || cur === '--watch', false);
let config: webpack.Configuration = {
context: path.join(__dirname, 'src'),
entry: {
app: './redux-webmidi.ts'
},
output: {
filename: 'redux-webmidi.js',
path: path.resolve(__dirname, 'dist'),
library: 'ReduxWebmidi',
libraryTarget: 'commonjs2'
},
resolve: {
extensions: [ '.ts', '.tsx', 'js' ]
},
module: {
rules: [
{
test: /\.ts$/,
exclude: /node_modules/,
loader: 'ts-loader',
options: {
}
}
]
},
node: false,
externals: [
{
redux: {
root: 'redux',
commonjs2: 'redux',
commonjs: 'redux',
amd: 'redux'
}
}
],
plugins: [
new webpack.DefinePlugin({
'process.env': {
NODE_ENV: JSON.stringify(isProduction ? 'production' : 'development')
}
})
],
optimization: {
minimize: isProduction ? true : false
}
};
/**
* Start Build
*/
const runCallback = (err: Error, stats: webpack.Stats) => {
if (err) return console.error(err);
if (stats.hasErrors()) {
let statsJson = stats.toJson();
console.log('❌' + ' · Error · ' + 'redux-webmidi failed to compile:');
for (let error of statsJson.errors) {
console.log(error);
}
return;
}
console.log(
'✔️️' +
' · Success · ' +
'redux-webmidi' +
(isProduction ? ' (production) ' : ' (development) ') +
'built in ' +
(+stats.endTime - +stats.startTime + ' ms.')
);
};
const watchCallback = (err: Error, stats: webpack.Stats) => {
if (err) return console.error(err);
if (stats.hasErrors()) {
let statsJson = stats.toJson();
console.log('❌' + ' · Error · ' + 'redux-webmidi failed to compile:');
for (let error of statsJson.errors) {
console.log(error);
}
console.log('\n👀 · Watching for changes... · \n');
return;
}
console.log(
'✔️️' +
' · Success · ' +
'redux-webmidi' +
(isProduction ? ' (production) ' : ' (development) ') +
'built in ' +
(+stats.endTime - +stats.startTime + ' ms.') +
'\n👀 · Watching for changes... · \n'
);
};
if (watching) {
const compiler: webpack.Compiler = webpack(config);
compiler.watch({}, watchCallback);
} else {
// CommonJS
webpack(config).run(runCallback);
// Browser
webpack({
...config,
output: { ...config.output, libraryTarget: 'window', filename: 'redux-webmidi.browser.js' }
}).run(runCallback);
}